RVM and Gemsets Tutorial

The Ruby language uses "gems" to extend itself, Rails for example, is a gem. You may need to have different gems installed for different Ruby projects, you may even need different versions of Ruby; you can handle this easily using RVM. RVM stands for Ruby enVironment Manager, instructions for installing it can be found on RVM's website.

Once you have RVM installed you can install a version of ruby using rvm install <version-number> for example, to install Ruby version 1.9.2 you would run:

rvm install 1.9.2
You can then list all the versions of Ruby in RVM with the command  rvm list. You can also use rvm to manage sets of gems, you can create a gemset with a command like rvm gemset create <gemset-name> for example:
rvm gemset create my-first-gemset
You can then tell rvm which version of ruby to use and which gemset to use like so:
rvm use 1.9.2@my-first-gemset

You really don't want to have to run this command every time you switch between projects, so you can use .rvmrc files to switch your environment. You can put an .rvmrc file containing a command like above inside a directory and every time you change to that directory your rvm environment will change to it. You can put other commands to be run in your .rvmrc file as well, because of this, every time an edit is made to your .rvmrc file, you will be prompted before the commands are run.

Once you have your gemset selected you can install gems and they will only be installed in that particular gemset. You can intall gems like gem install <gemname> --version <version-number> for example:

gem install bundler --version 1.0.21
You can then list all the installed gems in the selected gemset with the command gem list you can also list all the gemsets and see which one you are currently using with the command rvm gemset list.

The bundler gem lets you create a GemFile to list gems and have them automatically installed when you run bundle install or just bundle for short. You specify the gems to be installed via gem "gemname" and you can specify the version with gem "gemname", "version" for example:

gem 'rails', '3.2.2'
If you don't specify a version then bundler will install the latest version. I recommend always specifying a version to avoid some issues. For example, if your application relies on a specific gem version, running bundle install could update to a newer gem and break your application.

Also when working with Rails, make sure every gem you use is specified in your GemFile because some places you may deploy to like Heroku may depend on our GemFile to install the necessary gems. Your app may work fine locally because you manually installed a required gem but when you deploy, Heroku has no way to know your app needs this gem, so it doesn't get installed and you wonder why your app works locally but not remotely. Because of this I also recommend you do not install any gems into your global gemset.

There is a lot more to RVM, Gemsets, Bundler, and GemFiles then what I have covered, but this should be a good introduction to get a novice up and running.

Leave A Comment