My favorite Linux server distribution is Debian and I use it whenever possible. Its sane layout and APT system make working with it easy and fun. I've also been doing quite a bit of Ruby on Rails coding recently and I have grown to love the way it works and how easy it is to get massive amounts of work done. The problems start when you try to use Ruby on Debian. Sure, Debian tries to make things super easy to install and upgrade by using the APT package manager. However, the Ruby community insist on using Gem as its package manager and the two don't work and play well together.

I recently wanted to try out the latest Ruby and found that while you can install both Ruby 1.8 and Ruby 1.9.1 they don't use the Alternatives system built-in to Debian. This means that the Ruby 1.8 executable is "hard-coded" into the system and even installing Ruby 1.9.1 doesn't change the default version that runs.

One solution is to go through the system looking for links to the 1.8 stuff and change it to point to 1.9.1. To do this manually is tedious, error prone, and not reproducible; wouldn't it be great if there was a way to make all these links automatically? Enter the Debian alternatives system. Thanks to a post by David Lee I had a starting point to get my system working the way I wanted it to. His post was for Ubuntu and specifically targeted Ruby 1.9 so I've updated it a bit and laid out the steps below for your convenience.

  1. Install the latest versions for Ruby1.8 and Ruby1.9.1
    sudo aptitude install -s ruby1.8  riby1.9.1 irb1.8 irb1.9.1 ri1.8 ri1.9.1

  2. Next we're going to configure the Alternatives system. Create a new file on your system and copy this code into it.
    # Filename: ruby-alternatives.sh
    # install ruby1.8 & friends with priority 500 and make them the default
    update-alternatives --install /usr/bin/ruby ruby /usr/bin/ruby1.8 500 \
    --slave /usr/share/man/man1/ruby.1.gz ruby.1.gz \
    /usr/share/man/man1/ruby.1.8.gz \
    --slave /usr/bin/ri ri /usr/bin/ri1.8 \
    --slave /usr/bin/irb irb /usr/bin/irb1.8

    # install ruby1.9.1 & friends with priority 400
    update-alternatives --install /usr/bin/ruby ruby /usr/bin/ruby1.9.1 400 \
    --slave /usr/share/man/man1/ruby.1.gz ruby.1.gz \
    /usr/share/man/man1/ruby.1.9.1.1.gz \
    --slave /usr/bin/ri ri /usr/bin/ri1.9.1 \
    --slave /usr/bin/irb irb /usr/bin/irb1.9.1

  3. Now execute the script you just created to install your new configuration. You should see some messages go by but don't worry if it complains a little bit about the old 1.8 stuff not being there.
    sh ruby-alternatives.sh

  4. You are now ready to alternate between Ruby installations on your Debian box. Just run the command below whenever you want to switch between 1.8 and 1.9.1.
    update-alternatives --config ruby
NOTE: These commands worked on my Debian Squeeze install. Your mileage may vary.