21

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
PRN
  • 231
  • 2
  • 5

3 Answers3

5

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

Tushar Maroo
  • 325
  • 1
  • 5
  • 24
4

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.

Robert
  • 1,964
  • 1
  • 22
  • 22
-1

You have 3 options on a typical system:

  1. bin/rake db:migrate
  2. rake db:migrate
  3. 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.

  • 2
    " the same as the first on most systems" - I highly doubt this (that most systems have this alias). Instead, naked "rake" will invoke that from global gem list, not from this app's bundle. – Sergio Tulentsev Aug 20 '15 at 07:35
  • 1
    actually there is some truth to this answer. Rvm does indeed fudge the path so that `rake ...` runs your project binaries. In fact there is a plugin for rbenv which allow you to do the same. `which` points to RVM/rbenv which in turn points to bin although recent recommendations suggest pointing bundler at .bundle/bin as rails has it's own scripts. – PhilT Oct 13 '16 at 16:44