Posted March 27, 2011 by CoderTrader in bundler, jruby, ruby. Leave a Comment

We’ve all been there. At one time or another, we’ve installed a gem that has other dependencies that conflict with other gems on our system, and we end up playing dependency whack-a-mole or monkey-patching our way to a working environment. Along comes Bundler, a tool with a goal to be the solution to this problem.

Bundler assumes you have a Gemfile at the root of your project; this applies not only rails projects, but to other Ruby projects or gems as well. Entries in the Gemfile look like the following:

#Gemfile format: gem , (, :require => ) (, :git=>repo)

gem ‘activemerchant’, ‘1.4.2’

gem ‘nokogiri’

gem ‘faker’, ‘> 0.3’

gem ‘decent_exposure’, ‘~> 1.0.0.rc1’

gem ‘rspec’, ‘2.0.0.beta.20’

gem ‘sqlite3-ruby’, :require => ‘sqlite3’

gam ‘paperclip’, :git => ‘git://github.com/thoughtbot/paperclip.git’

gem ‘deep_merge’, ‘1.0’, :git => ‘git://github.com/peritor/deep_merge.git’ # specify a git repo should use a particular branch as options to git directive

git ‘git://github.com/rails/rails.git’, :branch => ‘2-3-stable’ # install from a specific branch when installing a gem inline

gem ‘nokogiri’, :igt => ‘git://github.com/tenderlove/nokogiri.git’, :ref => ‘0eec4’ #install from local code

gem ‘nokogiri’, :path => ‘~/code/nokogiri’

Once you have your Gemfile ready to go and all dependencies listed, you can run:

bundle install

to ensure all dependencies will be installed and available to your application. You may notice that this will start to install more gems than you’ve listed in your Gemfile. This is because it ensures that any dependencies of your listed dependencies are installed, and so on. Bundler is as conservative in its installation as possible, only installing dependencies with versions that do not conflict with other dependencies.

If you have listed libraries that should only be installed in certain environments, you may run:

bundle install –without development test

The default location for bundler installations is ~/.bundler. To change this, use:

bundle install

To force installation and not use previously installed gems, use:

bundle install –disable-shared-gems

Once you’ve installed gems or used bundle update, the dependency tree is stored in Gemfile.lock.

It is good practice to check in your lock file into the repository so that everyone who gets your project can be sure to also install the same exact versions of dependencies that worked most recently.

If you’re developing a Rails application, you can go even one step further to make sure all of your dependencies are packaged up with your rails application structure under vendor/cache:

bundle package

This is especially useful at deploy time, or where you need to depend on private gems not in a public repo.

Bundler has become popular with the advent of Rails 3.0 and later, but you may also want to use this handy tool with a ‘legacy’ Rails 2.3 application. Luckily, the Bundler team has documented the steps you’ll need to take to make this happen for your project.

http://gembundler.com/rails23.html