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 allows us to run an executable script in the specific context of the project's bundle. Upon running the above command, bundle exec will run the executable script for rake version specified in project's Gemfile thus avoiding any conflicts with other versions of rake installed system-wide.
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.
bundle exec makes a number of changes to the shell environment, then executes the command you specify in full. It also modifies Rubygems: disallow loading additional gems not in the bundle. modify the gem method to be a no-op if a gem matching the requirements is in the bundle, and to raise a Gem::LoadError if it's not.
Beginning in Rails 4, Rails ships with a rails binstub at ./bin/rails that should be used instead of the Bundler-generated rails binstub. So, at the end of the day there's no difference. But considering the fact that Rails goes through the trouble of shipping its own binstubs, I'd favor bin/rails alternative.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With