Dec 27, 2007

Multiple Versions of Ruby On Ubuntu

This post is from a time before RVM or rbenv you should check out those instead..

With Ruby 1.9 out there's the obvious possiblitity some people will want to run multiple versions of Ruby so I thought I'd share this.

My goal is to have the Ubuntu Ruby packages installed along side the most current releases of 1.8 and 1.9

First I'll install using apt to create my default ruby install.

$> sudo apt-get install ruby irb ri rdoc libruby-extras rubygems ruby1.8-dev
$> sudo gem install rake

Next I'll install the most current release packages of 1.8 and 1.9 in to /opt/ruby1.8.6 and /opt/ruby1.9.0 respectively.

Before doing that I'll make sure I have all the necessary build dependencies for both packages
$> sudo apt-get build-dep ruby1.8 ruby1.9

Next I downloaded both packages into a working directory and decompress them
$>mkdir temp; cd temp
$>tar -xvzf ruby-1.8.6-p111.tar.gz
$>tar -xvzf ruby-1.9.0-0.tar.gz

Next I build each of the packages.
$> cd ruby-1.8*
$> ./configure --prefix=/opt/ruby1.8 --program-suffix=1.8.6
$> sudo make && make install
$> cd ../ruby-1.9*
$> ./configure --prefix=/opt/ruby1.9 --program-suffix=1.9.0
$> sudo make && make install
Ruby 1.8 doesn't have built in support for gems like Ruby 1.9 so we'll have to install it.
$> wget
$> tar -xvzf rubygems-1.0.1.tgz
$> cd rubygems*
$> sudo /opt/ruby1.8.6/bin/ruby1.8.6 setup.rb
Notice that I specified the entire path to the ruby executable while installing RubyGems for 1.8.6

The next thing I do is create some symbolic links to make life a little bit easier.
$> sudo ln -s /opt/ruby1.8.6/bin/* /usr/local/bin
$> sudo ln -s /opt/ruby1.9.0/bin/* /usr/local/bin
Now test things a bit to make sure everything makes sense
$> which ruby
# /usr/bin/ruby
$> gem env
# ....
# GEM PATH: /var/lib/gems/1.8
# ....

$> which ruby1.8.6
# /usr/local/bin/ruby1.8.6
$> gem1.8.6 env
# ....
# GEM PATH: /opt/ruby1.8.6/lib/ruby/gems/1.8
# ....

$> which ruby1.9.0
# /usr/local/bin/ruby1.9.0
$> gem1.9.0 env
# ....
# GEM PATH: /opt/ruby1.9.0/lib/ruby/gems/1.8
# ....

A couple of things to watch out for:
  • Make sure you don't have any RubyGems environment variables set. They're not needed for any of this and most likley will mess something up.
  • Don't install any gems until after you create your symbolic links or the executable commands may clobber each other.
  • You have to specify the full path to an executable gem because they don't play nicely with program suffixes. So for example '/opt/ruby1.8.6/bin/rake' will do what you expect but just typing 'rake' may not.
One last little tidbit. I'm not sure if the Ubuntu RubyGems packages deal with fixing up the $PATH yet? Regardless the fix is easy; add this little bit of code to the bottom of your ~/.bashrc file
if [ -d /var/lib/gems/1.8/bin ]; then
export PATH


Adi said...

Thanks for these instructions. Works perfectly.

J Crockett said...

sorry about my over-excitement.. last night i made a fresh install from disc for ubuntu 7.10. after following your instructions, everything worked past the first call to 'gem env'.. however, now the call to 'which ruby' yields '/usr/local/bin/ruby' and rails 2 does not work, nor will install by 'sudo gem install rails'. i don't mean to bother you, but is this something that i should tinker with (ask a lot of questions) or something that i should just start over, from scratch, again? i guess that if there's a question, it's to ask your opinion for an inexperienced programmer in this regard -- are your instructions tenable to follow if all that is needed is ruby 1.8.6, or is it worthwhile in my situation to follow your instructions?

Mike Greenly said...

jcrocket: If all you need is Ruby 1.8.6 just remove any manually installed ones and if need be re-install the distribution packges. If your 'which ruby' says '/usr/local/*' it's finding a ruby that was previously installed from source but not to /opt like I described.

J Crockett said...

thank you. i had been unable to determine the glitch i found in or by way of your directory switches, at which point i found it most beneficent to junk the system and install from disk, re-allocate an old drive to root, and start fresh. i'll check back to this post in a year, i would guess, if programming ruby on rails becomes to something for my purposes. again, thank you, and best of the year.

jesse said...

will you help me ?

pastie 146552

Mike Greenly said...

Jesse: It looks to me like you had an existing ruby installed and it already had links in /usr/bin/local.

The other common problem is that you had environment variables for ruby gems set. if you have GEM_HOME set it will not put things where it should.

Focus on getting everything working through the full paths. Type /opt/ruby1.8.6/bin/ruby1.8.6 to make sure it works. Using that full path run the gem install etc...

Once all that's working symlink what ever you want into /usr/local

If your not using it "apt-get remove --pruge" all the system installed stuff.

Let me know if that helps any.

Jesse said...

You're the greatest. Thanks for being there, and so soon.

Until I can get that stuff squared in, I welcome you to collaborate here: