Bundler.setup
Configure the load path so all dependencies in
your Gemfile can be required
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
Only add gems from specified groups to the
load path. If you want the gems in the
default group, make sure to include it
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'
Compatibility
Ruby 2.0 and RubyGems 2.0 both require Bundler 1.3 or later. If you have questions about compatibility between Bundler and your system, please check the compatibility list.
Learn More: Compatibility
Setting Up Your Application to Use Bundler
Bundler makes sure that Ruby can find all of the gems in the
Gemfile
(and all of their dependencies). If your app is a Rails 3 app, your default application
already has the code necessary to invoke bundler. If it is a Rails 2.3 app, please see:
Setting up Bundler in Rails 2.3
For another kind of application (such as a Sinatra application), you will need to set up
bundler before trying to require any gems. At the top of the first file that your
application loads (for Sinatra, the file that calls
require 'sinatra'
), put
the following code:
require 'rubygems'
require 'bundler/setup'
This will automatically discover your
Gemfile
, and make all of the gems in
your Gemfile
available to Ruby (in technical terms, it puts the gems "on the
load path"). You can think of it as an adding some extra powers to require
'rubygems'
.
Now that your code is available to Ruby, you can require the gems that you need. For
instance, you can
require 'sinatra'
. If you have a lot of dependencies, you
might want to say "require all of the gems in my Gemfile
". To do this, put
the following code immediately following require 'bundler/setup'
:
Bundler.require(:default)
require 'rails'
require 'rack-cache'
require 'nokogiri'
Astute readers will notice that the correct way to require the
rack-cache
gem is require 'rack/cache'
, not require 'rack-cache'
. To tell
bundler to use require 'rack/cache'
, update your Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.0.0.rc'
gem 'rack-cache', :require => 'rack/cache'
gem 'nokogiri', '~> 1.4.2'
For such a small
Gemfile
, we'd advise you to skip
Bundler.require
and just require the gems by hand (especially given the
need to put in a :require
directive in the Gemfile
). For much
larger Gemfile
s, using Bundler.require
allows you to skip
repeating a large stack of requirements.
Edit this document on GitHub if you caught an error or noticed something was missing.