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 --defaultTest that you’re on the right ruby: ruby -v
Install Rails 3
rvm gem install railsBoom! 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:
- 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....... - 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...... - 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...... - 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......