As a Ruby on Rails Freelancer since few years now, I have been worked in a bunch of projects.

For each mission, the environment setup is very important as I’ve to be efficient quickly.

This is where RVM Gemsets are important.

Before to start

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this link! 🙏

Thank you for your time!

What’s a Gemset

RVM Gemset allows you to create a new isolated environment for dependencies. This means that you ensure to avoid cross-dependency issues as you are isolated from other gemsets for a given Ruby version.

So, your installed gems in this gemset will only be available in this last one.

Let’s have a look to RVM Gemset specificities.

The default and global Gemsets

When a new version of Ruby is installed, default and global gemsets are automatically created

$> rvm install ruby-2.4.1

...

Install of ruby-2.4.1 - #complete

$> rvm gemset list

gemsets for ruby-2.4.1 (found in /Users/mehdi/.rvm/gems/ruby-2.4.1)

=> (default)

global

The default gemset is used when no gemset is explicitly provided

$> rvm use 2.4.1

Using /Users/mehdi/.rvm/gems/ruby-2.4.1

$> rvm gemset list

gemsets for ruby-2.4.1 (found in /Users/mehdi/.rvm/gems/ruby-2.4.1)

=> (default)

global

The global gemset is shared amongst all the other gemsets for a specific version of Ruby. It contains a bunch of important gems

$> rvm use 2.4.1@global

Using /Users/mehdi/.rvm/gems/ruby-2.4.1 with gemset global

$> gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0)

bundler-unload (1.0.2)

did_you_mean (1.1.0)

executable-hooks (1.3.2)

gem-wrappers (1.3.0)

io-console (default: 0.4.6)

json (default: 2.0.2)

minitest (5.10.1)

net-telnet (0.1.1)

openssl (default: 2.0.3)

power_assert (0.4.1)

psych (default: 2.2.2)

rake (12.0.0)

rdoc (default: 5.0.0)

rubygems-bundler (1.4.4)

rvm (1.11.3.9)

test-unit (3.2.3)

xmlrpc (0.2.1)

The default gemset “inherit” from the global gemset.

$> rvm use 2.4.1

Using /Users/mehdi/.rvm/gems/ruby-2.4.1

$> gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0)

bundler-unload (1.0.2)

did_you_mean (1.1.0)

....

rvm (1.11.3.9)

test-unit (3.2.3)

xmlrpc (0.2.1) $> rvm use 2.4.1 --ignore-gemsets

Using /Users/mehdi/.rvm/gems/ruby-2.4.1

$> gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0)

io-console (default: 0.4.6)

json (default: 2.0.2)

openssl (default: 2.0.3)

psych (default: 2.2.2)

rdoc (default: 5.0.0)

In this case, the --ignore-gemsets will ignore the global gemset (in reality, this option ignores everything but the default gemset).

The Foobar project

Nothing better than an example to illustrate the above explanation.

Let’s assume that my new mission is for the Foobar project.

This is a SaaS with a RoR 5 backend. The project is based on the version 2.4.1 of Ruby.

So first, let’s install this ruby version

$> rvm install 2.4.1

...

Install of ruby-2.4.1 - #complete

Then we create the foobar gemset

$> rvm use 2.4.1

Using /Users/mehdi/.rvm/gems/ruby-2.4.1

$> rvm gemset create foobar

ruby-2.4.1 #gemset created /Users/mehdi/.rvm/gems/ruby-2.4.1@foobar

ruby-2.4.1 #generating foobar wrappers..............

$> rvm use 2.4.1@foobar

Using /Users/mehdi/.rvm/gems/ruby-2.4.1 with gemset foobar

$> gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0)

bundler-unload (1.0.2)

did_you_mean (1.1.0)

executable-hooks (1.3.2)

gem-wrappers (1.3.2, 1.3.0)

io-console (default: 0.4.6)

json (default: 2.0.2)

minitest (5.10.1)

net-telnet (0.1.1)

openssl (default: 2.0.3)

power_assert (0.4.1)

psych (default: 2.2.2)

rake (12.0.0)

rdoc (default: 5.0.0)

rubygems-bundler (1.4.4)

rvm (1.11.3.9)

test-unit (3.2.3)

xmlrpc (0.2.1)

Now, let’s add the bundler gem to our foobar gemset

$> gem install bundler

Fetching: bundler-1.16.1.gem (100%)

Successfully installed bundler-1.16.1

Parsing documentation for bundler-1.16.1

Installing ri documentation for bundler-1.16.1

Done installing documentation for bundler after 4 seconds

1 gem installed

$> gem list bundler *** LOCAL GEMS *** bundler (1.16.1)

bundler-unload (1.0.2)

rubygems-bundler (1.4.4)

Let’s move back to the default gemset to see if the bundler gem is available in it

$> rvm use 2.4.1

Using /Users/mehdi/.rvm/gems/ruby-2.4.1

$> gem list bundler *** LOCAL GEMS *** bundler-unload (1.0.2)

rubygems-bundler (1.4.4)

As you can see, the bundler gem is not available from outside of the foobar gemset.

Note that foobar has access to all the gem available in the global gemset.

Conclusion

When you start or join a ruby project (ruby script, gem, ruby on rails, sinatra, etc..), ensure to create a new gemset to use a clean and isolated environment for your dependencies.

This can save you a lot of time if you’ve to deal with a bunch of projects in same time.

Voilà !

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com