bundler17 Apr 2017
gems outside any named group are in
install gems from all groups:
install gems from all groups except
$ bundle --without production
if your app is gem dependent gems are added to gemspec file instead of
add_runtime_dependency (kind of
default group) or
add_development_dependency (kind of
development group) methods.
these dependencies are then included in Gemfile using
in this case Gemfile might contain only
test or custom groups.
require all gems from groups
Rails app by default requires
default and group corresponding to
current environment in config/application.rb:
# Rails.groups = [:default, Rails.env] Bundler.require(*Rails.groups)
in plain Ruby app you have 2 options:
require all gems from all or selected groups in lib/my_app.rb (common entry point of CLI application or gem)
# require gems from all groups Bundler.require # require gems from default and production groups Bundler.require(:default, :production)
current environment can be fetched from custom environment variable - you’ve got to set it manually when launching app.
this method is recommended for large
require gems manually in appropriate files where they are used
- gems from
rspec) are required in spec/spec_helper.rb
- gems which are used in Rakefile only (say,
rubocop) are required only there
pry-byebuggem can be required in lib/my_app.rb only if
DEBUGenvironment variable is set
this method is recommended for smaller
- gems from
bundler setup configures the load path so that gems from all or specified groups can be required.
configure the load path for all gems:
configure the load paths for gems from specified groups:
in theory you should first configure the load path and then require gems:
require 'bundler/setup' Bundler.require
but in practice I don’t configure the load path as described above - all gems can be required without configuring the load path first.
UPDATE: I encountered situations when gem cannot be loaded because it’s not found without calling
require 'bundler/setup'first - this is the case for
ruby-fifogem and my own gem
files. thus I had to configure the load path before requiring gems by adding
require 'bundler/setup'to the beginning of lib/my_app.rb file.