Skip to content

/commands/bundler/bundle-exec

bundle exec: run a command with the Gemfile's gem versions

The commands on this page have not been executed by our verification runner yet.updated
tool
Bundler
versions
Bundler 2.x
os
linux, macos, windows

bundle exec COMMAND runs COMMAND (for example rake, rspec, rails or ruby script.rb) in the context of the project's bundle: only the gems listed in Gemfile.lock, at exactly those versions, can be loaded. Without it, the shell runs whatever rake or rspec executable comes first in PATH, which may load a different version of a gem than your project uses and fail with errors such as You have already activated rake X, but your Gemfile requires rake Y. Prepending bundle exec to your command may solve this.

Examples

bash
bundle install                      # install the gems from Gemfile / Gemfile.lock
bundle exec rake db:migrate
bundle exec rspec spec/models
bundle exec ruby script.rb
bundle exec rails server

What it does

  • Sets up the load path so require finds only the gems in the bundle.
  • Sets BUNDLE_GEMFILE and RUBYOPT=-rbundler/setup for the command, so Ruby subprocesses it starts use the same bundle.
  • Makes the bundle's executables (rspec, rubocop...) available.

Binstubs: skip typing bundle exec

bash
bundle binstubs rspec-core          # creates bin/rspec
bin/rspec spec/models               # same effect as bundle exec rspec

A binstub is a small script in bin/ that loads Bundler before the gem's executable. Rails applications ship bin/rails and bin/rake this way. Commit the bin/ scripts so everyone uses them.

CommandUses the Gemfile.lock versions?
rakeNo: whichever rake is first in PATH
bundle exec rakeYes
bin/rake (binstub)Yes
ruby -rbundler/setup script.rbYes (same mechanism as bundle exec)

Sources