Using Bundler to install Ruby gems

 

Overview

The following describes how to set up and use Bundler for your RubyGem applications.

Background

If your Ruby application won't start because of a missing gem, then you must install it locally using Bundler.

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are required in development, staging, and production.

Ruby

This article assumes you've already set up a Ruby on Rails site using RVM.

SSH

Make sure you have also created a shell user and are able to log into your server via SSH.

Setting up Bundler

  1. Log in to your server via SSH.
  2. Install Bundler:
    [server]$ gem install bundler
    
  3. Navigate to your project root directory.
  4. Specify your dependencies in a Gemfile:
    source 'https://rubygems.org'
    gem 'multi_json'
    gem 'rack'
    gem 'rspec', '~>3.7.0'
  5. Install all of the required gems from your specified sources. This adds the Gemfile and Gemfile.lock to your repository, which ensures that other developers on your app, as well as your deployment environment, all use the same third-party code that you are using now.
    [server]$ bundle install
    
  6. Inside your app, load up the bundled environment:

    This step is only necessary if you are not running RVM.

    require 'rubygems'
    require 'bundler/setup'
    
    # require your gems as usual
    require 'multi_json'
    
  7. Run an executable that comes with a gem in your bundle: 
    [server]$ 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 not recommended. Even if it looks like it works, it may not work in the future or on another machine.

  8. Create a shortcut to gems in your bundle:
    [server]$ bundle install --binstubs
    [server]$ bin/rspec spec/models
    

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

Creating a RubyGem with Bundler

Bundler is also an easy way to create new gems. Just like you might create a standard Rails project using rails new, you can create a standard gem project with bundle gem.

For example, the following creates a new gem with a README, .gemspec, Rakefile, directory structure, and the basic boilerplate you need to describe, test, and publish a gem:

[server]$ bundle gem my_gem

create  my_gem/Gemfile
      create  my_gem/.gitignore
      create  my_gem/lib/my_gem.rb
      create  my_gem/lib/my_gem/version.rb
      create  my_gem/my_gem.gemspec
      create  my_gem/Rakefile
      create  my_gem/README.md
      create  my_gem/bin/console
      create  my_gem/bin/setup
      create  my_gem/CODE_OF_CONDUCT.md
      create  my_gem/LICENSE.txt
      create  my_gem/.travis.yml
      create  my_gem/test/test_helper.rb
      create  my_gem/test/my_gem_test.rb
Initializing git repo in ./my_gem

Using Bundler with Ruby applications

See the following links for more information.

See also

DreamHost links

Third-party links 

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?