Supporting Multiple Ruby Versions on Ubuntu 10.04 Lucid

I’ve been trying to support multiple versions of Ruby on my Ubuntu 10.04 for a while now (it’s not been high priority so it took a while).

Ruby Version Manager didn’t work for me

At first I tried, RVM.  While it works for most people, I did not work for me.

I kept getting errors like:

   no such file to load -- zlib (LoadError)

I found out much later, you need to install zlib yourself and then uninstall/install Ruby.  See here and here.  You must also have to get several other libraries and compile ruby from a source – see here.

It all proved too hard for a Ubuntu newbie with limited time on my hands.

update-alternatives works well

I gave up for a while until to saw this post from alexn on how to use the Ubuntu’s update-alternatives command to manage Ruby versions.

It made perfect sense to me because update-alternatives is used to manage java versions – so why not Ruby.

I followed alexn’s instructions with a few modifications for Ubuntu.

1. Install Ruby from Ubuntu repositories
   sudo apt-get install ruby-full rubygems ruby1.9.1-full rubygems1.9.1

This will install the two ruby versions into separate directory structures.

For example, ruby version 1.8.7 will be installed as /usr/bin/ruby1.8 and ruby version 1.9.1 will be installed as /usr/bin/ruby1.9.1.

A symbolic link called /usr/bin/ruby is made to /usr/bin/ruby1.8.

2. Get some gems
   sudo gem install rack
   sudo gem1.9.1 install rack

This will install the rack gem and force the creation of the /var/lib/gems/1.8/bin and  /var/lib/gems/1.9.1/bin directories.

3. Remove gem option already registered in update-alternatives

For some reason, Ubuntu has gem as a selectable option within update-alternatives. We want gem to be dependent on Ruby so let’s remove it.

   sudo update-alternatives --remove-all gem
4. Register Ruby with update-alternatives

update-alternatives is a really nice a way to change symbolic links to a set of files.

Enter the following:

    sudo 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/ruby1.8.1.gz \
        --slave /usr/bin/ri ri /usr/bin/ri1.8 \
        --slave /usr/bin/irb irb /usr/bin/irb1.8 \
        --slave /usr/bin/gem gem /usr/bin/gem1.8 \
        --slave /var/lib/gems/bin gem-bin /var/lib/gems/1.8/bin

    sudo 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/ruby1.9.1.1.gz \
        --slave /usr/bin/ri ri /usr/bin/ri1.9.1 \
        --slave /usr/bin/irb irb /usr/bin/irb1.9.1 \
        --slave /usr/bin/gem gem /usr/bin/gem1.9.1 \
        --slave /var/lib/gems/bin gem-bin /var/lib/gems/1.9.1/bin

The parameters for update-alternative are:

  • –install tells update-alternative to link /usr/bin/ruby to /usr/bin/ruby1.8 for alternative 1 or /usr/bin/ruby1.9.1 for alternative 2 . The name of the option is called “ruby”.  The 500 and 400 are the priority of the options- the option with the highest priority will be assigned as the default.
  • –slave tells update-alternative to link these other files when this version of “ruby” is selected. As you can see, we will link man for ruby, ri, irb, gem and the gem/bin directory (so that we can keep our gems separate for each ruby version).
5. Change versions

To switch between Ruby version:

   sudo update-alternatives --config ruby

You will see the following menu to allow you to select your option:

5. Installing gems

You can now install gems in the normal way.

   sudo gem install sproutcore

Note that the gem will only be installed in the currently active Ruby version.  gem will install the gem files to /var/lib/gems which is a symbolic link to the currently active Ruby version.

If you wish to install a gem to multiple Ruby  versions, you will need to switch versions and run gem install for each version.

Other notes on update-alternatives

To get a list of all programs that are registered with update-alternatives, enter

   sudo update-alternatives --get-selections

It is good to check this once in a while to make sure that the programs you are running is what you expect.

To get more information on update-alternatives, enter

   man update-alternatives
Leave a comment

3 Comments.

  1. Hi,

    Thanks for sharing this. Great post

  2. Thanks for sharing. It is very useful.

Leave a Reply


[ Ctrl + Enter ]

Trackbacks and Pingbacks: