HomeWeb DevelopmentRuby Dependency Management with Bundler

Ruby Dependency Management with Bundler

Managing dependencies is a difficult task in any system. Bundler makes it almost enjoyable for Ruby!

Bundler is a dependency manager for Ruby. Projects that are cognizant of dependency management are more favorable than those that rely on manual management. Bundler creates project (or folder) specific bundles of gems with specific versions for that project’s needs. Luckily for us, it is easy to use.

You may want to understand Ruby Version Manager before proceeding.

Installing Bundler

You will need to have a managed version of ruby on your machine before installing.

Verify you have Rubygems installed. Follow the installation instructions if it is not installed.

$ gem -v

Install bundler using rubygems.

$ gem install bundler
Fetching source index from https://rubygems.org/
Installing rake 10.3.0
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

Verify that bundler is installed.

$ bundle -v
Bundler version 1.6.2

Using Bundler

Bundler acts as a wrapper when Ruby commands are executed that includes the correct gem versions. Bundle has a few commands you should understand. These are typically executed from a projects directory. A typical use case would be to clone a repository with a Gemfile in its root directory, followed by executing these commands from that directory.

Generate the bundle by downloading the appropriate gems. This command is initiated from any directory that contains a Gemfile. Read the Docs

$ bundle install

Bundler works with Gemfile.lock file that stores the dependency graph for the project. If this file is committed to a project, it locks each gem in the dependency graph to that version. If it does not exist when bundle install is run, the latest versions are installed.

Ruby commands are instructed to use a bundle with the exec command. The following example tells foo to execute with the correct bundle. Read the Docs

$ bundle exec foo

Updating the bundle is straight forward. The bundle update command tells bundler to ignore the contents of the Gemfile.lock and get the latest versions. Read the Docs

$ bundle update