Zed Lopez

Apt-get Globally, Gem Locally

It seems that Debian packages and Ruby gems don’t play nicely together. I had apt-get installed rails, and my first attempt to use a rails app (someone else had written) blew up because it had an internal check for a Rails gem of a certain version. Since my Rails hadn’t been installed as a gem at all, it immediately failed.

So far as I can read on the Interwebs, most people facing this install rubygems as root, and do their subsequent gem installs as root, letting them write wherever in the filesystem they like.

That makes me queasy. I don’t want to mix two package systems in the same environment.

So here’s how I installed ruby/rubygems/rails in Ubuntu 7.04, with all gems under /usr/local.

Following the ruby1.8 package’s own instructions for a full Ruby 1.8 distribution:

sudo apt-get install ruby1.8 ruby1.8-dev ri1.8 rdoc1.8 irb1.8 ruby1.8-elisp ruby1.8-examples libdbm-ruby1.8 libgdbm-ruby1.8 libtcltk-ruby1.8 libopenssl-ruby1.8 libreadline-ruby1.8

I assign ownership of /usr/local and everything under it to the admin group, and make /usr/local and its subdirectories group-writable (per Ubuntu’s defaults, my primary login, which I created during installation, is a member of the admin group.)

sudo chown -R root:admin /usr/local 
sudo chmod 775 /usr/local /usr/local/*

I get and install rubygems.

mkdir /usr/local/lib/rubygems
export GEM_HOME=/usr/local/lib/rubygems
cd /usr/local/src
# as of this writing, the latest rubygems from http://rubyforge.org/frs/?group_id=126
wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar xzf rubygems-0.9.4.tgz
cd rubygems-0.9.4
ruby setup.rb config --prefix=/usr/local
ruby setup.rb setup
ruby setup.rb install

I add the following to my .bashrc, but you’ll want them in any environment using gems. With multiple users on a system, you might want to put this in /etc/bash.bashrc.

export GEM_HOME=/usr/local/lib/rubygems
export RUBYLIB=/usr/local/site_ruby/1.8
export RUBYOPT=rubygems
export PATH=$PATH:/usr/local/lib/rubygems/bin

The grand finale:

source .bashrc # or wherever you put them
gem install rails --include-dependencies

It’s that easy!