Rubygems on Ubuntu (with Puppet if you like)

Debian packages and Rubygems: they get on like two angry cats in a sack. This post explains how you get Rubygems and Dpkg to play nicely on Ubuntu Hardy Heron.

What's the issue? The Debian Packaging System (DPKG) is pretty good as packaging systems go. It's had dependency support baked in for years, which means you can install one package, and any package that it needs, and so on. Rubygems is good in other ways. It's a very convenient way to use and distribute other people's Ruby code. Rubygems is optimised for developer convenience. The Debian system is optimised for stability. What happens when you try and make them work together? Bloodshed.

How do we cope? Most people get around it by installing the stock Rubygems into the Debian or Ubuntu system. This overwrites '/usr/bin/gem' with a version of Rubygems that will install gems into the '/usr/' directory on your system. What happens if you upgrade your Ubuntu distribution? The new version of Rubygems will overwrite the stock one. Because the Debian maintainers changed rubygems to install gems into a safer place (/var/lib/gems), you'll probably notice installed gems disappear.

But what's the downside? When the new Ubuntu LTS (Lucid Lynx) is released in April, you'll probably want to upgrade. I'm finding Hardy harder to support as new tools arrive (couchdb is a good example). Is it time to get your systems sorted out now, so that you don't break your apps on the upgrade?

How to do this? You can install the rubygems packages from Karmic Koala on Hardy Heron. Have a look at these instructions below. Note: this might make your Gems disappear. Do have some gem list --local output so you can put them back!

# HINT: run the sudo first or it will swallow any other text
sudo -s 
# rubygems and this scripts's dependencies
# if they are already installed, it won't reinstall
aptitude install wget ruby1.8 rdoc1.8  

# get the new gems on the system
cd /tmp
/usr/bin/wget   http://mirrors.kernel.org/ubuntu/pool/universe/libg/libgems-ruby/rubygems_1.3.5-1ubuntu2_all.deb
/usr/bin/wget http://mirrors.kernel.org/ubuntu/pool/universe/libg/libgems-ruby/rubygems1.8_1.3.5-1ubuntu2_all.deb
dpkg -i *.deb


# tell your system about them
cat <<HERE > /etc/profile.d/rubygems.sh
  export GEM_HOME=/var/lib/gems/1.8
  export GEM_PATH=/var/lib/gems/1.8
  PATH=${PATH}:${GEM_HOME}/bin
HERE

You'll need to log out and back in again to see the changes. I also have this handy Puppet manifest to do all this en masse. Comment if you want me to publish.

Image thanks to Jijy

Update: I also want to help try and get rubygems working out of the box in either Debian or Ubuntu. Watch this space. Hopefully I'll look at this over Xmas.

Update: Thanks to @auxesis for the comment. I've made a couple of changes to this post.

DevOps New Zealand