Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between bin/rake and bundle exec rake

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
like image 508
PRN Avatar asked Mar 22 '15 07:03

PRN


People also ask

What does bundle exec rake do?

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.

What is bundle exec rake db migrate?

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.

Why do we need bundle exec?

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.

Should be used instead of the bundler generated rails Binstub?

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.


2 Answers

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

like image 94
Tushar Maroo Avatar answered Nov 15 '22 19:11

Tushar Maroo


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.

like image 20
Robert Avatar answered Nov 15 '22 19:11

Robert