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
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):
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
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
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
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
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?