How to use Bundler with Ruby
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
Learn More: Groups
require 'rubygems' require 'bundler' Bundler.setup(:default, :ci) require 'nokogiri'
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 app, your default application already has the code necessary to invoke bundler.
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
Gemfileavailable 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
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
For our example Gemfile, this line is exactly equivalent to:
require 'rails' require 'rack-cache' require 'nokogiri'
Astute readers will notice that the correct way to require the
require 'rack/cache', not
require 'rack-cache'. To tell bundler to use
require 'rack/cache', update your Gemfile:
source 'https://rubygems.org' gem 'rails', '5.0.0' gem 'rack-cache', :require => 'rack/cache' gem 'nokogiri', '~> 1.4.2'
For such a small
Gemfile, we'd advise you to skip
Bundler.requireand just require the gems by hand (especially given the need to put in a
:requiredirective in the
Gemfile). For much larger
Bundler.requireallows you to skip repeating a large stack of requirements.