Checking out the Rails 3 Beta, Part I: Bundler

Rails 3
The beta version of Ruby on Rails 3.0 was released back in February. It’s the result of the merging of the Rails project with Merb (another Ruby-based MVC framework), as well as a ton of bug fixes, performance enhancements, and new features.

I’ve been playing with it a bit for a personal project I’m working on, so I thought I’d share some of the cool new features with you. A lot of the posts out there about Rails 3 are aimed at hardcore Rails heads, so they’re unlikely to appeal to anyone who’s not used the framework before (or who’s only dabbled with it). I’m going to take a different approach here, and try and show you that now’s as good a time as any to jump into Rails and start playing around. If you tried Rails back in the 1.x or 2.x branches, maybe one of the new features I’ll be outlining will convince you to give it another try.

So, without further ado, let’s look at the first Rails 3 feature a new developer is likely to encounter. Rails 3 introduces Bundler, a very simple way of managing the dependencies of your application. Rails applications typically depend on a number of Ruby gems (what Ruby calls its libraries). For example, you might use Paperclip for handling models with attached files, Devise for authenticating users, or Cucumber for writing integration tests. Up until Rails 2.1, you basically had to manage any of your application’s dependencies yourself. If you deployed your application to a server that was missing a gem, or had a different version, it might just break without warning. Rails 2.1 (and 2.2) introduced some dependency management in the form of gem.config, but its usage was a little arcane, and it was prone to breakage.

Rails 3′s Bundler makes gem management as simple as you can imagine. You put a file in your application’s root, called Gemfile, where you specify which gems your application needs in the following format:

gem 'paperclip', '2.3.0'

The version string is optional; if it’s absent, Bundler will grab the latest version available on Gemcutter, the principal Ruby Gem repository. Once you’ve set up the Gemfile to your satisfaction, all you need to do is run this command from your application’s directory:

bundle install

Bundler will then download all the required gems (and any dependencies they might have) to your machine.

These features alone make Bundler a great addition to Rails, but there’s more to it than that. Bundler also installs gems from GitHub seamlessly. All you need to do is specify a URL and a branch, and the rest is handled automatically when you run bundle install. For instance, the current version of Paperclip has yet to be updated to support Rails 3. However, Jason King has forked the project on GitHub and patched it so that it works. So, to import the patched version into my app, all I need to write is:

gem 'paperclip', :git => 'git://github.com/JasonKing/paperclip.git', :branch => 'rails3'

My application will now grab that patched version. When the official Paperclip gem is updated, all I need to do is remove the :git and :branch options, and re-run bundle install. I’ll now be running the version from Gemcutter.

Another great feature of Bundler is that you can specify the environments where a given gem is required by putting your gem statements into groups:

group :test do
  gem 'webrat'
  gem 'shoulda'
end

With this code in my Gemfile, I can run the following on my production machine:

bundle install --without test

This is useful because I’d rather avoid worrying about managing the libraries required to compile Webrat and its dependencies on a machine on which they’ll never be used anyway.

Bundler is only one of the many new features in Rails 3; it just happens to be the first one you’ll encounter when setting up a new application. Next week I’ll be looking at a few features that more directly impact development. Until then, check out the release announcement for instructions on installing the beta, and the official release notes to find out about everything that’s in the new version.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • andain

    Spoted tiny bug in text:

    When the official Paperclip gem is updated, all I need to do is remove the :gem and :branch options…

    Shouldn’t it be :git?

    Aside from that good article and i hope sitepoint will post more on RoR.

  • Louis Simoneau

    Whoops! Fixed, thanks for pointing it out.

  • J

    Why do you use double quotes in the first code example (gem “paperclip”, “2.3.0″) and single quotes in the second related example (gem ‘paperclip’, :git => ‘git:…’)?

  • Louis Simoneau

    @J: no particular reason. But consistency is good, so I’m changing them to single quotes throughout. Good spotting!

  • Oldroy

    Only trouble is that bundler (bumbler?) doesn’t seem to install gems in a manner that rails 3.0 can use. On paperclip for instance, using instructions above on a fresh and working clean install of rails 3.0 on ubuntu, errors abound. In particular, “Could not find generator paperclip” continues – both with JasonKing and with :branch => ‘rails3′ of thoughtbot.

    Paperclip gem is installed in .bundle/ so the installation is recognized. It’s just that rails 3.0 can’t find the generator.

    Not to hijack the thread to talk about paperclip problems, it’s just that bundler kind of bumbles the install on some if the stuff it was supposed to make installation and management easier. Yes bundler will be great, it just doesn’t seem like it’s quite finished and ready yet.

  • Louis Simoneau

    @OldRoy: That’s weird. An app I’m working on uses Paperclip (the JasonKing fork) with Rails 3.0 and I haven’t had any errors (also on an Ubuntu server) …

  • Oldroy

    Yep,
    Weird. I’ve spent a bunch of time learning all of the ins and outs of bundler commands. Install, show, check etc. Run rails generate and all the generators are there except for paperclip. I’ve been reading in several places of needing to “require” paperclip ( as in :require => ‘paperclip’ at the end of the gem line ) in the gemfile but I don’t think that is correct.

    I’m on rails beta 3. I’m not sure if maybe that is the problem, I doubt it. Also I’m on Kubuntu 10.04 Desktop Beta. I don’t think it’s an issue but I’m about to overwrite it with Ubuntu 9.10 and start over.

    There is a lot of incorrect or old info out there about bundler use. Your info seems to be the only correct info for correct install of paperclip.

    I do have to say that I get it now about the usefullness of bundler. Just list it, install it, lock it and forget it. It just doesn’t work for me this week. Maybe next week….