The major benefit of using rvm is for organization. When you start to have multiple projects that span different versions of Ruby, Rails, gems, etc.; you’ll need a way to separate your gems in a clear way. You also get the benefit of organizing the gems you use for your projects, say you are working with Rails in one project and Sinatra in another.
You can find setup guides here for OSX and here for Ubuntu.
First lets install the latest Ruby [Ruby 1.9.2-p180] for rvm.
rvm list
# will show you all Rubies installed in your current system, we have nothing because nothing has been installed
rvm list known
# will show you all Rubies that are known to rvm
rvm install ruby-1.9.2-p180
# this will install Ruby 1.9.2 p180
rvm list
# see we installed Ruby 1.9.2 correctly
Now the fun part. Lets see what Ruby we are using.
which ruby && ruby -v
# returns your system ruby
# /usr/local/bin/ruby
# ruby 1.9.2p180 (....) [x86_64-darwin10.6.0]
Lets use rvm Rubies.
rvm use ruby-1.9.2
# returns
# Using /User/[username]/.rvm/gems/ruby-1.9.2-p180
which ruby && ruby -v
# returns rvm’s ruby
# /Users/[username]/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
# ruby 1.9.2p180 (....) [x86_64-darwin10.6.0]
Lets say we need to work on a legacy Ruby 1.8.7 app.
rvm install ruby-1.8.7
rvm list
# returns a list of rubies rvm has installed
Now I don’t like typing periods, dashes, and such so I give my Rubies aliases.
rvm alias create r187 ruby-1.8.7
rvm alias create r192 ruby-1.9.2
rvm alias list
# returns your list of aliases
# now some fun stuff lets switch rubies
rvm use r187
which ruby && ruby -v
# returns
# .../.rvm/rubies/ruby-1.8.7-p344/bin/ruby
# ruby 1.8.7 ...
rvm use r192
which ruby && ruby -v
# returns
# .../.rvm/rubies/ruby-1.9.2-p180/bin/ruby
# ruby 1.9.2 ...
# Cool! Right. This will be better when I show you gemsets.
If you ever want to know what ruby you are using with rvm.
rvm current
If you want to use the system ruby.
rvm use system
# returns
# Now using system ruby.
# to see if that’s true
which ruby && ruby -v
# sweet!
Another useful rvm command for you to see more information is.
rvm info
# this gives in detail the information on the ruby you are using. useful when we use gemsets
Now RiversideRuby. Lets use Ruby1.9.2. Create a gemset for RiversideRuby, a fake new Rails 3.0.3 app, and a fake old legacy Rails 2.2.2 app.
rvm use r192
rvm gemset create riversideruby
rvm gemset create rails303project
rvm gemset create rails222project
RiversideRuby (RR) is a Sinatra app, lets go get the required gems.
rvm r192@riversideruby
gem install sinatra --no-ri --no-rdoc
gem install haml --no-ri --no-rdoc
rvm info
For our fake Rails 3.0.3 Project
rvm r192@rails303project
gem install rails -v 3.0.3 --no-ri --no-rdoc
gem list
For our fake legacy Rails 2.2.2 Project
rvm r192@rails222project
gem install rails -v 2.2.2 --no-ri --no-rdoc
gem list
See what we got.
rvm r192@riversideruby && rails -v
# oh it blew up, why because rails is not installed and it’s not suppose to be
rvm r192@rails303project && rails -v
# returns
# Rails 3.0.3
rvm r192@rails222project && rails -v
# returns
# Rails 2.2.2
Now we have multiple versions of Ruby installed, multiple versions of Rails, and multiple projects with under 1.9.2 with different gems all organized.
Now what if there was a way to make this all seamless and you didn’t have to switch back and forth. rvm r192@riversideruby, rvm r192@rails303project, etc.
Lets make these projects.
Make a Code Directory. Just a personal preference I place all my work under ~/Code
mkdir ~/Code && cd ~/Code
These are just place holders rather than running rails rails222project or rails rails303project
mkdir rails222project rails303project
Git the RR site.
git clone https://[username]@github.com/[username]/riversideruby.git
If I ls in my Code directory I should get.
ls
# returns
# rails222project rails303project riversideruby
Auto rvm with .rvmrc
echo “rvm r192@rails222project” > ~/Code/rails222project/.rvmrc
echo “rvm r192@rails303project” > ~/Code/rails303project/.rvmrc
echo “rvm r192@riversideruby” > ~/Code/riversideruby/.rvmrc
Now I should be able to do this.
cd ~/Code/rails222project && ruby -v ; rails -v
cd ~/Code/rails303project && ruby -v ; rails -v
cd ~/Code/riversideruby && ruby -v
# note allow the trusting of the .rvmrc files you just created
Now everytime you cd into a project path you are using the correct version of ruby and gemsets.
Read up on all things RVM here.
Now back to RiversideRuby.
cd ~/Code/riversideruby
ruby app.rb
Fin.