The other day, Adam (one of Cantina’s founding fathers) sent out an email recommending that we check out RVM if we’re dealing with multiple versions of Ruby. Although I primarily use one version of Ruby (old faithful, 1.8.7), I use multiple versions of Rails – you know, since I love Rails 3 but am still tethered to Rails 2. I thought I’d give it a try to see if it could help out.
It did.
In fact, it worked so well that I immediately installed RVM on all of my machines. In this post, I’ll go over how I installed RVM on my MacBook Pro running the latest OS X Snow Leopard (10.6.4). This should work on any Unix-based system, though.
First things first, you gotta install RVM. Open up Terminal.app and run the following:
$> bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Then, open up your .profile file
$> vi ~/.profile
and append the following line to the very bottom:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Save the file and exit. Now, back in Terminal, reload your profile, and test to make sure RVM is installed correctly:
$> source ~/.profile
$> type rvm | head -n1
You should see “rvm is a function”. If not, you screwed up. Go directly to jail. Do not pass Go, do not collect $200.
So, RVM is all working – but there’s one more step. To make sure RVM has all the required dependencies for your system, run
$> rvm notes
Now, before we can get rolling in the fun, you might need to install “readline”. If you have it installed, locate it and skip ahead to the next section. I got my copy from ftp://ftp.cwru.edu/pub/bash/readline-6.1.tar.gz. Once downloaded, you can un-tar it, and run
$> ./configure
$> make
$> sudo make install
Awesome. Now that everything is good to go, I’ll show you how I set up my Ruby/Rails versions. First, it’s worth noting that RVM has no rubies installed by default. So, let’s get cracking. We’ll install Ruby 1.8.7, 1.9.2 (now that it’s released), and Ruby Enterprise Edition (why not).
$> rvm install 1.8.7 -C --enable-shared,--with-readline-dir=/usr/local
$> rvm install 1.9.2 -C --enable-shared,--with-readline-dir=/usr/local
$> rvm install ree -C --enable-shared,--with-readline-dir=/usr/local
Side note: The –enable-shared piece just enables shared library linking. The –with-readline-dir parameter points to where “readline” was installed – you’ll need to change the path if you installed it elsewhere.
At this point, you have 3 versions of Ruby installed, all completely separate and not interfering with each other. Now let’s get Rails installed.
These examples will show how to install Rails using Ruby 1.8.7, but the same flow works exactly the same for the other versions of Ruby (except, of course, substituting “1.9.2″ or “ree” in for “1.8.7″).
First, we’ll select the 1.8.7 Ruby, and create a “gemset” within it. Basically, this creates another partition within a Ruby version for different sets of gems. So, for example, you can install the Rails 2 gem and the Rails 3 gem without any conflict. In fact, that’s what we’re going to do right now.
$> rvm use 1.8.7
$> rvm gemset create rails2
$> rvm gemset create rails3
$> rvm use 1.8.7@rails2
$> gem install rails
$> rvm use 1.8.7@rails3
$> gem install rails --pre
Note that I’m not doing “sudo gem” here. The rules for this are as follows: if you use sudo to install the first gems here, you should always use it. Otherwise, you don’t need to use it. Since I think saving a few extra keystrokes a day is a good idea, I opt to omit.
Now we have both Rails 2 and Rails 3 installed using Ruby 1.8.7. All this with only a few lines in the terminal. To switch between the two versions, simply run either of the following commands:
$> rvm use 1.8.7@rails2
$> rvm use 1.8.7@rails3
Rince, wash, and repeat for each version of Ruby you’ve installed. Now, to set one as the default Ruby/Rails combo, just enter:
$> rvm --default 1.8.7@rails2
Done and done. I hope using RVM saves you as much time as it already has saved me.
I’m glad I could have this affect on you.