Bundler - manager for managing gems

    What is a bundler?

    This is a manager for managing gem dependencies in ruby ​​applications. This utility allows you to easily install the necessary gems for your application, while not depending on the installed system. If you used Rails for your development, then you will recall how you defined gem dependencies using config.gem in enviroment.rb, Bundler solves this problem much more convenient and simpler. It was included in Rails 3.0 by default and now, it is it that is used to manage gem dependencies in this version of the framework. This utility can be used for any ruby ​​framework.


    Install this utility like any other gem:

    gem install bundler

    For Rails 3.0, nothing else needs to be done, because it is used by default. And for Rails.2.3.x, you need to follow these steps. From the beginning, add the following code to boot.rb before Rails.boot !: Next, create the preinitializer.rb file in the config / initializers directory with the contents:

    class Rails::Boot
      def run

      def extend_environment
        Rails::Initializer.class_eval do
          old_load = instance_method(:load_environment)
          define_method(:load_environment) do
            Bundler.require :default, Rails.env

      # Require the preresolved locked set of gems.
      require File.expand_path('../../.bundle/environment', __FILE__)
    rescue LoadError
      # Fallback on doing the resolve at runtime.
      require "rubygems"
      require "bundler"


    After creating a new Rails application in version 3.0, a Gemfile file already exists in the project root, which is a config for Bundler. For versions of Rails 2.3.x, you need to create it yourself. To do this, go to the project directory and run the command:

    bundle init

    In this file, all the necessary gem dependencies are set. Consider exactly what features this config file provides.

    First, the resource is set from where gem will be installed by default:

    source 'http://gemcutter.org'

    As many already know, the gemcutter.org resource becomes a kind of standard for storing gems, so when creating a config, this resource will be installed by default. But you can easily replace it, say on gems.github.com or add as many resources as you need:

    source 'http://gemcutter.org'
    source 'http://gems.github.com'
    source 'http://gems.rubyforge.org'

    The following is a list of gems that are needed for the application to work: Here it should be noted that gems can be combined into groups and then only certain groups can be installed: Another form of combining gems into groups is also available: By default, all gems are turned on to the default group. If you need a specific version of gem, you can set its number: It is possible to specify the name of the file that will be connected during the connection of the Bundler library. By default, this is the name of the gem, so in most cases nothing needs to be specified. The option that allows you to specify the file name for the connection is called require, it is used in the following way: If you need to specify the git repository to download gem, then you need to use the git option:

    gem 'will_paginate'
    gem 'oauth'
    gem 'money'

    group :development do
      gem 'rspec'
      gem 'populator'
      gem 'faker'

    group :production do
      gem 'memcache-client'

    gem 'rspec', :group => 'development'
    gem 'populator', :group => 'development'
    gem 'memcache-client', :group => 'production'

    gem "rack", "1.0.1"
    gem "rails", ">=2.3.2"

    gem 'gchartrb', :require => 'google_chart'

    gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git'


    Once all the necessary gems have set the current, you need to run the command:

    bundle install

    This command will solve all the dependencies and install the missing gems. Moreover, if you execute:

    gem list

    Then you won’t see the gems that were installed using the bundler. All installed gems are located in the ~ / .bundler directory. Where did he connect them from.
    You can view the list of installed gems using the command:

    bundle show

    If the desired gem is already installed in the system, a link will be created for it. After each change to the Gemfile, you need to run the install command. The ~ / .bundler folder is optional, if you want to install gems in another folder, this is easy to do:

    bundle install ./vendor/bundler_gems

    If you remember, in the config file it is possible to include all gems in groups, and so during installation you can specify which groups of gems to not install:

    bundle install —without test

    Let's say why install gems on a production server that is needed only for testing.
    If the gem has executable files, you can run them as follows:

    bundle exec cassandra_helper cassandra

    Once you have finished developing the application, you need to block the Gemfile change:

    bundle lock

    After executing this command, the Gemfile.lock file will be created, which will contain all the dependencies based on the installed gem on your computer . This is done in order to fix the gem versions at which the application works correctly. This file will look like this:

        - :development
        version: ">= 0"
        - :test
        version: ">= 0"
        - :default
        version: ">= 0"
        - :default
        version: ">= 0"
    - stomp:
        version: 1.1.4
    - populator:
        version: 0.2.5
    - json:
        version: 1.2.0
    - thrift:
        version: 0.2.0
    - thrift_client:
        version: 0.3.3
    - rspec:
        version: 1.3.0
    - ruby-hmac:
        version: 0.4.0
    - oauth:
        version: 0.3.6
    hash: 0ac3c8666943a1e2294be2851316d83791479451
    - Rubygems:
        uri: gemcutter.org

    At the same time, if you change the Gemfile and try to run the install command, the installation will not be performed, since the gems are blocked. In order to install new gems already in a locked state, the install command is executed with the relockinstall stall --relock parameter, the command is executed and you need to block the change:

    bundle install --relock

    The bundler provides the ability to pack gems:

    bundle pack

    After running this command in the vendor / directory cache, all the necessary gems will be saved, after which the installation of gems will occur from this directory. This option will be useful only for those who do not have the ability to install gems from public repositories on the production server.

    That's all I wanted to tell you about this handy utility. Try to work with her for at least an hour and I think you will like her.

    Also popular now: