What is the difference between using bin/rake and bundle exec rake. And which is one preferred style?
bin/rake db:migrate
bundle exec rake db:migrate
What is the difference between using bin/rake and bundle exec rake. And which is one preferred style?
bin/rake db:migrate
bundle exec rake db:migrate
bundle exec
executes a command in the context of your application.
As each application can have different versions of gem used. Using bundle exec guarantees that you use the correct versions.
I use bundle exec
always instead of rake because i have multiple applications running on my system.
Try to use bundle exec rake db:migrate
always.
You can learn more about it here Official documentation
bin/rake
is a kind of stub for the rake command from bundled Gems. It has exactly the same function as bundle exec rake
. See http://bundler.io/v1.14/man/bundle-install.1.html and search for binstubs
for more about stub. And also note that bin/rake
and bin/rails
are stubs generated by Rails, which are different in code from the stubs generated by bundler. However, they all serve the same purpose and have the same function.
You have 3 options on a typical system:
bin/rake db:migrate
rake db:migrate
bundle exec db:migrate
The first option is simply calling the path to the rake
program, whose launcher can be found in the hidden /bin
folder. This launcher is usually just a symlink to the program's content found in your /.rvm
directory. You can find its original location by executing $ which rake
, which will give you something like /home/ubuntu/.rvm/gems/ruby-2.2.3-p481@devonzuegel/bin/rake
.
By default, the second option is essentially the same as the first on most systems. It's what is called an alias, which is basically just a shorthand command for some other program. This is defined somewhere in your shell settings as something like alias rake='/bin/rake'
. It's possible that this alias is pointed to a different program on your machine though, so check that before taking my word for it.
When you use bundle exec
you're telling bundler to ensure that only the gems and their specified versions from your Gemfile.lock
are loaded. This will only work if you're in a directory that contains a Gemfile.lock
or whose parent/grandparent directory contains one.