Kevin Trowbridge

Software Developer. Husband. I ❤ Webpages.

Migrate to Rails 3 and Ruby 1.9, While Still Working on Rails 2, Ruby 1.8 Projects

| Comments

I’ve been hearing about Rails 3 for quite a while now. I thought it was about time to install it and take it for a drive. And I wanted to upgrade to Ruby 1.9 too, while I was at it.

But I’ve got several projects that still need Ruby 1.8.6 to run. So I discovered a tool called Ruby version manager (RVM) that allows me to install many versions of ruby and switch between them (the tool works by storing different versions of ruby and gems in specific directories, and juggling the environment’s paths).

Rvm works well and greatly simplifies the pain of working with projects written in Rails 3 and Rails 2 on the same machine.

Installing Ruby 1.9

Rails 3 can use either Ruby 1.8.7 or 1.9. It’s probably time to start switching to Ruby 1.9, correct? But there is a problem: I have (and you probably do too) bunches of projects that I do not want to (or maybe can’t) migrate to the new version of Ruby, or at least certainly not today. And so how to install and manage two versions of Ruby?

RVM is a command line tool which allows you to easily install, manage and work with multiple ruby environments from interpreters to sets of gems.

I suggest you follow the installation instructions.

Once you have installed Rvm, use it to painlessly install Ruby 1.9:

Note: I initially tried to install Ruby 1.9.2, but I ran into some wicked bugs when I tried to boot Rails. These issues are documented here in this Ruby language bug report. There, folks recommended upgrading to the ruby-head version. So I did that, and it resolved the issues that I had.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
macbook:~$ rvm install ruby-head
Installing Ruby from source to: /Users/kmtrowbr/.rvm/rubies/ruby-head
Updating ruby from http://svn.ruby-lang.org/repos/ruby/trunk
Configuring ruby-head, this may take a while depending on your cpu(s)...
Compiling ruby-head, this may take a while, depending on your cpu(s)...
Installing ruby-head
Installation of ruby-head is complete.
Updating rubygems for /Users/kmtrowbr/.rvm/gems/ruby-head@global
Updating rubygems for /Users/kmtrowbr/.rvm/gems/ruby-head
Installing gems for ruby-head.
Installing rdoc to /Users/kmtrowbr/.rvm/gems/ruby-head@global
Installing rdoc to /Users/kmtrowbr/.rvm/gems/ruby-head
Installing rake to /Users/kmtrowbr/.rvm/gems/ruby-head@global
Installing rake to /Users/kmtrowbr/.rvm/gems/ruby-head
Installation of gems for ruby-head is complete.

Rvm works on a per-user-basis by playing with the shell paths. It places ruby into a hidden .rvm directory in your home directory and adds and removes directories from the path to set you up with different versions of ruby.

So—aspiring to develop with Rails 3 and Ruby 1.9 by default in the future, I set rvm to load Ruby 1.9 by default when I open a shell (by issuing another rvm command):

1
macbook:~$ rvm --default ruby-head

Close and reopen your shell so as to reload your ‘default’ ruby version, and then check to see that it is indeed the 1.9 version:

1
2
3
macbook:~$ ruby -v
ruby 1.9.3dev (2010-05-18 trunk 27869) [x86_64-darwin10.3.0]
Installing Rails 3

Now we’ve got Ruby 1.9 installed, so let’s install Rails. Rails comes as a bunch of gems, so installing it is a matter of updating your rubygem program and then installing those gems.

How does Rvm work with gems? Well, it installs them into ruby-version-specific directories in its hidden .rvm directories. Even rubygems is versioned. Here you can see how when I check the path of the Ruby executables when I have the ruby-head version selected with Rvm, it’s off in a strange special Rvm controlled place:

1
2
3
4
5
6
7
8
macbook:~$ which ruby
/Users/kmtrowbr/.rvm/rubies/ruby-head/bin/ruby
macbook:~$ which gem
/Users/kmtrowbr/.rvm/rubies/ruby-head/bin/gem
macbook:~$ env | grep ruby
GEM_HOME=/Users/kmtrowbr/.rvm/gems/ruby-head
MY_RUBY_HOME=/Users/kmtrowbr/.rvm/rubies/ruby-head
RUBY_VERSION=ruby-head

So basically this means that once you are using Rvm you’ll probably want to use it to install Ruby gems as well. In the case of installing Rails, we want to install rails into the RVM environment that goes with rails 1.9. Rvm has documentation around this.

The basic principle is to not use sudo to install gems. Since Rvm works by isolating code in your ~/.rvm (user level) directory, it’s a waste (and will probably screw things up) to use sudo gem install to install the gems in a system level directory.

The second principle is to run rubygems through rvm. Rvm will invoke the version of rubygems that is associated with that version of Ruby, and install the gems in the proper path.

So in the following sequence of commands I update rubygems to the most current version, check to see which version that is, and then install the new rails:

Note how for some reason you have to install a bunch of prerequisite gems before installing the Rails 3 gem. I’m not sure why this is but these instructions come from DHH so – there must be a good reason!

1
2
3
4
5
6
7
8
9
10
11
12
13
macbook:~$ rvm ruby-head gem update --system
Updating RubyGems
Nothing to update

macbook:~$ rvm ruby-head gem -v
1.3.7

macbook:~$ rvm ruby-head gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
macbook:~$ rvm ruby-head gem install rails --prerelease
ruby-head: ruby 1.9.3dev (2010-05-18 trunk 27869) [x86_64-darwin10.3.0]
installing rails ...
rails  installed, output logged to:
/Users/kmtrowbr/.rvm/log/ruby-head/gem.install.log

Switching between Rubies

So there you have it folks. If you need to use Ruby 1.8.6 to work on that old Rails 2.3 project, just tell rvm to use that as the version of ruby for that console session:

1
2
3
4
5
6
7
8
macbook:~$ rvm system
macbook:~$ ruby -v
ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
Once you've finished, you can switch back to Ruby 1.9:

macbook:~$ rvm default
macbook:~$ ruby -v
ruby 1.9.3dev (2010-05-18 trunk 27869) [x86_64-darwin10.3.0]

I know that most Rails developers are going to run into this situation as they straddle Rails 2 and 3 moving forward, and if there is a better way of switching back and forth between Ruby 1.8.6 and 1.9, I have yet to hear of it!

What’s your take on this?

software

Related

Comments