Bundler Gives You Pay-as-you-go Gem Management
You may have heard about Bundler, a rising star in the latest wave of Ruby tools. Bundler’s specialty is libraries management: it helps you to sort out the gems that your projects need.
With all the interesting tools coming out lately, you might be tempted to file Bundler under “cool stuff I’ll eventually look at,” together with the latest testing framework and NoSQL database. However, there is a good reason to adopt Bundler in your own project right now: Bundler is a pay-as-you-go technology. It doesn’t force you through a steep learning curve like other tools do (cough, vim, cough). You can reap a few benefits out of Bundler in a matter of minutes, and the more time you invest in it, the more time-saving features you seem to discover.
In this article I’ll show you three very cheap, very quick ways to get a little help from Bundler. But first, let me explain why you should use Bundler in the first place.
Isn’t RubyGems Enough?
RubyGems is a powerful, flexible package manager. However, there is one place where even RubyGems has trouble reaching: inside your projects.
By “project,” I mean anything from a simple script to a full-fledged Rails app. RubyGems doesn’t know anything about this stuff. When you install a gem, RubyGems just adds it to a global list of system gems. As this list grows, it can become difficult to tell which gems are used by which projects.
You might have experienced some of that confusion yourself. Maybe you checked out the latest version of your team’s project, and saw it crash because of a missing gem. Maybe you were left wondering which gems in your system you could safely uninstall without hurting some long-forgotten project. Or maybe your freshly deployed system crashed because of a gem on the production machine that wasn’t the exact same version you’d been using in development.
In general, per-project gems management can turn out to be more trouble than you might expect.
For years, Ruby programmers have rolled out their own custom-made solutions to this problem. Now we have Bundler, which builds upon RubyGems to give you a number of project-focused goodies.
First Benefit: A List of Gems You Can Rely On
First, install Bundler – it’s a gem itself:
Gem install bundler
(You might need to run most commands in this article as a superuser, with the sudo command. Also, you need RubyGems 1.3.6 or later to install Bundler. You can check your current RubyGems version with gem – version, and update it with gem update – system.)
OK, now you have Bundler. Pick one of your existing projects, or just create a test project – a directory containing a simple Ruby script. Go to the project’s root directory, fire up your editor and create a file named Gemfile. In the Gemfile, list the gems that you want to use in your project. Use this syntax:
Source :rubygems
Gem “activerecord”
gem “json”
The first line in the Gemfile points Bundler to a place where it can download missing gems – in this case, the official RubyGems host. If you want to be sure that all the gems in the Gemfile are installed on your system, just issue Bundler’s most popular command:
Bundle install
Bundler checks whether you already have all the gems that your project needs. If you don’t, it installs the missing gems – and the gems they depend upon.