LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1118 min read

Gems & Bundler

Learn RubyGems for installing third-party libraries, and Bundler and the Gemfile for managing reproducible per-project dependencies.

Introduction

A real Ruby project rarely relies purely on the standard library — RubyGems and Bundler together are how the Ruby ecosystem shares, installs, and reliably locks the third-party code (gems) a project depends on, including the Rails framework itself.

What You Will Learn
  • What a "gem" actually is, and how to install and use one.
  • Why real projects need per-project dependency isolation, not just globally installed gems.
  • How Bundler and a Gemfile declare and manage a project's dependencies.
  • What Gemfile.lock does and why it matters for reproducible installs.

What a Gem Actually Is

A gem is a packaged, distributable Ruby library — code someone else wrote, tested, and published to RubyGems.org so anyone can install and use it with a single command, similar in role to a package on npm or PyPI.

Installing and Requiring a Gem

gem install colorize
require "colorize"
puts "Success!".colorize(:green)
puts "Warning!".colorize(:yellow)
Terminal Output

Click Run to see what this code prints.

require "colorize" loads the gem's code into your script, making the methods it adds (like .colorize on String) available to use.

Why Per-Project Dependencies Matter

Installing gems globally, as shown above, works for quick experiments, but real projects run into a familiar problem: Project A needs version 1.0 of a gem, Project B needs version 2.0, and a single global install can't satisfy both at once. Bundler solves exactly this problem.

Bundler and the Gemfile

Use case: a Gemfile, placed in a project's root, declares exactly which gems (and which versions) that specific project needs — the Ruby equivalent of package.json in a Node project or requirements.txt in Python.

# Gemfile
source "https://rubygems.org"
gem "colorize", "~> 1.1"
gem "httparty", "~> 0.21"

bundle install and Gemfile.lock

Running bundle install reads the Gemfile, resolves compatible versions for every dependency (including dependencies of dependencies), installs them, and writes the exact resolved versions to Gemfile.lock — which should be committed to version control so every developer and every deployment installs the identical dependency versions.

bundle install
Terminal Output

Click Run to see what this code prints.

Why Commit Gemfile.lock

Without Gemfile.lock, two different machines running bundle install against the same loosely-versioned Gemfile could resolve slightly different gem versions — committing the lock file guarantees everyone, including your production server, uses the exact same versions you tested with.

Common Mistakes

Avoid These Mistakes
  • Relying only on globally installed gems (gem install) for a real project instead of a Gemfile, risking version conflicts across projects.
  • Forgetting to commit Gemfile.lock to version control, losing the exact reproducible version guarantee it provides.
  • Pinning every gem version too rigidly (with = instead of ~>), making routine security and bug-fix updates harder to receive.

Best Practices

  • Use a Gemfile and Bundler for any project beyond a single throwaway script.
  • Commit Gemfile.lock alongside your code so every environment installs identical dependency versions.
  • Use the ~> "pessimistic" version constraint to allow safe patch/minor updates while avoiding unexpected major-version breaks.

Frequently Asked Questions

No — gem install and require work fine for quick scripts and experiments. Bundler becomes valuable once a project has multiple dependencies and needs reproducible, per-project version management.

Yes — every Rails application is generated with a Gemfile and relies on Bundler to manage Rails itself plus every other gem the application depends on.

It means "allow updates that don't change the specified precision" — "~> 1.1" allows 1.2, 1.3, and so on, but not 2.0, giving you safe updates without unexpected breaking changes.

Key Takeaways

  • A gem is a packaged, reusable Ruby library, installed with gem install and loaded with require.
  • Global gem installs can conflict across projects with different version needs.
  • A Gemfile declares a project's exact dependencies; bundle install resolves and installs them.
  • Gemfile.lock records the exact resolved versions and should always be committed to version control.

Summary

RubyGems and Bundler are what let the Ruby ecosystem — including Rails itself — share and reliably reuse code across countless projects. In the final lesson, you'll get an introduction to Ruby on Rails and a consolidated best-practices recap across the whole course.

Next Lesson →

Ruby in the Real World: Rails & Best Practices