2014-10-26 14:54:36 +00:00
|
|
|
|
# Gems, Eggs and Perl Modules
|
2017-01-04 16:16:00 -06:00
|
|
|
|
|
2016-09-18 19:57:19 +01:00
|
|
|
|
On a fresh macOS installation there are three empty directories for
|
2014-10-26 14:54:36 +00:00
|
|
|
|
add-ons available to all users:
|
|
|
|
|
|
2018-10-05 17:23:22 -04:00
|
|
|
|
* `/Library/Ruby`
|
|
|
|
|
* `/Library/Python`
|
|
|
|
|
* `/Library/Perl`
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2019-01-26 17:13:14 +00:00
|
|
|
|
You need `sudo` to install to these like so: `sudo gem install`,
|
|
|
|
|
`sudo easy_install` or `sudo cpan -i`.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2018-10-05 17:23:22 -04:00
|
|
|
|
An option to avoid sudo is to use an access control list. For example:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
chmod +a 'user:<YOUR_NAME_HERE> allow add_subdirectory,add_file,delete_child,directory_inherit' /Library/Python/3.y/site-packages
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
will let you add packages to Python 3.y as yourself, which
|
2014-10-26 14:54:36 +00:00
|
|
|
|
is probably safer than changing the group ownership of the directory.
|
|
|
|
|
|
|
|
|
|
### So why was I using sudo?
|
2021-11-16 12:34:46 -05:00
|
|
|
|
|
2014-10-26 14:54:36 +00:00
|
|
|
|
Habit maybe?
|
|
|
|
|
|
|
|
|
|
One reason is executables go in `/usr/local/bin`. Usually this isn’t a
|
2021-07-14 14:22:31 +01:00
|
|
|
|
writable location. But if you installed Homebrew as we recommend on macOS Intel,
|
2014-10-26 14:54:36 +00:00
|
|
|
|
`/usr/local` will be writable without sudo. So now you are good to
|
2017-03-18 17:45:12 -04:00
|
|
|
|
install the development tools you need without risking the use of sudo.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2020-02-10 22:54:11 +01:00
|
|
|
|
### Python packages (eggs) without sudo using system’s Python
|
2021-11-16 12:34:46 -05:00
|
|
|
|
|
2014-10-26 14:54:36 +00:00
|
|
|
|
_This is only recommended if you **don't** use a brewed Python._
|
|
|
|
|
|
2016-09-18 19:57:19 +01:00
|
|
|
|
On macOS, any [Python version X.Y also searches in
|
2014-10-26 14:54:36 +00:00
|
|
|
|
`~/Library/Python/X.Y/lib/python/site-packages` for
|
2015-01-04 04:27:16 +01:00
|
|
|
|
modules](https://docs.python.org/2/install/index.html#inst-alt-install-user).
|
2014-10-26 14:54:36 +00:00
|
|
|
|
That dir might not yet exist, but you can create it:
|
2017-03-25 22:53:33 -04:00
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
|
|
|
|
To teach `easy_install` and `pip` to install there, either use the
|
2017-03-18 17:45:12 -04:00
|
|
|
|
`--user` switch or create a `~/.pydistutils.cfg` file with the
|
2014-10-26 14:54:36 +00:00
|
|
|
|
following content:
|
|
|
|
|
|
2018-10-05 17:23:22 -04:00
|
|
|
|
```
|
|
|
|
|
[install]
|
|
|
|
|
install_lib = ~/Library/Python/$py_version_short/lib/python/site-packages
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2020-02-10 22:54:11 +01:00
|
|
|
|
### Using virtualenv (with system Python)
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2018-10-03 21:03:22 +00:00
|
|
|
|
[Virtualenv](https://virtualenv.pypa.io/) ships `pip` and
|
2014-10-26 14:54:36 +00:00
|
|
|
|
creates isolated Python environments with separate site-packages,
|
2017-03-18 17:45:12 -04:00
|
|
|
|
therefore you won’t need sudo.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
## Rubygems without sudo
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
|
|
|
|
**If you use rbenv or RVM then you should ignore this stuff**
|
|
|
|
|
|
|
|
|
|
Brewed Ruby installs executables to `$(brew --prefix)/opt/ruby/bin`
|
|
|
|
|
without sudo. You should add this to your path. See the caveats in the
|
|
|
|
|
`ruby` formula for up-to-date information.
|
|
|
|
|
|
|
|
|
|
### With system’s Ruby
|
|
|
|
|
|
2015-10-07 12:52:44 +02:00
|
|
|
|
To make Ruby install to `/usr/local`, we need to add
|
2017-03-18 17:45:12 -04:00
|
|
|
|
`gem: -n/usr/local/bin` to your `~/.gemrc`. It’s YAML, so do it manually
|
2014-10-26 14:54:36 +00:00
|
|
|
|
or use this:
|
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
|
|
|
|
echo "gem: -n/usr/local/bin" >> ~/.gemrc
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
**However, all versions of RubyGems before 1.3.6 are buggy** and ignore
|
2014-10-26 14:54:36 +00:00
|
|
|
|
the above setting. Sadly a fresh install of Snow Leopard comes with
|
2017-02-23 12:20:07 -05:00
|
|
|
|
1.3.5. Currently the only known way to get around this is to upgrade
|
2014-10-26 14:54:36 +00:00
|
|
|
|
rubygems as root:
|
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
|
|
|
|
sudo gem update --system
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
### An alternative
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
|
|
|
|
Just install everything into the Homebrew prefix like this:
|
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
|
|
|
|
echo "export GEM_HOME=\"$(brew --prefix)\"" >> ~/.bashrc
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
|
|
|
|
### It doesn’t work! I get some “permissions” error when I try to install stuff!
|
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
*Note, maybe you shouldn’t do this on Lion, since Apple has decided it
|
2014-10-26 14:54:36 +00:00
|
|
|
|
is not a good default.*
|
|
|
|
|
|
|
|
|
|
If you ever did a `sudo gem`, etc. before then a lot of files will have
|
2017-03-18 17:45:12 -04:00
|
|
|
|
been created owned by root. Fix with:
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
2019-05-22 10:18:56 +01:00
|
|
|
|
sudo chown -R $(whoami) /Library/Ruby /Library/Perl /Library/Python
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
## Perl CPAN modules without sudo
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
The Perl module `local::lib` works similarly to rbenv/RVM (although for
|
2014-10-26 14:54:36 +00:00
|
|
|
|
modules only, not perl installations). A simple solution that only
|
2017-03-25 22:53:33 -04:00
|
|
|
|
pollutes your `/Library/Perl` a little is to install
|
|
|
|
|
[`local::lib`](https://metacpan.org/pod/local::lib) with sudo:
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
|
|
|
|
sudo cpan local::lib
|
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
Note that this will install some other dependencies like `Module::Install`.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
Then put the appropriate incantation in your shell’s startup, e.g. for
|
2020-05-11 21:26:45 -07:00
|
|
|
|
`.profile` you insert the below, for others see the
|
2017-03-25 22:53:33 -04:00
|
|
|
|
[`local::lib`](https://metacpan.org/pod/local::lib) docs.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```sh
|
2021-09-13 20:32:20 +08:00
|
|
|
|
eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"
|
2017-03-25 22:53:33 -04:00
|
|
|
|
```
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
|
|
|
|
Now (after you restart your shell) `cpan` or `perl -MCPAN -eshell` etc.
|
|
|
|
|
will install modules and binaries in `~/perl5` and the relevant
|
|
|
|
|
subdirectories will be in your `PATH` and `PERL5LIB` etc.
|
|
|
|
|
|
|
|
|
|
### Avoiding sudo altogether for Perl
|
|
|
|
|
|
|
|
|
|
If you don’t even want (or can’t) use sudo for bootstrapping
|
2017-03-18 17:45:12 -04:00
|
|
|
|
`local::lib`, just manually install `local::lib` in
|
2018-10-05 17:23:22 -04:00
|
|
|
|
`~/perl5` and add the relevant path to `PERL5LIB` before the `.bashrc` eval incantation.
|
2014-10-26 14:54:36 +00:00
|
|
|
|
|
2017-03-18 17:45:12 -04:00
|
|
|
|
Another alternative is to use `perlbrew` to install a separate copy of Perl in your home directory, or wherever you like:
|
2017-03-25 22:53:33 -04:00
|
|
|
|
|
|
|
|
|
```sh
|
2017-03-12 19:44:01 +00:00
|
|
|
|
curl -L https://install.perlbrew.pl | bash
|
2014-10-26 14:54:36 +00:00
|
|
|
|
perlbrew install perl-5.16.2
|
|
|
|
|
echo ".~/perl5/perlbrew/etc/bashrc" >> ~/.bashrc
|
|
|
|
|
```
|