Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active December 21, 2015 01:19
Show Gist options
  • Save jendiamond/6226837 to your computer and use it in GitHub Desktop.
Save jendiamond/6226837 to your computer and use it in GitHub Desktop.
Bundler - Getting Started

Getting Started

Getting started with bundler is easy! Open a terminal window and run this command:

$ gem install bundler

Specify your dependencies in a Gemfile in your project's root: source 'https://rubygems.org' gem 'nokogiri' gem 'rack', '~>1.1' gem 'rspec', :require => 'spec'

Learn More: Gemfiles

Install all of the required gems from your specified sources:

$ bundle install
$ git add Gemfile Gemfile.lock

Learn More: bundle install

The second command adds the Gemfile and Gemfile.lock to your repository. This ensures that other developers on your app, as well as your deployment environment, will all use the same third-party code that you are using now.

Inside your app, load up the bundled environment:

require 'rubygems'
require 'bundler/setup'

# require your gems as usual
require 'nokogiri'

Learn More: Bundler.setup

Run an executable that comes with a gem in your bundle:

$ bundle exec rspec spec/models

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

Finally, if you want a way to get a shortcut to gems in your bundle:

$ bundle install --binstubs
$ bin/rspec spec/models

The executables installed into bin are scoped to the bundle, and will always work. Learn More: Executables

A Simple Bundler Workflow

When you first create a Rails application, it already comes with a Gemfile. For another kind of application (such as Sinatra), run:

$ bundle init

The bundle init command creates a simple Gemfile which you can edit.

Next, add any gems that your application depends on. If you care which version of a particular gem that you need, be sure to include an appropriate version restriction:

source 'http://rubygems.org'

gem 'sinatra', '~> 0.9.0'
gem 'rack-cache'
gem 'rack-bug'

If you don't have the gems installed in your system yet, run:

$ bundle install

To update a gem's version requirements, first modify the Gemfile:

source 'http://rubygems.org'

gem 'sinatra', '~> 1.0.0'
gem 'rack-cache'
gem 'rack-bug'

and then run:

$ bundle install

If bundle install reports a conflict between your Gemfile and Gemfile.lock, run:

$ bundle update sinatra

This will update just the Sinatra gem, as well as any of its dependencies

To update all of the gems in your Gemfile to the latest possible versions, run:

$ bundle update

Whenever your Gemfile.lock changes, always check it in to version control. It keeps a history of the exact versions of all third-party code that you used to successfully run your application. When deploying your code to a staging or production server, first run your tests (or boot your local development server), make sure you have checked in your Gemfile.lock to version control. On the remote server, run:

$ bundle install --deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment