Adam Collins' Tumblelog Follow me on Twitter, Flickr, GitHub, or StackOverflow

May 25

7:39 pm

Integrating Ruby 1.8 and 1.9 with Lucid Lynx

Conveniently, Ruby versions 1.8, 1.9, and 1.9.1 are all available in the Lucid (Ubuntu 10.04 LTS) repositories. Inconveniently, only the rubygems packages have configurations assigned for Debian’s “alternatives” system. Here’s a quick fix!

The Issue

Lucid’s ruby1.8 and rubygems1.8 packages install the ruby1.8 command for the interpreter and the gem and gem1.8 commands for RubyGems administration. Simple.

I need to migrate existing ruby 1.8 apps and gems to ruby 1.9, though, so it would be nice to have both Ruby 1.8 and 1.9.1 installed and available. The necessary Lucid packages areruby1.8, ruby1.9.1, rubygems1.8, and rubygems1.9.1. Since the Ubuntu gems packages use /var/lib/gems/$VERSION/bin as the bin installation directory, there won’t be any 1.8/1.9 gem installation overlaps. Wonderful.

The remaining issue for me, though, is the lack of generic/default Ruby programs. That’s right, there’s no more ruby, irb, or ri. Version numbers need to be appended to run any of the main Ruby programs. I’d assumed that the idea behind this was to banish the idea of “generic” Ruby and force the developer/administrator to choose their flavor.

It was quite surprising, then, to find that both the rubygems1.8 and rubygems1.9.1 packages claim ownership of /usr/bin/gem. Well, not exactly ownership: the RubyGems packages use Debian’s “alternatives” system to let us choose the preferred default RubyGems version. So why not use the alternatives system for Ruby? Who knows.

The Fix

# Assuming the following have been installed:
sudo aptitude install ruby-full rubygems1.8 \
                      ruby1.9.1-full rubygems1.9.1

# Ruby 1.9.1 alternatives
sudo update-alternatives \
  --install /usr/bin/ruby ruby /usr/bin/ruby1.9.1 170 \
  --slave   /usr/bin/erb  erb  /usr/bin/erb1.9.1 \
  --slave   /usr/bin/irb  irb  /usr/bin/irb1.9.1 \
  --slave   /usr/bin/rdoc rdoc /usr/bin/rdoc1.9.1 \
  --slave   /usr/bin/ri   ri   /usr/bin/ri1.9.1

# Ruby 1.8 alternatives
sudo update-alternatives \
  --install /usr/bin/ruby ruby /usr/bin/ruby1.8 180 \
  --slave   /usr/bin/erb  erb  /usr/bin/erb1.8 \
  --slave   /usr/bin/irb  irb  /usr/bin/irb1.8 \
  --slave   /usr/bin/rdoc rdoc /usr/bin/rdoc1.8 \
  --slave   /usr/bin/ri   ri   /usr/bin/ri1.8

These commands install alternative choices for ruby, erb, irb, rdoc, and ri; 1.8 versions have a higher priority, 180, over the 1.9.1 versions, 170. (These priority numbers match up with the rubygems* packages). Now, I just need to choose which version of Ruby is the default:

# Probably want to choose the same version for both!
sudo update-alternatives --config ruby
sudo update-alternatives --config gem

Postscript

Of course, what would all of that hard work be without committing the changes to my /etc Git repository:

# See, the real tip is all the way down here!
# Keep /etc under Git and worry a little less
# about configuration changes
cd /etc
sudo git add alternatives/
sudo git commit -m 'Ruby 1.8 and 1.9.1 alternatives added'