What is the major difference between running the rake command with and without bundle exec?
I have seen few posts stated that when you run the command with bundle exec then it will be run on scope of the gems version defined in the gem file. If that is the case, then it should be mandatory to run rake command with bundle exec
?
bundle exec rake some:task
runs the rake task within the context of your bundle.
You didn't explicitly mention Rails but i see you're post is tagged with Rails so a contrived example of this in action might be the following:
You have version 2.0 of the fictitious whateva-whateva
gem installed on your system for some valid reason.
You decide you want to pull down an old Rails project from somewhere to check it out and run bundle install
within the cloned project's root folder. That command will install all of the gems that the Rails app requires and one of them happens to be verison 1.0 of the fictitious whateva-whateva
gem.
So the current state is this: your old rails app has a gem bundle that includes an older version of the whateva-whateva
and your systemwide gems include a newer version of the whateva-whateva
gem.
When you run rake tasks associated with your Rails app, which version do you want loaded? The older one of course.
In order to do this you can use bundle exec rake the:task
and it runs the rake command within the context of your bundle -- the older version of the gem plus whatever other stuff was specified in the old rails app's Gemfile.
So yeah after all that, i think it's safe to say that BEST practice would be that you should always prepend bundle exec
but to be honest I'm pretty lazy and rarely do it unless I see problems.
In other news, if you use Bundler's binstubs you don't need to add it. Here's a link to setting that up: https://thoughtbot.com/blog/use-bundlers-binstubs