What is it?
Bundler is a Ruby gem that helps you make sure you have all of the gems your application needs, no matter what environment you are working in. In short, it tracks your application dependencies for you.
Why do I need it?
Bundler has made handling your gems easy. You use bundler to save you time when running your application on multiple servers (development, staging-testing, production).
How do I use it?
Bundler will use what is called a Gemfile that it stores in your document root. This file is where you list all of the gems your file will need. It must always start with a source location (of which you can declare more than one). Also notice that if your require statement is different than your gem name you need to include the :require declaration.
source "http://rubygems.org"
gem "rails", "~> 3.1.0"
gem "rack-cache", :require => "rack/cache"
gem 'authlogic'
gem 'ruby-debug19'
gem 'resque'
gem 'carrierwave'
gem 'rmagick'
You will then need to tell bundler to sync your gems.
bundle install
After you run ‘bundle install’ you will notice a new file called ‘Gemfile.lock’. This is a snapshot of all the gems your application is set to use. This file is used to rebuild your gem dependency tree.
Bundler with Sinatra:
Bundler works great with Rails, it also works with any other Ruby development tool. Sinatra is a good example of something that you will want to be able to use Bundler with. You have to actually tell the Sinatra application that you want Bundler to be involved.
# Running at the command line in the root of your application.
# Creates the Gemfile.
bundle init
# This is required at the top of your application.
require "rubygems"
require "bundler/setup"
# Require all of the gemsets.
Bundle.require(:default)
# Running at the command line in the root of your application.
# Brings in the gem files you need.
bundle install
Grouping:
Keep in mind that you can group together your gems in order to fit your environment. This will help you separate what each environment is meant to do. You do not always want to share the same gem packages with your production environment that is in your development environment.
group :development do
gem "rails", "~> 3.1.0"
gem "rack-cache", :require => "rack/cache"
gem 'authlogic'
gem 'ruby-debug19'
end
group :production do
gem "rails", "~> 3.1.0"
gem "rack-cache", :require => "rack/cache"
gem 'authlogic'
gem 'ruby-debug19'
gem 'resque'
gem 'carrierwave'
gem 'rmagick'
end
That is what I have come to understand about Bundler. As with everything else you will want to familiarize yourself with the documentation on this gem. As always if you have something else to add or notice a problem with this post, feel free to make a comment. I don’t know everything…