Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active December 21, 2015 02:39
Show Gist options
  • Save jendiamond/6236409 to your computer and use it in GitHub Desktop.
Save jendiamond/6236409 to your computer and use it in GitHub Desktop.
get_started
%h2#getting-started
Getting Started
.contents
.bullet
.description
Getting started with bundler is easy! Open a terminal window and run this command:
:highlight_plain
$ gem install bundler
.bullet
.description
Specify your dependencies in a Gemfile in your project's root:
:highlight_ruby
source 'https://rubygems.org'
gem 'nokogiri'
gem 'rack', '~>1.1'
gem 'rspec', :require => 'spec'
= link_to 'Learn More: Gemfiles', '/v1.3/gemfile.html'
.bullet
.description
Install all of the required gems from your specified sources:
:highlight_plain
$ bundle install
$ git add Gemfile Gemfile.lock
= link_to 'Learn More: bundle install', '/v1.3/bundle_install.html'
.notes
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.
.bullet
.description
Inside your app, load up the bundled environment:
:highlight_ruby
require 'rubygems'
require 'bundler/setup'
# require your gems as usual
require 'nokogiri'
= link_to 'Learn More: Bundler.setup', '/v1.3/bundler_setup.html'
.bullet
.description
Run an executable that comes with a gem in your bundle:
:highlight_plain
$ bundle exec rspec spec/models
.notes
%p
In some cases, running executables without <code>bundle exec</code>
may work, if the executable happens to be installed in your system
and does not pull in any gems that conflict with your bundle.
%p
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.
.description
Finally, if you want a way to get a shortcut to gems in your bundle:
:highlight_plain
$ bundle install --binstubs
$ bin/rspec spec/models
.notes
The executables installed into <code>bin</code> are scoped to the
bundle, and will always work.
= link_to 'Learn More: Executables', '/v1.3/man/bundle-exec.1.html'
%h2#simple-workflow
A Simple Bundler Workflow
.contents
.bullet
.description
When you first create a Rails application, it already comes with a <code>Gemfile</code>. For another kind of application (such as Sinatra), run:
:highlight_plain
$ bundle init
%p
The <code>bundle init</code> command creates a simple <code>Gemfile</code> which you can edit.
.bullet
.description
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:
:hightlight_ruby
source 'http://rubygems.org;
gem 'sinatra', '~> 0.9.0'
gem 'rack-cache'
gem 'rack-bug'
.bullet
.description
If you don't have the gems installed in your system yet, run:
:hightlight_plain
$ bundle install
.bullet
.description
To update a gem's version requirements, first modify the Gemfile:
:highlight_ruby
source 'http://rubygems.org'
gem 'sinatra', '~> 1.0.0'
gem 'rack-cache'
gem 'rack-bug'
%p
and then run:
:highlight_plain
$ bundle install
.bullet
.description
If <code>bundle install</code> reports a conflict between your <code>Gemfile</code> and <code>Gemfile.lock</code>, run:
:highlight_plain
$ bundle update sinatra
%p
This will update just the Sinatra gem, as well as any of its dependencies
%p
To update all of the gems in your <code>Gemfile</code> to the latest possible versions, run:
:highlight_plain
$ bundle update
.bullet
.description
Whenever your <code>Gemfile.lock</code> 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.
.bullet
.description
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 <code>Gemfile.lock</code> to version control. On the remote server, run:
:highlight_plain
$ bundle install --deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment