Let us begin with a basic Hello World application. First, I create a hello-world project directory with a lib/ folder (standard Ruby convention). Next, I create a hello_world.rb file in the lib/ directory (I am ignoring the spec/ folder and not including any tests, for simplicity).

$ mkdir hello-world
$ cd hello-world/
$ mkdir lib
$ echo puts \"Hello, world\!\" >> lib/hello_world.rb
$ ruby hello_world.rb

> Hello, world!

Now, I want my application to print “Hello, world!” in a nice ruby red color. To do this, I can either look up how to manually add a color to my string, or look for a Ruby Gem (commonly referred to as simply “Gem”) that someone has already created in order to make the process of adding colors to strings much easier.

But what is this magic? RubyGems is a package manager for Ruby. People can create their own applications/libraries to be re-used by other people in other applications. This post will only cover how to use and bundle other people’s gems into your project, but if you’d like to create your own gems, there are guides on the official RubyGems website (https://guides.rubygems.org/make-your-own-gem/).

So, back to the hello-world application: I found a Gem that seemingly solves the problem of adding color to a printed string, called colorize (https://rubygems.org/gems/colorize/versions/0.8.1). Installing colorize and using it in our application is fairly straightforward. A simple gem install colorize will install the colorize gem on your computer. If you experience a permission error, you can override your permissions by using sudo gem install colorize or setting up a Ruby development environment manager such as rbenv (I also wrote a tutorial about setting up rbenv: https://medium.com/@gratefulcheddar/installing-rbenv-and-updating-to-the-newest-ruby-version-22c465063450).

After the colorize gem is successfully installed, to import it into the project we just add the line require 'colorize' at the top of hello_world.rb. Then we’re able to use the gem in our application. The following code is the final syntax of the hello_world.rb program:

require 'colorize'
puts "Hello, world!".colorize(:red)

Now we can share our application with others. But wait, there’s a problem! If we share our program with another person who doesn’t have the colorize gem installed, they’ll get an error:

> in `require': cannot load such file -- colorize (LoadError)

One solution is to have the other person or people manually install the colorize gem on their computer. This solution doesn’t seem too bad in our current context. But in larger applications with many gem dependencies across many different files, this is not a viable solution. If only there was some way to bundle all the gems in our application into a single location that can be used to install them all.

The last sentence just happens to describe bundler (https://bundler.io). According to Bundler’s help command, it “manages an application’s dependencies through its entire life across many machines systematically and repeatably”. Briefly, it allows you to save/add project dependencies to a file called Gemfile, and install all of these dependencies with a single command.

Bundler itself is a RubyGem so it can be installed by running the gem install bundler (if you have permissions errors, see the previous note about installing colorize).

Then, to initialize a brand new Gemfile for your project, in the root folder of your application (hello_world/ in this example) issue the command bundle init:

$ bundle init
> Writing new Gemfile to /Users/tim/ruby_test_examples/hello-world/Gemfile

The new, blank Gemfile should look like this:

# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"

Then, to add each gem/dependency, use the bundle add <gem_name> command:

$ bundle add colorize
> Fetching gem metadata from https://rubygems.org/.............
> Resolving dependencies...
> Fetching gem metadata from https://rubygems.org/.............
> Resolving dependencies...
> Using bundler 1.16.2
> Using colorize 0.8.1

Now, the Gemfile should have this line added to the end:

gem "colorize", "~> 0.8.1"

Now that all of our application dependencies are bundled within the single Gemfile, when we distribute our application, the recipient simply has to run bundle install and they’ll see the following:

$ bundle install
> Fetching gem metadata from https://rubygems.org/.............
> Using bundler 1.16.2
> Fetching colorize 0.8.1
> Installing colorize 0.8.1
> Bundle complete! 1 Gemfile dependency, 2 gems now installed.
> Use `bundle info [gemname]` to see where a bundled gem is installed.

Of course, for our little example application, this seems like a lot of extra work for very little reward. For existing projects that depend on specific (often older) versions of specific gems, this tool is extremely valuable.

For more information about RubyGems, check out https://rubygems.org.
For more information about Bundler, check out https://bundler.io.
For more information about the colorize gem, check out https://rubygems.org/gems/colorize and https://github.com/fazibear/colorize.