RVM or Ruby Version Manager allows you to install and manage Ruby environments. Similar tools are Rbenv and chruby. I previously wrote about installing these on a Mac here.
I used RVM for a long time on my Mac. One of the useful features was Gemsets. It gives you a self-contained environment for your gems. For the same Ruby version, you can have multiple versions of a gem like Rails. One gemset can have Rails 5.1 while another gemset can have Rails 5.2.0.rc2 if you want to test the upcoming Rails version.
I recently switched to chruby which doesn’t have a similar feature to Gemsets. But it’s fine as I no longer need it. Gemsets are not totally obsolete but you can work around your need for it as we’ll see below.
Why do we need Gemsets in the first place? When working with Ruby projects, we often times need to install different versions of the same gem. We probably have projects that were started with different Rails versions and we haven’t upgraded yet. Or we probably use a dependency that has a new version and we want to install both versions before we upgrade.
Rubygems uses the latest version installed on your system. You can require a specific version of a gem from your application but if another part of your application requires the same gem but with a different version, you’ll run into problems. If you’ve used Rails 2, you know what I’m talking about. The easiest way to avoid these problems is to install one version of the gems your app use.
Gemsets were needed when you have multiple applications. Since you want one set of gems per application, you can create one gemset for every application. The gems from one application will be installed in a self-contained environment.
All this became unnecessary when Bundler was released. Bundler chooses which gems are loaded on your application. Even if you have different versions installed at the same time, Bundler can choose the latest version that satisfies your app’s requirements. It won’t naively choose the latest installed version. It has to satisfy the requirements of all gems of your application.
On chruby, my Ruby environment contains gems with multiple versions. I run my apps using bundler so I don’t get any errors related to multiple versions being activated at the same time. You have to prepend all commands with bundle exec
. Using an alias for bundle exec
like be
will save you from typing a few keystrokes.
If you’re still using RVM, one advantage of Gemsets is you don’t have to use bundle exec
. I used to have 2 different Gemsets for Engine Yard versions 2 and 3. I can run ey deploy
on either Gemsets and it will work. Now with chruby, I need to have a Gemfile and use bundle exec
.
I can also use the version number when calling a command. If you have both Rails 5.1.5 and 5.2.0.rc2, you can choose Rails 5.1.5 with
rails _5.1.5_ new rails51app
If you don’t specify _5.1.5_
, the rails command will use 5.2.0.rc2. When using Gemsets, you don’t need to specify the version since you only have one version per Gemset.
In summary, if you are still using RVM, use Gemsets. But don’t stay with RVM for that feature alone. If you use chruby, the lack of Gemsets shouldn’t be a problem since you can use Bundler.