Jeff Kreeftmeijer

Bundler, because your gems depend on gems too

2010-08-16

Bundler made gem dependencies in Rails projects a lot easier. It creates a “bubble” of gems you listed in your Gemfile, which means it only loads the ones you specified. While this might sound annoying, it keeps you from having incomplete gem dependency specifications like you did when you were still using config.gem. This makes it easier to keep your Gemfile complete and your project simple to get up and running.

But what about gems themselves? We use the .gemspec file to specify gem dependencies for our gems, which — when complete — makes installing and using gems a breeze since it installs all dependencies automatically. Great.

I’m not talking about using gems, though. I’m talking about working on and contributing to gems. Your .gemspec doesn’t help contributors when they clone your project and want to get the tests running, they still have to read through it and install everything manually.

Giving your gem a Gemfile partially solves this problem. If you specify everything your gem depends on in your Gemfile, contributors can quickly install everything they need without having to look anything up. The downside is that now we have two files (the .gemspec and Gemfile) that specify dependencies, which is nasty.

Lucky for you, Bundler loves you. I didn’t know this, but it can read and install gems from your .gemspec. To get it up and running, create a Gemfile and add:

gemspec

This will check your .gemspec for runtime and development dependencies (specified with gem.add_runtime_dependency and gem.add_development_dependency) and install them to the correct group when running:

$ bundle install

Now the only place we specify dependencies is in the .gemspec, but Bundler can install everything for us making our gem and its dependencies easy to install for users and contributors.

Oh, since you have a Gemfile in your project now, here’s a little bonus: You can use Envy LabsRuby Tracker to get notified when new versions of your dependencies get released so you can make sure your gem keeps working with the latest stuff. Nice tip, right?