I learned Rails using just the rake command like rake db:migrate
; however, I read that I should be using the bundle exec rake ...
instead of just plain rake
. Now I'm confused about which to use.
Should I be using bundle exec rake
instead of just plain rake
or is it just a preference thing? Any insight would be much appreciated! Thanks!
If you get tired of typing bundle exec all the time, you can configure rvm so it's not necessary: ruby.railstutorial.org/chapters/… A nice side-effect of using bundle exec is that it will also tell you if you should run bundle install to install versions of gems you are missing compared to the Gemfile. lock file.
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
executes a command in the context of your bundle.
That means it uses the gems specified in your Gemfile. Much of the time, running bundle exec rake foo
has the same results as if you just ran rake foo
, especially if you have the same gems installed systemwide as in your Gemfile. However, some applications may specify different versions of gems than the ones you have installed systemwide, and may want those exact gems and versions to be able to run correctly. If you just run without bundle exec
, you may get some weird errors.
Using bundle exec
guarantees that the program is run with the environment specified in the gemfile, which hopefully means it is the environment that the creators of the program want it to be run in, which hopefully means it should run correctly no matter what weird setup you have on your computer.
It basically standardizes the environment under which the program is run. This helps avoid version hell and makes life much easier.
See http://bundler.io/v1.3/man/bundle-exec.1.html for more info.
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