What does bundle exec rake mean?
bundle exec
is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate
is the script where db is the namespace and migrate is the task name defined.
So bundle exec rake db:migrate
executes the rake script with the command db:migrate
in the context of the current bundle.
As to the "why?" I'll quote from the bundler page:
In some cases, running executables without
bundle exec
may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.
You're running bundle exec
on a program. The program's creators wrote it when certain versions of gems were available. The program Gemfile specifies the versions of the gems the creators decided to use. That is, the script was made to run correctly against these gem versions.
Your system-wide Gemfile may differ from this Gemfile. You may have newer or older gems with which this script doesn't play nice. This difference in versions can give you weird errors.
bundle exec
helps you avoid these errors. It executes the script using the gems specified in the script's Gemfile rather than the systemwide Gemfile. It executes the certain gem versions with the magic of shell aliases.
See more on the man page.
Here's an example Gemfile:
source 'http://rubygems.org'
gem 'rails', '2.8.3'
Here, bundle exec
would execute the script using rails version 2.8.3 and not some other version you may have installed system-wide.