Skip to content

Gems, Eggs and Perl Modules

safronoff edited this page Oct 18, 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.

But…

Executable files are the problem:

Command With sudo Without sudo
easy_install foo /usr/local/bin/foo /usr/local/bin/foo
cpan -i foo /usr/local/bin/foo /usr/local/bin/foo
gem install foo /usr/bin/foo ~/.gem/bin/foo

It would be nice if gem install foo would install to /usr/local/bin.

Fixing Rubygems

If you only 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.

If you use RVM or similar software

If you are using RVM or similar software, don’t use gem: -n/usr/local/bin, which would force all gem installs for all Rubies into that same folder. Instead, set your GEM_HOME and GEM_PATH in your .bashrc before sourcing RVM.

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

Note, use pip instead of easy_install

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

brew install pip

Upgrading RubyGems

$ gem update --system

No sudo required because RubyGems is awesome and uses whatever is newest wherever the gems are installed.

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

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

RubyGems

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.

CPAN

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

EasyInstall

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
Clone this wiki locally