I use RVM with gemsets and Bundler to manage my Ruby environment and dependencies. Some people question that. “Isn’t RVM over-complicated?” they say. Or, “why are you using gemsets when you have bundler?” There are even some ethical objections: “I don’t like how RVM overrides shell commands!”
Allow me to address these questions, starting with the easy one: RVM overrides shell commands, mainly the cd command so that it can detect environment files when we change directories. So what!? It just works and unless you’re some kind of Shell Puritan that’s all you should care about. Besides, many other apps and utilities use all kinds of shims and proxies and no-one bats an eyelid. Get over it!
But what about complexity? Installing RVM involves a single command, two if you include downloading the gpg key. The vast majority of the RVM commands that I use daily are just a combination of the words use, gemset and create with a couple of list and install thrown in for good measure. Pretty simple really.
Finally, why use gemsets? Surely, with Bundler there is no need to use gemsets any more, is there? Well, from a deployment point of view, this is correct. We can just deploy our Gemfile and Gemfile.lock and Bundler will ensure that the right versions of our gems are installed. Furthermore, Bundler can also tell us whether our gems are outdated or if we can upgrade the version of a specific gem without breaking others and other such wonderful stuff. However, from a development POV using gemsets is the cleanest and safest way to manage our environment. Consider a non-gemset set-up:
- We have to prefix commands with bundle exec. This is a right pain in the neck, even if we do get to alias bundle exec in our shell.
- There is no easy way to remove gems used in a project once we've finished with that project. So we tend to end up with a heap of unused and unnecessary gems of different versions in our system.
RVM simply stores each permutation of <RubyVersion> and <Gemset> in separate directories. Whenever we decide to use a new Ruby Version or Gemset, RVM will just set our GEM_HOME and GEM_PATH environment variables to the appropriate directories.
$> echo $GEM_HOME
/home/fred/.rvm/gems/ruby-2.4.3@gemset1
$> echo $GEM_PATH
/home/fred/.rvm/gems/ruby-2.4.3@blog:/home/fred/.rvm/gems/ruby-2.4.3@global
My Workflow
Setup global gemset
I install commonly-used gems like bundler and nokogiri in the Global gemset. The Global gemset acts as a template from which all other gemsets are created. This means that whenever I create a new gemset, it will already include bundler and nokogiri.
$> rvm gemset use global
$> gem install bundler nokogii
Create new gemset
When I start a new project, I create a new gemset for it
$> rvm gemset use myapp --create
$> gem install bundler nokogiri
#myapp gemset now includes bundler and nokogiri
App-specific gems
In the case of Rails projects, I’ll install the Rails version I want straight into my gemset. No, I don’t put it in my Gemfile first and then call bundle; there’s no need: the rails new generator will do that when I create my Rails project
$> gem install rails -v 5.0.0
$> rails new myapp
Installing rails directly in my gemset also means that I know I’m using the right rails version, so i don’t have to type bin/rails.
Config files
I then create RVM config files .ruby-version and .ruby-gemset in my project directory. These files will be looked up by RVM whenever I cd into this directory so that RVM will load the right Ruby version and gemset for me.
$> cd myapp
$> rvm use --ruby-version 2.4.3@myapp
#creates .ruby-version .ruby-gemset
And that’s it. Simple and effective. Clean separation of dependencies, no faffing about with bundle exec and full control over my gems. Sweet!
I’d love to hear how you manage your gems and dependencies. If you use other version managers, what makes them better / easier than RVM? Let me know.