Skip to main content

How to Add a Gem to a Ruby Project with bundle add

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

bundle add is the one command for giving a Ruby project a new gem. It writes the gem into the Gemfile, installs it with everything it needs, and records the exact versions in Gemfile.lock, so the project gets the same gems on every computer.

Syntax​

Syntax
bundle add <gem> [--version "<requirement>"] [--pessimistic | --strict]
PartWhat it does
bundle addAdds a gem to the Gemfile in the folder you're in and installs it.
<gem>The gem's name on RubyGems.org, such as kramdown.
--version "<requirement>"Optional. A version requirement, such as "~> 2.6".
--pessimisticOptional. Writes ~>, which allows newer versions up to the next major one.
--strictOptional. Writes =, which allows exactly one version.

Without either option, Bundler 4 writes >=, which allows any newer version. Its own help says of the old --optimistic option: "Ignored (now default behavior)".

The Example​

The example adds kramdown, a gem that converts Markdown into HTML, and uses it to turn a heading into HTML.

Prerequisites​

  • Ruby, which comes with Bundler. This guide uses Ruby 4.0.10 and Bundler 4.1.0.

Step 1: Create the Project​

Terminal
mkdir markdown-demo
cd markdown-demo
bundle init

How to Create a Gemfile with bundle init explains the last command. These commands, like the rest, are the same in zsh, bash and PowerShell.

Step 2: Add the Gem​

Terminal
bundle add kramdown
Output
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Fetching gem metadata from https://rubygems.org/..
Fetching kramdown 2.6.1
Installing kramdown 2.6.1

Bundler looked the gem up on RubyGems.org, worked out what else it needs, and installed it. The Gemfile gained one line at the end:

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"

gem "kramdown", ">= 2.6"

>= 2.6 accepts 2.6 or anything newer. A new file, Gemfile.lock, records what was actually installed: kramdown 2.6.1, and rexml 3.4.4, which kramdown needs and Ruby already comes with. Commit both files.

Step 3: Use the Gem​

Create main.rb:

main.rb, which requires kramdown and prints the HTML for the Markdown heading # Hello from Ruby.

main.rb
require 'kramdown'

puts Kramdown::Document.new('# Hello from *Ruby*').to_html
Terminal
bundle exec ruby main.rb
Output
<h1 id="hello-from-ruby">Hello from <em>Ruby</em></h1>

A zsh terminal where bundle init writes a Gemfile, bundle add kramdown fetches and installs kramdown 2.6.1, and bundle exec ruby main.rb prints the HTML h1 for Hello from Ruby.

# Hello became an <h1> heading, and *Ruby* became <em>. How to Run a Ruby Script with bundle exec explains why the script runs through bundle exec.

Common Mistakes​

MistakeWhat happensDo this instead
Installing the gem with gem install insteadThe project doesn't record it, and bundle exec can't load itUse bundle add
Running bundle add outside the project folderBundler can't find a Gemfilecd into the project first
Expecting >= to protect you from breaking changesA later install can pick a new major versionUse --pessimistic to write ~> instead

Troubleshooting​

SymptomLikely causeFix
Could not find gem 'kramdwon' in rubygems repository https://rubygems.org/ or installed locally.The gem's name is misspelledCheck the name on RubyGems.org
cannot load such file -- bigdecimal (LoadError) when the code runsThe gem needs a former default gem it doesn't declarebundle add bigdecimal

FAQ​

How do I install every gem a project lists?​

Run bundle install in the project's folder. It installs the exact versions in Gemfile.lock.

How do I remove a gem?​

bundle remove <gem> deletes its line from the Gemfile and updates Gemfile.lock.

Conclusion​

bundle add <gem> writes the gem into the Gemfile, installs it with its dependencies and locks the versions in Gemfile.lock. Bundler 4 writes >= by default, and --pessimistic writes the stricter ~>.

Additional Resources​

  • bundle add, in Bundler's documentation.
  • Gemfile, including every way to write a version requirement.