Ruby - Bundler
Bundler is a tool that manages gem dependencies for your application. You specify a Gemfile with a list of all your dependencies.
# Gemfile example
source "https://rubygems.org" # From where to download gems.
ruby "2.2.2" # Ruby version.
gem 'gem-name' # Installs latest if no Gemfile.lock
gem 'gem-name', '3.0.0.beta3' # Specific version
gem 'gem-name', '>=1.0' # Version must be over or equal to 1.0
gem 'gem-name', '~>2.0.3' # ~> 2.0.3 is identical to >= 2.0.3 and < 2.1
gem 'gem-name', '~> 2.1' # ~> 2.1 is identical to >= 2.1 and < 3.0
# Gems for a specific environment.
group :development do
gem 'gem-name'
end
# Gems from a specific source.
source "https://my-gems.com" do
gem 'gem-name'
end
# Sometimes gem packages and Ruby require statement path are different
gem 'gem-name', :require => 'gem/name'
gem 'rspec', :require => false # Installs the gem but doesn't require it
# Sometimes you have a custom location for a gem.
gem 'gem-name', git: 'git@github.com:user/gem-name.git'
gem 'gem-name', github: 'user/gem-name'
gem 'gem-name', git: 'git://github.com/user/gem-name.git', ref: '0eec4'
gem 'gem-name', path: '~/gemz/gem-name'
You install all dependencies with bundle install
. Will use Gemfile.lock
if it is found, Gemfileif not found. The first
bundle installon
Gemfilewill generate the lockfile and you can update gems in it with
bundle update`
bundle install
bundle update gem-name
bundle install --local # Save gems inside the project, to vendor/cache.
bundle exec file.rb # Non-rails Ruby requires special execution command.
Binstubs are scripts that are executed in bundler context. Rails stores binstubs in bin
directory.
# Usually these are the same inside a project.
bundle exec rails
bin/rails
Don't remove the Gemfile.lock
from version control. This is not some randomly generated file - it makes sure that all of your team members get the same gem versions with bundle install
.
Separate gems to groups for development or testing in the Gemfile.
Separate gems for OS X and Linux when necessary. Otherwise Gemfile.lock
will keep changing all the time.
# Gemfile
group :darwin do
gem "rb-fsevent"
gem "growl"
end
group :linux do
gem "rb-inotify"
end
# and add config/application.rb
platform = RUBY_PLATFORM.match(/(linux|darwin)/)[0].to_sym
Bundler.require(platform)
Use only established gems in your projects. If you're contemplating on including some little-known gem you should do a careful review of its source code first.