Bundler is for dependency management in ruby projects.

Bundler is itself a ruby gem, so you need install it first

$ gem install bundler

Prepare Gemfile file in project root directory

  • Manually create it;
  • Run bundle init to automatically create it;

Specify dependencies in Gemfile

source 'https://rubygems.org'
gem 'rails', '~> 4.2.0'
gem 'rspec'
gem 'puma'

Refer to Gemfiles for in-depth.

Install dependencies based on Gemfile

$ bundle install

After installation completes, it will also generate Gemfile.lock with snapshot of depencency details. See more.

Setup in application

require 'rubygems'  # builtin for ruby 1.9+ so no need
require 'bundler/setup'

# option 1 - require dependency one by one if not many, e.g.
require 'rails'
require 'rspec'

# option 2 - require all by groups if many to avoid requiring one by one, e.g.
Bundler.require(:default)

See more.

Execute a command in the context of the bundle

$ bundle exec rspec ./spec/model_spec.rb

References

TOP▲
Post tagged with: ruby