1.1 Bundler
Bundler is not a technology that is specific to Rails 4, but it is the preferred way to manage your application's gem dependencies.
One of the most important things that Bundler does is dependency resolution on the full list of gems specifiedin your configuration, all at once.
1.1.1 Gemfile
Located in the root of your Rails project directory, is a Ruby-based gem manifest file named Gemfile. The Gemfile specifies all dependencies of your Rails app, including the version of Rails being used.
loading gems from http://rubygems.org. or other sources specified in
gem source -l
# basic syntax
gem 'kaminari'
gem 'nokogiri'
# load a dependency only in a specific environment
group :development do
gem 'pry-rails'
end
group :test do
gem 'capybara'
gem 'database_cleaner'
ends
group :development, :test do
gem 'spec-rails'
gem 'factory_girl_rails'
end
# The gem directive takes an optional second argument describing the version of the Rubygem desired.
gem 'nokogiri', '1.5.6'
gem 'pry-rails', '>0.2.2'
gem 'decent_exposure', '~>2.0.1'
gem 'draper', '1.0.0.beta6'
# the name of the gem that should be used in a require statement is different than the name ofthat gem in the repository
gem'webmock',require:'webmock/rspec'
1.1.1.1 Loading Gems Directly From a Git Repository
# specify a gem by itssource repository as long as it has a `.gemspec` file in the root directory(Gemspecs with binaries or C extensions are also supported.)
gem 'carrierwave', git: '[email protected]:jnicklas/carrierwave.git'
# If the gem source repository is hosted on GitHub and is public, you can use the `:github` shorthand.
gem 'carrierwave', github: 'jnicklas/carrierwave'
# If there is no .gemspec file at the root of a gem’s git repository, you must tell Bundler which version to usewhen resolving its dependencies.
gem 'deep_merge', '1.0', git:'git://github.com/peritor/deep_merge.git'
# It’s also possible to specify that a git repository contains multiple .gemspec files and should be treated as agem source.
git 'git://github.com/rails/rails.git'
gem 'railties'
gem 'action_pack'
gem 'active_model'
# Additionally, you can specify that a git repository should use a particular ref, branch, or tag as options to thegit directive:
git 'git://github.com/rails/rails.git',
ref: '4aded'
git 'git://github.com/rails/rails.git',
branch: '3-2-stable'
git 'git://github.com/rails/rails.git',
tag: 'v3.2.11'
# specified inline
gem 'nokogiri', git:
'git://github.com/tenderlove/nokogiri.git',
ref => '0eec4'
1.1.1.2 Loading Gems From the File System
gem 'nokogiri', path: '~/code/nokogiri'
1.1.2 Installing Gem
$ bundle install
# You can opt to install dependencies, except those in specified groups using the --without option
$ bundle install --without development test
1.1.3 Gem Locking
Every time you install
or update
, Bundler calculates the dependency tree for your application and storesthe results in a file named Gemfile.lock
. From that point on Bundler will only load specific versions of gemsthat you are using at the moment that the Gemfile was locked, versions that you know will work well with your application.
1.1.4 Packaging Gems
You can package up all your gems in the vendor/cache
directory inside of your Rails application.
$ bundle package
Running bundle install
in an application with packaged gems will use the gems in the package and skipconnecting to rubygems.org or any other gem sources. You can use this to avoid external dependencies atdeploy time, or if you depend on private gems that are not available in any public repository.
Non-Rails scripts must be executed with bundle exec in order to get a properly initialised RubyGems environment.
$ bundle exec guard
Bundler also comes with the ability to create binstubs
of every executable in your bundle. Invoking bundle install
with the --binstubs
option creates a folder bin in the root directory of your Rails application, that contains scripts wrapping executables initialized with the project’s RubyGems environment.
Using binstubs
, scripts can be executed directly from the bin directory without prefixing bundle exec
.
$ bin/guard
$ guard
As of Rails 4, generating a new application will result in the creation of binstubs for your Rails app executables. A bin
directory is added to your project root containing bin/bundle
,bin/rails
, and bin/rake
. To updateRails specific stubs on an existing project, run rake rails:update:bin
.