A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Friday, April 24, 2009

Installing Ruby 1.9 and gems on a machine with Ruby 1.8

I've run into problems in the past with multiple versions of Ruby on one machine, such as not finding gems, etc. Here are the steps I took to install Ruby 1.9.1 from source on a machine that already had Ruby 1.8.6 installed. Specifically this was on a Fedora 8 machine that had Ruby 1.8 installed from the ruby-devel rpm, which places it in /usr/lib.

To stir things up a bit more, some gems are not yet compatible with Ruby 1.9 (This post is written in April 2009). I'll address those later on. Hopefully those problems will go away over the next few months.

1: Capture a list of the Ruby gems that you have installed on your machine.
We'll use this later to reinstall the gems under 1.9.
# gem list --local --no-versions > gem_list

2: Fetch the Ruby 1.9 source
Download from ruby-lang.org into a staging directory. Pick their recommended version.
# wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz

3: Compile and install
The defaults will install it in /usr/local/lib/ruby. If you already have an existing ruby installed there then you might want move it out of the way.
# ./configure
# make
# make install

4: Check the new version
Make sure that you have /usr/local/bin at the start of your PATH. This line in your .bashrc file will do that.
export PATH=/usr/local/bin:$PATH

Check the version of Ruby:
# which ruby
# ruby -v
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]

5: Download rubygems
Use wget rather than curl if you are doing this on the command line as wget handles the redirects that the rubyforge download links use. Choose the latest stable version.
wget http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz

Unpack it, cd to the directory and install it.
# ruby setup.rb
# gem -v
# which gem

This creates /usr/local/lib/ruby/gems/.

6: Download and install a bunch of gems
Use the list of previously installed gems as your guide. I've written a script that will take care of most of the work for you: install_gems.rb. Comment out any gems in your list that you no longer need. More complex gems, such as Rails, will install other gems that they need, so don't worry if you comment out any gems that you don't recognize.

We will exclude the mysql gem from this automated process as it needs a special option. So comment that one out in your list for now.

Run the script (as root or sudo) and watch it do its thing:
# ./install_gems.rb gem_list
Installing actionmailer
Skipping actionpack
Installing actionwebservice

Don't worry when it skips certain gems. That just means that another gem has already installed them. You may see errors with some gems - don't freak out yet. These may well be related to Ruby 1.9. Capture the output in a file for easier troubleshooting. You can safely run install_gems.rb multiple times. Eventually it should skip over all the gems.

7: Manually install the mysql gem
If you want to use MySQL with Rails then install with an explicit path to mysql-config.
# which mysql_config
# gem install mysql -- --with-mysql-config=/usr/bin/mysql_config

Note that you may get installation errors.

8: Ruby 1.9 and Gem Install Errors
By the time you try your installation all your gems may install just fine. But as of now (April 2009) several gems are not compatible with 1.9. The ones that gave me errors were:

Patching gem source code is a pain, so before you start down that path, decide if you really need these gems anymore. I've moved from mongrel to thin, so I can live without that one. I don't need to profile my apps right now so I can skip rubyprof. In fact the only one that I really need is mysql.

Time to poke around on the web and see how other people are dealing with the same problem. For Mysql you can edit the gem code or pull down a patched version or just give it a couple of weeks and see if the gem has been updated. It can be frustrating but there you go.

9: Clean up after yourself
Once you have verified that your new version of Ruby is working the way you expect then you might want to rename the old installation directories. I wouldn't delete anything but renaming them should ensure that you don't accidentally use the old version due an incorrect path, etc.

I hope this helps guide you through the Ruby install process and avoids the problems that can arise with two or more versions of Ruby on your machine.


1 comment:

mikof said...

Just tried. after removing the old Ruby -- there were no errors but when trying to install gems I get:

~# gem install actionwebservice
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/package.rb:10:in `require': no such file to load -- zlib (LoadError)
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/package.rb:10:in `top (required)'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/format.rb:9:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/format.rb:9:in `top (required)'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:11:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:11:in `top (required)'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/dependency_installer.rb:3:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/dependency_installer.rb:3:in `top (required)'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/commands/install_command.rb:4:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/commands/install_command.rb:4:in `top (required)'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:167:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:167:in `rescue in load_and_instantiate'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:159:in `load_and_instantiate'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:88:in `[]'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:144:in `find_command'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:131:in `process_args'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:102:in `run'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/gem_runner.rb:58:in `run'
from /usr/local/bin/gem:21:in `main'

Any idea? can it be that ruby came without Zlib?

Archive of Tips