Gems & Bundler
Ruby is a dynamic, beginner-friendly programming language with a famous ecosystem of gems — packaged libraries you can install and require — managed reliably with Bundler.
Learn Gems & Bundler in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll install and require gems, write a Gemfile, and understand how Bundler locks versions for reproducible projects.
What You'll Learn in This Lesson
1️⃣ Installing and Using Gems
Install a gem from your terminal with gem install name , then load it in code with require . Ruby's standard library already includes many useful libraries — like json — that you can require without installing anything.
2️⃣ Bundler: A Gemfile for Reproducible Projects
Real projects declare their gems in a Gemfile . Running bundle install resolves versions and writes a Gemfile.lock , so everyone installs the same versions. Run your app with bundle exec to use exactly those gems.
Your turn. Use the json gem to serialise and parse settings. Confirm the two TODO lines, then run it.
Mini-Challenge: Write a Gemfile
Author a real Gemfile with a source, a pinned gem, and a test-group gem — then note the commands you'd run. This is the file at the heart of every Ruby project.
Common Errors (and the fix)
- "cannot load such file -- xyz" (LoadError) — The gem isn't installed or isn't required. Run gem install xyz and add require "xyz" .
- Version conflicts — Two gems need incompatible versions. Let Bundler resolve them via bundle install and read its message.
- Forgetting bundle exec — Running ruby app.rb may pick a wrong global gem; use bundle exec .
- Not committing Gemfile.lock — Without it, teammates get different versions. Commit it for applications.
- Permission errors on install — Avoid sudo ; use a version manager (rbenv/RVM) so gems install in your user space.
Pro Tips
- 💡 Browse gems: search rubygems.org and check downloads and last-updated dates before depending on a gem.
- 💡 Pin with ~>: it permits safe updates while blocking breaking major versions.
- 💡 Groups matter: put dev/test gems in groups so production stays lean.
📋 Quick Reference — Gems & Bundler
Concept
Ruby Syntax
Install a gem
gem install httparty
Load in code
require "httparty"
Declare
gem "rspec", "~> 3.0"
Install set
bundle install
Run via Bundler
bundle exec ruby app.rb
List gems
gem list
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ A gem is a packaged Ruby library from RubyGems.org
- ✅ Install with gem install , load with require
- ✅ A Gemfile declares a project's gems
- ✅ bundle install locks versions in Gemfile.lock
- ✅ ~> allows safe upgrades while blocking breaking ones
- ✅ Next lesson: Intro to Rails — the web framework
Practice quiz
What is a gem?
- A Ruby keyword
- A packaged, shareable Ruby library
- A type of variable
- A built-in data structure
Answer: A packaged, shareable Ruby library. A gem is a packaged Ruby library with code plus metadata, published to RubyGems.org.
Which command installs a gem globally from the terminal?
- bundle install
- gem install name
- require name
- gem load name
Answer: gem install name. gem install name fetches and installs a single gem globally on your system.
How do you load an installed gem in your code?
- import gem
- include gem
- require "name"
- load gem
Answer: require "name". You load a gem (or standard-library module) in code with require "name".
What file declares a project's gems for Bundler?
- Gemfile
- package.json
- gems.txt
- Rakefile
Answer: Gemfile. A file literally named Gemfile lists the gems a project needs.
What does write to lock exact versions?
- Gemfile
- Gemfile.lock
- versions.rb
- lockfile.json
Answer: Gemfile.lock. bundle install resolves versions and records the exact ones in Gemfile.lock.
What does the version constraint mean?
- Exactly this version
- Any version
- Pessimistic: safe minor/patch upgrades, no breaking major bump
- Less than this version
Answer: Pessimistic: safe minor/patch upgrades, no breaking major bump. ~> is the pessimistic operator; ~> 0.21 means at least 0.21 but less than 1.0.
Why run your app with ?
- It is faster
- It ensures the exact gems from the Gemfile/lock are used, not some global version
- It installs gems
- It compiles the app
Answer: It ensures the exact gems from the Gemfile/lock are used, not some global version. bundle exec runs your app with exactly the gems Bundler resolved, avoiding wrong global versions.
Which library ships with Ruby and needs no install before ?
- httparty
- rails
- json
- rspec
Answer: json. json is part of Ruby's standard library, so you can require it without installing anything.
What error appears when a required gem isn't installed?
- SyntaxError
- LoadError (cannot load such file)
- ArgumentError
- NameError
Answer: LoadError (cannot load such file). Requiring a missing gem raises LoadError: 'cannot load such file'.
What is the difference between gem install and bundle install?
- No difference
- gem install adds one gem globally; bundle install installs the whole Gemfile set with locked versions
- gem install needs a Gemfile
- bundle install installs only one gem
Answer: gem install adds one gem globally; bundle install installs the whole Gemfile set with locked versions. gem install is for single global tools; bundle install installs a project's whole declared, version-locked set.
Continue this course
- Previous: Metaprogramming Intro
- Next: Intro to Rails