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
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 serverWhat it does
- Sets up the load path so
requirefinds only the gems in the bundle. - Sets
BUNDLE_GEMFILEandRUBYOPT=-rbundler/setupfor the command, so Ruby subprocesses it starts use the same bundle. - Makes the bundle's executables (
rspec,rubocop...) available.
Binstubs: skip typing bundle exec
bundle binstubs rspec-core # creates bin/rspec
bin/rspec spec/models # same effect as bundle exec rspecA 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.
| Command | Uses the Gemfile.lock versions? |
|---|---|
| rake | No: whichever rake is first in PATH |
| bundle exec rake | Yes |
| bin/rake (binstub) | Yes |
| ruby -rbundler/setup script.rb | Yes (same mechanism as bundle exec) |
Sources
Related pages
errors · rubyRuby NoMethodError: undefined method for nilRuby raises NoMethodError undefined method for nil when a method is called on a value that is nil, often a failed lookup. Find the nil source; use &. or fetch.config · composercomposer.json minimum-stability and prefer-stableminimum-stability sets the least stable version Composer may install (default stable). Use @dev on one package or prefer-stable instead of lowering it globally.glossary · semverWhat is semantic versioning (SemVer)?Semantic versioning numbers releases MAJOR.MINOR.PATCH: major for breaking changes, minor for compatible features, patch for fixes. Pre-release rules.errors · bundlerBundler: Could not find rake-13.2.1 in locally installed gemsCould not find GEM-VERSION in locally installed gems: Gemfile.lock lists a gem not installed for this Ruby. Run bundle install; check path and platforms.