Wednesday 29 February 2012

What is Bundler in ruby on rails?


This article was written for Bundler version 1.0. If you are using a later version of Bundler, the commands and code here may or may not work. If in doubt refer to the Bundler home page.
Bundler is a program for managing gem dependencies in your Ruby projects. With Bundler you can specify which gems your program needs, what versions they should be at, it can help you install them, load them at runtime and distribute them with your software. Basically, it takes all of the guesswork out of installing the gems needed to run your Ruby projects.
Before Bundler, you'd often see a section in the README for any particular project stating the gems you'll need to install. They might have even been kind enough to include a command line for you to copy and paste to install these gems. This worked just fine. Kind of. Sometimes. The problem here (besides it being very clunky and ad hoc) is that new versions of gems can introduce new features, break old features or just behave differently and make your Ruby program fail to run. By using Bundler to specify not only which gems you use but the versions of those gems, you can have very fine and automated control over which gems your program will use.
In addition to this amount of control, Bundler provides a simple way to install these gems. No more copying and pasting lists of gems, or even using the gem command manually. All of this is replaced by a simple command: bundle install. This is simple enough that system administrators and webmasters that have no idea about Ruby can run it, it all just works.
Another useful feature is the ability to store your gems in a vendor directory in your source tree. This allows you to distribute your Ruby program along with its dependencies so it doens't have to reach out to the Rubygems repository, it already has its gems right there.
In addition to the gems and gem versions you give to Bundler, Bundler will also keep track of the versions of all the dependencies of those gems. So it will not only track the gems you know about, but also the gems those gems rely on, whether you know about them or not.

Who Should Use Bundler?

The short answer to this question is "everyone." Bundler is so useful to practically everyone, it has become an absolute fixture in the Ruby ecosystem. Here are a few examples of the types of people who would find Bundler useful.
  • Individual developers. It might not sound like it's that important, but individual developers need to keep control of which gems their programs are using. There's nothing worse than having a program work fine on your development machine and fail elsewhere due to a differing gem version. Even if you're a single developer, and even if you only ever run your programs on your development machine, you should probably be using Bundler.
  • Groups of developers. Not every programmer on a team has the same versions of gems installed. Keeping all the developers on the same page is what Bundler was built for. It's hard to imagine doing development in a group without Bundler.
  • A web developer. If you're a web developer that needs to pass Rails, Sinatra or other Ruby web applications to web designers, system administrators, clients, etc then again, Bundler would be very useful. Even if the other members of the team are not programmers, they will still need to lock gem versions to the same versions you're using.
  • Everyone else. Every Ruby programmer should be using Bundler. I know I've personally encountered bugs where new versions of a gem have introduced some subtle bug and the whole program just crashes, or simply refuses to work. It is rare, gem authors usually do their best to keep regressions from happening, but it does happen. If you aren't using Bundler, you should be! I know that sounds like a sales pitch, but really, you should be.

Installing Bundler

Before you can dig in, you have to install Bundler itself. This should be the only gem you need to install yourself should all your programs' dependencies be managed by Bundler. And there are no surprises here, just gem install bundler. You'll now have the bundler gem installed and thebundle command line tool available.


$ gem install bundler

If you need to be root to install gems, you should run this from a root console or with sudo. Similarly, if the bundle command needs to install gems it should be run as root. If you fail to do this, it will ask you for your root password.

No comments:

Post a Comment