blog.mhartl | Michael Hartl's tech blog

2010-02-19

Some rvm gotchas

Filed under: Uncategorized — mhartl @ 12:59

Like many in Rails-land, I started playing with the Rails 3 beta as soon as it came out. And, like many, I discovered to my chagrin that (a) Rails 3 doesn’t play nice with Rails 2.3 and (b) it doesn’t uninstall cleanly. (I ended up manually removing all 3.0.0.beta gems by hand.) If you ask around a bit, you’ll quickly discover a solution to this problem (if you’re running a Mac), which is to run multiple Ruby versions and then install different versions of Rails on different Rubies. This works great in principle, but I ran into a few gotchas in the actual implementation, so I thought I’d share them for the benefit of Google searchers everywhere.

In order to manage the feat of multiple Rubies, the amazing Ruby Version Manager (rvm) by Wayne Seguin is simply essential. Here are the steps I followed:

After installing rvm, I first tried to install the latest version of Ruby 1.8.7, which works with both Rails 2 and Rails 3:

$ rvm install 1.8.7

At this point I got a readline error, but this was covered by the rvm troubleshooting guide:

$ rvm remove 1.8.7; rvm install readline

Unfortunately, the installation still fails to build a makefile:

$ rvm install 1.8.7
.
.
.
FAIL
$ less ~/.rvm/log/ruby-1.8.7-p249/make.error.log

[2010-02-12 08:54:30] make
make: *** No targets specified and no makefile found.  Stop.

After searching around for a while, I finally found a page (location already forgotten) with the fix, which involves including an explicit path to the readline dependency:

$ rvm remove 1.8.7
$ rvm install 1.8.7 -C --with-readline-dir=/Users/mhartl/.rvm/usr

This still didn’t work, though; on my system (OS X 10.5 Leopard) I got a segfault, which shows up when installing certain gems:

$ gem install metric_fu

/Users/mhartl/.rvm/rubies/ruby-1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/
spec_fetcher.rb:245: [BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin9.8.0]

Abort trap

Luckily, I have a working Ruby 1.8.7 in /usr/local/bin, so I could check the patchlevel: p174. Since rvm installs the latest version by default (1.8.7-p249 as of this writing), I had to indicate the patchlevel explicitly:

$ rvm remove 1.8.7
$ rvm install 1.8.7-p174 -C --with-readline-dir=/Users/mhartl/.rvm/usr

(Note that this also requires the explicit readline path.)

Finally, I had a working Ruby, but I wanted to set the rvm Ruby as the default, and my system wouldn’t remember it:

$ rvm 1.8.7-p174 --default
$ which ruby
/Users/mhartl/.rvm/rubies/ruby-1.8.7-p174/bin/ruby
new shell...
$ which ruby
/usr/local/bin/ruby

The solution was to remove the explicit /usr/local/bin path from my .bash_profile file:

before:
  export PATH="/usr/local/bin:$HOME/bin:/usr/local/mysql/bin:$PATH"
  if [[ -s /Users/mhartl/.rvm/scripts/rvm ]] ; then source /Users/mhartl/.rvm/scripts/rvm ; fi

after:
  export PATH="$HOME/bin:/usr/local/mysql/bin:$PATH"
  if [[ -s /Users/mhartl/.rvm/scripts/rvm ]] ; then source /Users/mhartl/.rvm/scripts/rvm ; fi

Wayne Seguin assures me that this step should be unnecessary as long as the PATH appears before the rvm line, but this is the only way I could get it to work on my system. (Since /usr/local/bin is on the system path, all the executables there still run; I’m not sure why I ever included it in .bash_profile in the first place.)

Finally, we’re ready for the raison d’ĂȘtre of all this work: multiple Rubies, multiple Rails. Here’s how to get it working (note the lack of sudo in the gem installations):

$ rvm use 1.8.7
$ gem install rails -v 2.3.5
$ rvm install 1.9.2
$ rvm use 1.9.2
$ gem install rails --pre

Voilà! Now switching between Rails versions is as easy as

$ rvm use 1.8.7

and

$ rvm use 1.9.2

2009-04-28

New RSS feed

Filed under: Uncategorized — mhartl @ 08:22

This blog’s RSS feed has changed; please re-subscribe here: http://feeds2.feedburner.com/mhartl. (It might take an hour or two to go live. If it doesn’t work for you now, come back in a bit and try again.)

Blog at WordPress.com.