RoR Gems and Bundler
Gems are Ruby packages — reusable libraries that add features to your application. Instead of writing authentication, payment processing, or image handling from scratch, you install a gem written by the community and start using it immediately. Bundler manages which gems your project uses and ensures every developer and server runs the exact same versions.
What a Gem Is
Gem = A packaged Ruby library
+ its dependencies
+ its version number
+ documentation
Examples:
devise ← user authentication
sidekiq ← background jobs
pundit ← authorization
pagy ← pagination
kaminari ← pagination (alternative)
ransack ← search forms
carrierwave ← file uploads
money-rails ← currency handling
geocoder ← location / mapping
The Gemfile
Every Rails app has a Gemfile that lists all gems the app uses:
Gemfile source "https://rubygems.org" ruby "3.2.2" gem "rails", "~> 7.1.0" gem "pg", "~> 1.5" gem "puma", "~> 6.0" gem "sidekiq", "~> 7.0" gem "devise" gem "pundit" gem "pagy" gem "image_processing", "~> 1.2" gem "httparty" group :development, :test do gem "rspec-rails" gem "factory_bot_rails" gem "faker" gem "bullet" end group :development do gem "letter_opener" gem "rack-mini-profiler" end group :test do gem "capybara" gem "selenium-webdriver" gem "shoulda-matchers" end
Version Specifiers
gem "rails", "7.1.0" ← exact version only gem "rails", "~> 7.1.0" ← 7.1.x allowed, not 7.2 gem "rails", "~> 7.1" ← 7.x allowed, not 8.0 gem "rails", ">= 7.0" ← 7.0 or higher gem "rails", ">= 7.0", "< 8.0" ← 7.x only gem "devise" ← no version constraint (uses latest)
Use ~> (the pessimistic constraint) in production apps. It allows patch releases with bug fixes but prevents major version upgrades that might break your app.
Bundler — The Dependency Manager
Bundler reads your Gemfile, resolves compatible versions for all gems and their dependencies, downloads them, and records the exact versions in Gemfile.lock.
Install all gems in the Gemfile: bundle install Update all gems to their latest allowed versions: bundle update Update one gem: bundle update devise Run any command using the gems in Gemfile.lock: bundle exec rails server bundle exec rspec bundle exec rake db:migrate
Gemfile.lock — The Exact Version Record
Gemfile:
gem "devise" ← "any version"
Gemfile.lock (auto-generated):
devise (4.9.3)
bcrypt (~> 3.1.7)
railties (>= 6.0.0)
bcrypt (3.1.18)
The lock file pins exact versions so every developer and server
uses devise 4.9.3 — not 4.9.2 or 4.10.0.
Always commit Gemfile.lock to version control.
Groups — Load Gems Only Where Needed
Gems in groups only load in the specified environment:
group :development, :test do gem "rspec-rails" ← never loads in production end group :production do gem "pg" ← only loads in production end gem "sidekiq" ← no group = loads in all environments
During deployment, skip development and test gems:
bundle install --without development test
Finding Gems
rubygems.org ← official gem registry (search by name) the-ruby-toolbox.com ← curated gem rankings by category github.com ← most popular gems are on GitHub with docs Search for gems: gem search "image processing" gem search "authentication"
Essential Rails Gems by Category
| Category | Gem | Purpose |
|---|---|---|
| Authentication | devise | Full user auth system |
| Authorization | pundit | Policy-based permissions |
| Background Jobs | sidekiq | Redis-backed job processing |
| Pagination | pagy | Fast, lightweight pagination |
| Search | ransack | Search form builder |
| File Uploads | image_processing | Image variants for ActiveStorage |
| HTTP Client | httparty | Simple HTTP requests |
| PDF Generation | prawn | Generate PDF files |
| Excel | caxlsx | Generate Excel files |
| Admin Panel | activeadmin | Auto-generate admin interface |
| Money | money-rails | Currency storage and formatting |
| Testing | rspec-rails | Testing framework |
Reading a Gem's Documentation
Every well-maintained gem has a README on GitHub. The README shows installation steps, configuration, and usage examples. Always read the README before using a gem. Check the gem's GitHub issues to see if there are known bugs or version incompatibilities with your Rails version.
Keeping Gems Updated
Check which gems have updates available: bundle outdated Update all gems: bundle update Update one gem safely: bundle update pagy After updating: Run your test suite: bundle exec rspec Fix any broken tests before deploying
Gems accelerate Rails development dramatically. The ecosystem has gems for almost every common task. Use well-maintained gems with active GitHub repositories, many stars, and recent commits. Avoid gems with no commits in over two years.
