Skip to content

Gems, Eggs and Perl Modules

indirect edited this page Oct 25, 2010 · 38 revisions

On a fresh OS X installation there are three empty directories:

/Library/Ruby
/Library/Python
/Library/Perl

When you gem install, cpan -i or easy_install stuff gets installed to these directories in a nicely segregated fashion similar to Homebrew.

These directories start empty — you are meant to fill them with your stuff and not worry about it.

On fresh OS X installing to these three directories doesn’t require sudo.

It doesn’t work! I get some “permissions” error when I try to install stuff!

If you ever did a sudo gem, etc. before then a lot of files will have been created chown root. Fix with:

sudo chown -R $USER /Library/Ruby /Library/Perl /Library/Python

Rubygems without sudo

Executable files are the problem. Rubygems on OS X is hard-coded to install binaries to /usr/bin with sudo or ~/.gem/bin without sudo. To fix this, we will configure Rubygems to install to /usr/local/bin instead.

Once you have followed the instructions below, Rubygems can be upgraded with gem update --system. No sudo required because RubyGems is awesome and uses whatever is newest wherever the gems are installed.

If you use RVM, or just want everything in the Homebrew prefix

If you are using RVM, make sure you set GEM_HOME in your .bashrc before sourcing RVM.

echo "export GEM_HOME='$(brew --prefix)/Cellar/gems/1.8'" >> ~/.bashrc
source ~/.bashrc
gem install brewbygems

Brewbygems is a gem by indirect that runs brew link gems and brew unlink gems for you after gems are installed or uninstalled.

If you will only ever use the system Ruby

We need to add gem: -n/usr/local/bin to your ~/.gemrc. It’s YAML so do it manually or use this:

ruby -C$HOME -ryaml -e "
gemrc = YAML::load_file('.gemrc') rescue {}
gemrc['gem'] = '-n/usr/local/bin'
YAML::dump(gemrc, File.new('.gemrc', 'w'))"

Now when you run gem (without sudo) it will install executables to /usr/local/bin. With sudo, /usr/bin.

However all versions of RubyGems before 1.3.6 are buggy and ignore the above setting. Sadly a fresh install of Snow Leopard comes with 1.3.5. Currently the only known way to get round this is to upgrade rubygems as root:

sudo gem update --system

Now gem can be used without sudo.

Python eggs without sudo

There is no easy_uninstall. Homebrew provides pip, we suggest you use that instead of easy_install.

brew install pip

EasyInstall

If you must use easy_install, note that this method does not work that well if you use virtualenv. The ~/.pydistutils.cfg file will overwrite the virtual environment install location. See issue 1061 for more details.

echo '[install]
install-scripts=/usr/local/Cellar/PyPi/2.6/bin
install-data=/usr/local/Cellar/PyPi/2.6/share' > ~/.pydistutils.cfg

CPAN without sudo

Use cpanminus

cpanminus aka cpanm is a far friendlier installer for CPAN modules than either CPAN.pm or CPANPLUS. Just grab a copy and go.

$ wget http://xrl.us/cpanm
$ chmod +x cpanm
$ cpanm Some::Module

cpanm fails pretty often while trying to update very old perl modules on Mac OSX. To avoid it install ‘local::lib’ first and then use cpanm -l ~/perl5
thus having installed CPAN modules in your home directory.

Upgrading CPAN

Frankly, don’t bother. When CPAN works, you leave it alone!

$ cpan
cpan> o conf prerequisites_policy  # don't ask stupid questions
cpan> install YAML  # saves you a million warning messages
cpan> upgrade CPAN  # never seems to work

Install everything to Homebrew

Some people prefer to keep everything inside Homebrew’s prefix. It looks like this:

$ cpan -i File::Rename
$ brew ln CPAN
$ brew ls CPAN
/usr/local/Cellar/CPAN/5.8/bin/file-rename
/usr/local/Cellar/CPAN/5.8/share/man1/file-rename.1
/usr/local/Cellar/CPAN/5.8/share/man3/File::Rename.3pm
$ find /Library/Perl/5.8.8/File
/Library/Perl/5.8.8/File
/Library/Perl/5.8.8/File/Rename.pm

Do do this, change where Perl modules are installed:

cpan> o conf makepl_arg INSTALL_BASE=/usr/local/Cellar/CPAN
cpan> o conf mbuild_arg --install_base=/usr/local/Cellar/CPAN
cpan> o conf commit

Make sure you have at least ExtUtils::MakeMaker 6.31 installed. Its the first version that honors INSTALL_BASE. If not, install it immediately after reconfiguring. It will install itself with itself and honor INSTALL_BASE.

$ perl -wle 'use ExtUtils::MakeMaker;  print $ExtUtils::MakeMaker::VERSION'
6.56

Note that you have to `brew ln` every time you install stuff.

Clone this wiki locally