Skip to content

Working with Ruby Version Manager

November 24, 2011

You should first install the Ruby Version Manager (RVM), you can follow my other article on how to do it and get the latest version of Ruby and Rails working on your Mac.

Once you have RVM you can change Ruby versions at will. For a full documentation of RVM you can go the the website.

This article is in no way a full on manual but a set of quick tips on how to get going, and best practices. So we’ll see how to:

  1. Choose a Ruby version
  2. Gemsets and their basic functionality
  3. How to use gemsets with rails projects

Choose a Ruby Version

To select a working version of Ruby you can do


bigdiver$ rvm use 1.9.3
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0

Then you can verify that you’re really using that version by running


bigdiver$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]
bigdiver$ which ruby
/Users/bigdiver/.rvm/rubies/ruby-1.9.3-p0/bin/ruby

Notice that we’re using the ruby installed in the user’s home directory (~/.rvm)and not the system’s which is in /usr/bin.

Gemsets

The next thing to get familiar with are gemsets. RVM gives you a compartmentalized ruby environment that is independent of the other versions on the system. This is great because it gives you full control over the gem, or package, versions your projects needs to function properly.
A huge part of that compartmentalization are the RVM gemsets. Gemsets are groups of Ruby Gems, that are associated with a version of ruby managed by RVM. You can think of them as gem repositories. You should have one gemset per project, and that’s where RVM really shines, because it makes the process really easy.

You can list the existing gemsets with the command


bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
global

At this point, and if you just installed RVM you should only have one global gemset, you can choose by using the command gemset use


bigdiver$ rvm gemset use global
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0 with gemset global
bigdiver$ rvm gemset name
global
bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
=> global

Both gemset name and gemset list show the current gemset.

To create a gemset you run the command


bigdiver$ rvm gemset create repo1
'repo1' gemset created (/Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@repo1).
bigdiver$ rvm gemset use repo1
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0 with gemset app4

Easy. Repo1 gemset was created and selected for use. Notice the notation 1.9.3@repo1? This indicates that we created the repo1 gemset “under” or “at” the “1.9.3” ruby version. By default RVM creates the gemset under the current ruby version (the one you chose with rvm use command).

You can abbreviate the two commands above with the –create option.


bigdiver$ rvm --create 1.9.3@repo2
bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
repo1
=> repo2
global

As you can see the hash-rocket (=>) now points to the newly created repo2 indicating that it is the selected gemset.

All the gem operations you make, like gem install, etc will only affect the current gemset.

The exception to this is the global gemset, which as the name indicates, is global to the ruby version in use. So all the gems installed to global will be available in all other gemsets created under this version. This is a very good thing since it saves us a lot of time and work when creating new projects.

Using Gemsets with Rails

I usually create a rails directory where all the projects go. And create a gemset per project. You should create the gemset and select it before creating the new rails application.

Let’s do it:

bigdiver$ mkdir ~/work/rails/
bigdiver$ cd ~/work/rails
bigdiver$ rvm --create app1
bigdiver$ rails new app1

If rvm, ruby and rails were properly installed all the commands should run without error, and the Bundler will install a bunch of new gems into the app1 gemset repository.

Now you’re ready for some RVM magic! Lets create a .rvmrc file so that RVM changes to the app1 gemset as soon as we enter, or cd, to the app1 directory.


bigdiver$ cd app1
bigdiver$ rvm --rvmrc 1.9.3@app1

Again we’re using the @ syntax. This creates a .rvmrc file in the current directory, which was ~/work/rails/app1. Now all you need to do is exit from the directory and jump back in again.


bigdiver$ cd ..
bigdiver$ cd app1

==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/Users/bigdiver/work/rails/app1/.rvmrc)
y[es], n[o], v[iew], c[ancel]> Yes

Answer Yes to trust the current .rvmrc file, and you’re done. Now every time you change to the app1 directory the 1.9.3@app1 gemset is selected automatically. Nice!

I also create a .rvmrc file in the rails directory that points to the global gemset, so when I want to install gems globally all i need to do is cd .. from any project.


bigdiver$ cd ..
bigdiver$ rvm --rvmrc 1.9.3@global
bigdiver$ cd ..
bigdiver$ cd rails
==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/Users/bigdiver/work/rails/global/.rvmrc)
y[es], n[o], v[iew], c[ancel]> yes
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@global

Try it out


bigdiver$ cd app1
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@app1
bigdiver$ cd ..
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@global

Neat.

Advertisements

From → Apple, Ruby on Rails

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: