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:
old_load = instance_method(:load_environment)
Bundler.require :default, Rails.env
# Require the preresolved locked set of gems.
require File.expand_path('../../.bundle/environment', __FILE__)
# Fallback on doing the resolve at runtime.
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:
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:
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:
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:
group :development do
group :production do
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:
This command will solve all the dependencies and install the missing gems. Moreover, if you execute:
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:
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:
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:
version: ">= 0"
version: ">= 0"
version: ">= 0"
version: ">= 0"
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:
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.