Switching between Rails 2 and Rails 3 on Mac OS X or Ubuntu with RVM

I’m migrating AdGrok to Rails 3 this weekend–what with the new ActiveRecord query functionality, and MongoMapper, and all our gems migrating to Rails 3, it seems like it’s time.

The Railscast (and ASCIIcast) is great, but a couple steps are now outdated. There also aren’t any instructions on how to switch between rails 2 and rails 3 gracefully.

Install RVM

The Ruby Version Manager (rvm) makes it easy to switch between versions of ruby as well as sets of gems. When you upgrade your rails app from 2.x to 3.x, this can be really handy.

One thing to note: RVM places all files in ~/.rvm. Run all these commands as you. Don’t use sudo with rvm or gem, (unless you’re doing a system-wide RVM installation, of course).

If you’re on Ubuntu, you’ll need curl, git, build-essentials, and a bunch of develpoment libraries before you can continue:

sudo apt-get install curl git-core build-essential libreadline-dev zlib1g-dev libssl-dev libxslt1-dev

If you’re using MySQL, add libmysqlclient-dev to that list.

To install RVM, run this in your terminal (as per the instuctions):

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Then add this to the end of your ~/.bashrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

And open a new shell. Test that rvm is installed by running rvm -v.

Install ruby 1.9.2

rvm install 1.9.2

(pretty slick, eh?) and make it the default ruby:

rvm 1.9.2 --default

Test that you’re on the right ruby: ruby -v

Install Rails 3

rvm gem install rails

Boom! You’re done! Test with rails -v.

Install Rails 2

If you want to run rails 2.x for one app in one shell and rails 3 in another, it’s pretty easy:

rvm install 1.8.7
rvm use 1.8.7
rvm gem install -v=2.3.8 rails

and presto, you’ve got a 2.3.8 environment:

$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
$ rails -v
Rails 2.3.8

And switch back to rails 3:

$ rvm use 1.9.2
Using /Users/mrm/.rvm/gems/ruby-1.9.2-p0
$ rails -v
Rails 3.0.1
$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

If you’re upgrading from macports…

If you’re starting from scratch, the preceding steps should work. I was upgrading from rails 2.3.x and ruby 1.8.x installed via MacPorts, however, and saw them not play nicely together.

In switching to rvm, I ended up completely nuking my MacPorts installation (as per the instructions) — I ended up only really needing wget and dsh, and those reinstalled simply), because I had an /opt/local/bin/rails that wasn’t owned by any installed port (who knows how it got there), and tons of old gems that I didn’t want to pull in accidentally.

For the benefit of the googles, here’s the errors I saw when the rvm ruby superceded the macports ruby:

$ rails
-bash: /opt/local/bin/rails: /opt/local/bin/ruby: bad interpreter: No such file or directory

And /usr/bin/rails (again, I don’t know where that one came from–Mac OS X?), barfed thusly:

$ rails
/Library/Ruby/Site/1.8/rubygems.rb:779:in `report_activate_error': Could not find RubyGem rails (>= 0) (Gem::LoadError)
	from /Library/Ruby/Site/1.8/rubygems.rb:214:in `activate'
	from /Library/Ruby/Site/1.8/rubygems.rb:1082:in `gem'
	from /usr/bin/rails:18

After I installed rvm properly (so the ~/.rvm paths are before system paths), and ran rvm gem install rails, everything worked.

If you’re installing on Ubuntu…

and you see this:

Error running './configure --prefix=/usr/local/rvm/rubies/ruby-1.8.7-p302 --enable-shared  ', please check /usr/local/rvm/log/ruby-1.8.7-p302/configure.error.log
There has been an error while running configure. Halting the installation.

and configure.error.log complains about “configure: error: no acceptable C compiler found in $PATH” — you need to install the “build-essential” package:

sudo apt-get install build-essential

If you run “rails console”, and see … lib/ruby/1.8/irb/completion.rb:10:in `require': no such file to load -- readline (LoadError) — you need to

sudo apt-get install libreadline-dev
rvm remove 1.8.7
rvm install 1.8.7

Related posts:

  1. Creating acts_as_… gems for Rails 3.1.x
    Update January 2012: since originally writing this post, I’ve recanted my love for rails plugins — having a stubbed rails app in your plugin’s test directory is just too funky.......
  2. Installing Phusion Passenger on Ruby 1.9.1, Nginx, & Ubuntu 10.04
    I wrote this a while back, before I knew about RVM. The Ruby Version Manager has a bunch of features that makes it better than macports for ruby and gem......
  3. Quick MacPorts Installation of Ruby on Rails
    October 2010 update: I wrote this a while back, before I knew about RVM. The Ruby Version Manager has a bunch of features that makes it better than macports for......
  4. HOWTO: Make system-wide RVM installations work with cron, monit, delayed_job, and passenger
    System-wide RVM installations are recommended for production deployments, but because /usr/bin/ruby is no longer the “correct” ruby, you need to tell all your moving parts about RVM. Before installing ruby......

  • Abdul Barek

    Fantastic! it saves me a couple of days. Thanks for GREAT sharing.

  • FrankTheTank

    Thanks for this well guided installation. Unfortunately, “ruby -v” as well as “rails -v” won’t work for me.
    I receive a “….’ruby’ is currently not installed from apt”. But it works when enter “rvm ruby -v”. Additionally this won’t work for rails. What went wrong? ….any suggestions?

  • Anonymous

    RVM has great docs, and they have an IRC channel that’s really active — I got help almost immediately a while back from the author and a couple other people.

    Because you said “apt”, you must be on ubuntu or debian. RVM will be installing ruby for you, so you don’t need to `sudo apt-get install ruby##` — follow these instructions: https://rvm.beginrescueend.com/rvm/install/ — and make sure you do the “Load RVM into your shell sessions as a function” step. Then run `rvm install 1.9.2` (or 1.8.7) and `ruby -v` should work then.

  • FrankTheTank

    I did the installation completely with “rvm” but as written above I have to check my ruby and rails versions. Therefore i want to enter “ruby -v” and “rails -v” and here I receive the message from bash, that ruby as well as rails is not installed.
    I am able to track all ruby and gem stuff under home/name/.rvm/gems/…” -> therefore it is installed correctly inside RVM.
    In my honest opinion my RVM don’t set environment variables right. …or i have to do another step beneath the ones that are listed above.Well, i think you are right and I will check the corresponding IRC channel

  • FrankTheTank

    alright……I have edited “.bash_profile” in contrast to “.bash_src” and loaded a new shell….well well….it works. Thanks guys!!!!

  • Aarthiraj561

    i have followed the same process.. but when i say
    “rvm use 1.9.2″
    it uses ruby 1.9.2 but the rails version is still 2.3.8

    please help me with this