Install Multiple Versions of Ruby on Leopard
I'm glad that Apple bundled Ruby (1.8.6) with Leopard, but I need 1.8.5 installed to ensure compatibility with some projects that are deployed on servers running 1.8.5. Not only do I need 1.8.5, but I need it installed at different patch levels. Here is how I set up OSX to have multiple versions of Ruby installed.
Download
The first step is to download the versions of Ruby you want to install from ftp://ftp.ruby-lang.org/pub/ruby/1.8/. The latest 1.8.5 release is ruby-1.8.5-p114.tar.gz, and may be sufficient unless you specifically want/need an older 1.8.5 release.
Install
Start by extracting the tarball you just downloaded:
tar zxvf ruby-1.8.5-p114.tar.gz
You can install Ruby in any directory you want to. I chose to put mine under /usr/local/ruby-<version>:
cd ./ruby-1.8.5-p114
./configure --prefix=/usr/local/ruby-1.8.5-p114
make
sudo make install
You can confirm the installation by checking the Ruby version:
/usr/local/ruby-1.8.5-p114/bin/ruby --version
symlink for default Ruby version
With multiple versions of Ruby, you'll want an easy way to switch between them. One way to accomplish this is to create a symlink to the "current" version:
sudo ln -s /usr/local/ruby-1.8.5-p114 /usr/local/ruby
To switch to a different version by default, all you have to do is update the /usr/local/ruby symlink. You can verify the version of Ruby the symlink is linking to by trying:
/usr/local/ruby/bin/ruby --version
symlink Leopard's version
I also created a symlink for 1.8.6 to the default Leopard install:
sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr /usr/local/ruby-1.8.6
/usr/bin executables
Leopard has Ruby-related executables in /usr/bin symlinked to the executables from the pre-bundled install. We need to change those to be able to toggle the Ruby version. There are a couple ways this can be accomplished. It should be done for all Ruby executables (cap, erb, gem, irb, rake, rdoc, ri, and ruby).
One option is to remove those symlinks altogether, and then add /usr/local/ruby/bin to the $PATH. That could be done by simply removing them from /usr/bin:
sudo rm /usr/bin/ruby (for all the executables mentioned above)
And then adding this line to ~/.bash_login:
export PATH="$PATH:/usr/local/ruby/bin"
The second option is to change the symlinks to point to the executables in /usr/local/ruby/bin. This isn't as favorable because any executable that is installed from a gem will be placed in /usr/local/ruby/bin. Unless you have that directory in your path (as mentioned with the previous option), you'll need a symlink for each executable. I recommend going with the previous option for that reason. However, you still may want to symlink the primary executables (namely, the core ones mentioned above) so that if your shell login script gets messed up, ruby, irb, gem, etc. will still be available.
alias
You may want to set up some aliases to make using and switching between Ruby versions simpler. These could go in ~/.bash_login. Here are some examples:
For executing a specific ruby, gem, or rake:
alias ruby186=/usr/local/ruby-1.8.6/bin/ruby
alias rake186=/usr/local/ruby-1.8.6/bin/rake
alias gem186=/usr/local/ruby-1.8.6/bin/gem
alias ruby185=/usr/local/ruby-1.8.5-p114/bin/ruby
alias rake185=/usr/local/ruby-1.8.5-p114/bin/rake
alias gem185=/usr/local/ruby-1.8.5-p114/bin/gem
For switching the default symlink:
alias use_ruby_186='sudo ln -fhs /usr/local/ruby-1.8.6 /usr/local/ruby'
alias use_ruby_185='sudo ln -fhs /usr/local/ruby-1.8.5-p114 /usr/local/ruby'
Rubygems
The last thing to do is to install Rubygems. Each Ruby installation will have its own copy of gems. You can download the latest version of of Rubygems from the Rubyforge project page. After downloading, extract the archive, switch your Ruby version, and run setup.rb:
tar zxvf rubygems-0.9.4.tgz
cd rubygems-0.9.4
use_ruby_185 (from alias)
sudo ruby setup.rb
Now you'll be able to install gems as normal using the gem command. Make sure you followed the steps with the /usr/bin executables so gem isn't always pointing to the 1.8.6 installation.
This should be everything you need to do to have multiple versions of Ruby available. I'll be writing an entry soon on how to take advantage of multiple installations to run test suites.
Posted on 2007-11-15 | permalink | del.icio.us
Blog Archive
