Skip to main content

How to Run a Ruby Script with bundle exec

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A computer can have several versions of the same gem installed, and a Ruby script loads whichever it finds first. bundle exec runs a command with exactly the gems and versions the project's Gemfile.lock records, so the script behaves the same on every computer.

Syntax​

Syntax
bundle exec <command>
PartWhat it does
bundle execRuns the command with the project's gems, at the versions in Gemfile.lock, and only those.
<command>The command to run, such as ruby main.rb, rake test or rails server.

The Example​

The example turns a line of Markdown into HTML with kramdown, a gem that converts Markdown, the plain-text format behind README files, 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 bold-demo
cd bold-demo
bundle init
bundle add kramdown

How to Create a Gemfile with bundle init and How to Add a Gem to a Ruby Project with bundle add explain these two commands. The project now has a Gemfile and a Gemfile.lock that pins kramdown's exact version:

Gemfile.lock (excerpt)
GEM
remote: https://rubygems.org/
specs:
kramdown (2.6.1)
rexml (>= 3.4.4)
rexml (3.4.4)

Step 2: Write the Script​

Create main.rb:

main.rb, which requires kramdown and prints the HTML for the Markdown text Bundler ran this script.

main.rb
require 'kramdown'

puts Kramdown::Document.new('**Bundler** ran this script.').to_html

Step 3: Run It with bundle exec​

Terminal
bundle exec ruby main.rb
Output
<p><strong>Bundler</strong> ran this script.</p>

A zsh terminal where bundle exec ruby main.rb prints the HTML paragraph with Bundler in strong tags.

The command is the same in zsh, bash and PowerShell. require 'kramdown' loaded kramdown 2.6.1, the version Gemfile.lock names, and the double asterisks became <strong>.

What bundle exec Changes​

Without it, ruby main.rb loads whatever versions of the gems it finds installed. Usually that works, but when another project installed a newer or older kramdown, the script can get that one instead. bundle exec removes the guesswork in two ways:

  • It picks the locked versions. Every gem loads at the version in Gemfile.lock, the same on every computer that runs the project.
  • It hides gems the project doesn't list. A require of a gem that isn't in the Gemfile fails at once, so a missing entry shows up on your computer before it shows up on someone else's.

Common Mistakes​

MistakeWhat happensDo this instead
Running bundle exec outside the project folderBundler can't find the Gemfilecd into the project first
Leaving out bundle exec when several versions of a gem are installedThe script may load a version the project didn't lockAlways run project code with bundle exec
Expecting bundle exec to install missing gemsIt only runs commandsRun bundle install first

Troubleshooting​

SymptomLikely causeFix
Could not locate GemfileYou're not in the project's foldercd into the folder with the Gemfile
cannot load such file -- bigdecimal (LoadError)A gem needs a former default gem that the Gemfile doesn't listAdd it with bundle add bigdecimal

FAQ​

Do I need bundle exec for every command?​

For commands that load the project's gems, such as ruby, rake or rspec, yes. Bundler's own commands, such as bundle add, don't need it.

What about the bin/ files Rails apps have?​

Those are binstubs: small scripts that set up Bundler before running the command, so bin/rails behaves like bundle exec rails.

Conclusion​

bundle exec <command> runs a command with exactly the gems and versions in Gemfile.lock, and nothing else. Use it for every command that loads your project's code.

Additional Resources​