Bundler & Gemfile
This course explains how Bundler manages the gems a Rails app depends on. It covers how the Gemfile declares gems with constraints such as ~> and >=, and how groups limit gems to certain environments. It shows how Bundler resolves every constraint at once and records the exact versions in Gemfile.lock. It explains what bundle install does when the lock matches and when the Gemfile changes. It also covers why bin/rails loads locked versions through bundler/setup and when plain Ruby scripts need bundle exec.
What you'll learn
- ✓How the ~> and >= constraints in a Gemfile limit which versions Bundler may pick
- ✓How Gemfile groups restrict gems to specific environments
- ✓What the four sections of Gemfile.lock record and why the file is committed to Git
- ✓What bundle install and bundle check do when the lock matches or the Gemfile changes
- ✓Why bin/rails never needs bundle exec and when other scripts do
- ✓How to inspect installed and locked gems with gem list and bundle info
What Bundler does
The Rails CLI: rails new course ended its setup lesson with this: "Gemfile and Gemfile.lock already exist when bin/setup runs bundle install. The lock file records the exact resolved gem versions. How that works, and how to manage gems on purpose, is for Bundler & Gemfile." This is that course.
Read the full lesson free →Curriculum
7 lessonsWhat Bundler does
Why 42 declared gems become 160 installed, and how Bundler resolves constraints and locks the answer.
The Gemfile: declaring what you depend on
How the Gemfile declares gems, what the ~> and >= constraints allow, and how groups limit environments.
Gemfile.lock: the resolved snapshot
The four sections of Gemfile.lock, why hand-editing it breaks the solved set, and why it is committed.
bundle install: resolving and fetching
What bundle install does when the lock matches or the Gemfile changes, and why bin/setup runs bundle check first.
bundle exec: why bin/rails never needs it
How bundler/setup in config/boot.rb pins locked versions for bin/rails, and why plain scripts need bundle exec.
Checking Bundler in this project
Using gem list, bundle check, bundle install and bundle info to inspect installed and locked gems in this project.
Putting it together: from Gemfile to running app
Traces the chain from a Gemfile line to a loaded gem and lists topics this course leaves out.