Bundle Up

Vladilen Napuri
5 min readAug 21, 2020

Just as we have used our mouse and keyboard from the pretty much the first day we started learning about programming, we have begun every session in our terminal with our dear:

$ bundle install

As useful and sometimes simple as it may seem, I thought this tool deserved a quick rundown of some of its options and features.

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.

As stated in its documentation, Bundler manages an application’s dependencies. It does so in a repeatable way that is the same on many different machines. It ensures that whatever application your are working on has all the appropriate gems. To install Bundler:

$ gem install bundler

Gemfiles require at least one gem source, in the form of the URL for a RubyGems server. This file is located in your applications root folder. Generate a Gemfile with the default rubygems.org source by running:

$ bundle init

Now the Gemfile in itself has many options. What we are doing with bundle init is simply creating the bare minimum connection to the RubyGems url server where we can access the dependencies to these gems.

In the documentations you can learn how to fetch gems from a private server, require specifically named gems, use an unpacked gem directly from the file system, and group gems together, among many other uses.

Install

At this point you would go about actually installing the specified gems by running :

$ bundle install
$ git add Gemfile Gemfile.lock

There are many options while running your bundle install. Some of them are

--binstubs, --clean, --deployment

--frozen, --full-index, --gemfile--jobs

--local, --no-cache, --no-prune, --path

These options give you all sorts of control including, keeping your gem cache local, not allowing the Gemfile.lock to be updated after the installation, and not removing stale gems from your cache.

Bundler makes sure that you can use all of the gems in your Gemfile for your Ruby application. if it is a Sinatra application you must specify your load path with the first file that the application loads.

Update

Bundler takes care of ensuring that the application still functions regardless of attempting to generally updating all gems. You can update the gems version directly in the gemfile to specify a version, or you can update all the gems to their most recent versions by typing:

$ bundle update

One of the most useful aspects of using bundler, is how is manages the versions of dependancies. For example if you are running Program A version 2 , and Gem A Version 1. If Gem A gets an update and different version, bundler will not necessarily install the newest version unless it is necessary for Program A to function correctly.

Bundler documentation has many ways to specify how, and from where your gems are being updated.

Run

Now you can start to run executables that use a gem. You can be explicit:

$ bundle exec executable file/path

or you can just call on the exec file:

$ rspec 

This may not always work. If the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle then things will go ok, otherwise there may be errors in trying to find and use that executable and gem.

Now if you do want a shortcut for your gems you can install them into your bin file and they will always function.

$ bundle install --binstubs
$ bin/rspec spec/models

Now you should be able to call on Rspec no matter on what system you are using.

There are also many options in executing commands with bundler. The documentation informs us on how Rubygem Plugins work, how binstubs work, how to make changes to our shell environment, amongst lots of other powerful commands.

Create

Another pretty awesome tool that is accessible is the ability to create your own gems using bundler. There are many gems available for us to use, but there may be instances when creating our own gems may be more appropriate.

To begin creating a gem:

$ bundle -v
$ bundle gem gemname

Bundle -V will update the bundle to it’s latest version, the bundle gem command creates a scaffold directory for our new gem and, if we have Git installed, initializes a Git repository in this directory so we can start committing right away. 7 to 8 files or folders will be created for you, and you can begin to create your new gem. The docs explain exactly what each file is and how it relates to the new gem you are creating.

For one if we want to share the code it is more easily shared as a gem than sharing the entirety of the program that we have written. Instead if we create a gem separate from our library we can just have the library require the code.

Code for your package is placed within the lib directory. The convention is to have one Ruby file with the same name as your gem, since that gets loaded when require 'gemname' is run

Inside that lib file you can write your ruby code. It can be as simple as a hello world program or a sophisticated database gem.

All in all, there are many different options available to you when using Bundler. Yes it can simply be called upon to ensure that we are using the appropriate version and dependencies of our gems, but it also allows us to have extreme flexibility in the way that we are creating and sharing our new software.

SOURCES:

--

--