A Ruby workflow with RVM and Bundler

Published 24 May 2012 under ruby, rvm, bundler, how to

Ruby has some great tools surrounding it which make it a pleasure to work with but getting everything setup can be tricky.

Bundler is an awesome tool which manages an application's dependencies. It removes the problems of having to document which RubyGems you need to install before you can get going on a project. Simple type bundle install in a directory with a Gemfile and the project's dependencies will be installed.

RVM (Ruby version manager) provides a nice way to have multiple rubies installed at once. It also releases you from the nightmare that is Ubuntu apt-get Ruby. There are a couple of things I really don't like about setting up RVM but once set up it's wonderful.

Installing RVM

To install RVM head over to their install instructions and execute the first command and make sure that the following or something similar is in your .bashrc:

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

Before going onto install a Ruby it's probably worth installing a few rvm packages:

rvm pkg install zlib
rvm pkg install readline

Once you've installed the packages you need, go ahead and install a Ruby:

rvm install 1.9.3

and use optionally make it the default:

rvm use 1.9.3 --default

The workflow

One of RVM's killer features is gemsets. A gemset allows you to have an isolated set of gems. It's a really good idea to have a specific set of gems for each project, that way you can be sure which versions of the gems you're actually using and avoid any potential conflicts between versions.

Before installing any gems add the following two lines to your ~/.gemrc to save waiting around for rdoc and ri to install.

install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

When starting or after cloning a Ruby repo from git which uses Bundler create a gemset for the project. The convention I use is to give the gemset the same name as the project directory - that way it's easy to remember what it is. To create and use the gemset:

rvm gemset use <gemset name> --create

Each project should have its own Gemfile so install and run the Bundler gem inside our empty gemset:

gem install bundler
bundle

To switch between gemsets use the following:

rvm gemset use <gemset name>

Manually switching between gemsets is time-consuming so we want to automate it. RVM to the rescue! By putting a .rvmrc file with the ruby and gemset version in each project we can get RVM to automatically switch between gemsets when we cd into the directory! For example the following will switch to Ruby 1.9.3 and the my-project gemset:

rvm 1.9.3@my-project

To automate even further we can automatically create a .rvmrc when we create the gemset:

rvm gemset use <gemset name> --create --rvmrc

Comments

blog comments powered by Disqus