Thursday, September 30, 2010

Rails Bundler

I've been working with Rails3, and in particular with the bundler feature. Bundler exists because dependency management is truly obnoxious, and bundler tries to make it easier.

I'll let the tutorial teach you how to set up bundler. Let's look at what we're striving for.

The ideal is that setting up a Ruby application is as simple as:
  • install your database
  • install the right Ruby
  • install the bundler gem
  • check out the source tree
  • configure config/database.yml
  • run "bundle install"
  • run "rake db:setup"
  • Go!
This is really cool. No. Really, it's that cool. It's much easier than clicking on a page and watching it go boom because of some dependency you forgot.

Here's what's going on:
You specify the gems you want in the Gemfile, including the version you want. Then you run "bundle install", and it creates a Gemfile.lock file with all the gems that you're using (including their versions).

This directly addresses the consistency of various environments issue, in that each person does a "bundler install" which retrieves the exact versions specified in the Gemfile.lock and makes them available to the application. This is much more reliable than simply saying "require 'mime-types'" and maybe getting version 1.16 or maybe getting 1.15 depending on what was installed on that developer's system or that server. The only ruby gem needed, in fact, is bundler itself - all others, including rails, are loaded using it in isolation of any other versions of those libraries installed on the system. You can have four versions of some gem installed, and it'll use exactly the one specified in Gemfile.lock.

There must be some downside:
The biggest drawback to the bundler arrangement we have now is that deployment to servers (or development environments) has a dependency on the availability of the rubygems.org server and specific versions specified in the project Gemfile. If that server(s) is down or the specific version required no longer available, the "bundle install" will fail.

The fix:
Run "bundle package", which loads the gem packages into your application. You can then check them in. No more external dependency (but your source tree is rather larger).

Now that's cool. If you're on Rails3, give bundler a shot.


This guest post is courtesy Dan Powell.

No comments:

Post a Comment