TExcelTAD
Home › Ruby › Ruby Gems & Bundler

Ruby Gems & Bundler

A gem is a packaged Ruby library — code someone else wrote and published so you can reuse it. There are more than 180,000 gems on rubygems.org: web frameworks, database drivers, HTTP clients, testing tools, PDF generators and much more. Two tools manage them:

  • RubyGems (gem command) installs and publishes individual gems.
  • Bundler (bundle command) manages the exact set of gems a project needs.

Both come with Ruby. The terminal sessions below show what you would see on your own machine (they can't run inside the browser playground).

Installing a gem with gem

bash · try it yourself
gem install colorize
Fetching colorize-1.1.0.gem
Successfully installed colorize-1.1.0
1 gem installed

After installation, load it with require and use it:

ruby
require "colorize"

puts "Success!".green
puts "Warning".yellow.bold

Other useful gem commands:

Command Purpose
gem install NAME install the latest version
gem install NAME -v 2.3.1 install a specific version
gem list list installed gems
gem update NAME upgrade a gem
gem uninstall NAME remove a gem
gem info NAME show details about an installed gem

The standard library and default gems

Many libraries ship with Ruby itself, so you only need require — no installation. You have already used several in this course:

ruby · try it yourself
require "json"
require "set"
require "date"
require "securerandom"

p JSON.generate({ok: true})
p Set[3, 1, 3].size
p Date.new(2025, 12, 24).strftime("%A")
p SecureRandom.respond_to?(:uuid)
"{\"ok\":true}"
2
"Wednesday"
true
Library Use
json read/write JSON
csv read/write CSV files
date, time dates and times
set collections of unique values
fileutils copy, move, delete files and folders
net/http, uri HTTP requests
securerandom random tokens and UUIDs
minitest unit testing (bundled gem)
benchmark timing code

Why Bundler?

Imagine a project that uses 30 gems. Each gem depends on other gems, with version requirements. Different developers, CI servers and production machines must all use exactly the same versions, or "it works on my machine" bugs appear. Bundler solves this with two files:

  • Gemfile — the gems you ask for (written by you).
  • Gemfile.lock — the exact versions Bundler resolved, including every dependency (generated; commit it to git).

A Gemfile

ruby
# Gemfile
source "https://rubygems.org"

ruby "3.4.7"

gem "sinatra", "~> 4.1"        # web framework
gem "puma"                     # web server
gem "sequel", ">= 5.80"        # database toolkit

group :development, :test do
  gem "minitest", "~> 5.25"
  gem "rubocop", require: false
end

The pessimistic operator ~> is the most common version constraint:

Constraint Allows
"2.1.0" exactly 2.1.0
">= 2.1" 2.1 or anything newer
"~> 2.1" ≥ 2.1 and < 3.0
"~> 2.1.4" ≥ 2.1.4 and < 2.2

bundle install

bash · try it yourself
bundle install
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Fetching rack 3.1.12
Installing rack 3.1.12
Fetching sinatra 4.1.1
Installing sinatra 4.1.1
Installing puma 6.6.0 with native extensions
Installing sequel 5.90.0
Bundle complete! 5 Gemfile dependencies, 14 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Bundler reads the Gemfile, resolves compatible versions, installs them and writes Gemfile.lock. Anyone who clones the project and runs bundle install gets the identical versions from the lock file.

Running code with the bundle

bash · try it yourself
bundle exec ruby app.rb
== Sinatra (v4.1.1) has taken the stage on 4567 for development with backup from Puma

bundle exec runs a command using exactly the gem versions in Gemfile.lock. In a script you can achieve the same with:

ruby
require "bundler/setup"   # use the versions from Gemfile.lock
require "sinatra"

Everyday Bundler commands

Command Purpose
bundle init create an empty Gemfile
bundle add NAME add a gem to the Gemfile and install it
bundle install install everything in the Gemfile
bundle update NAME upgrade one gem (and update the lock file)
bundle outdated list gems with newer versions available
bundle exec CMD run CMD with the bundle's gems
bundle info NAME where a gem lives, its version and summary

Popular gems to know

Gem What it does
rails full-stack web framework
sinatra / roda lightweight web frameworks
puma web server
pg, sqlite3 database drivers
sidekiq background jobs
rspec, minitest testing
rubocop linter and formatter
faraday HTTP client
nokogiri HTML/XML parsing
dry-rb, zeitwerk application building blocks, code loading

Creating your own gem

bash · try it yourself
bundle gem greeting_tools
Creating gem 'greeting_tools'...
      create  greeting_tools/Gemfile
      create  greeting_tools/lib/greeting_tools.rb
      create  greeting_tools/lib/greeting_tools/version.rb
      create  greeting_tools/greeting_tools.gemspec
      create  greeting_tools/Rakefile
      create  greeting_tools/README.md
      create  greeting_tools/test/test_greeting_tools.rb
Gem 'greeting_tools' was successfully created.

The .gemspec file describes the gem (name, version, author, files, dependencies). gem build packages it and gem push publishes it to rubygems.org.

Tip: Before adding a gem, check how actively it is maintained (recent releases, open issues) and whether the standard library already does the job. Every dependency is code you'll have to keep up to date.

Test yourself

Which file records the exact versions of every gem a project uses?

  1. Gemfile
  2. Gemfile.lock
  3. .gemspec
  4. Rakefile

What does gem "rack", "~> 3.1" allow?

  1. Only 3.1.0
  2. 3.1 up to (not including) 4.0
  3. Any version
  4. Versions below 3.1

Why run bundle exec rspec instead of just rspec?

  1. It is faster
  2. It uses the gem versions locked for this project
  3. It installs rspec
  4. It runs tests in parallel