Uma
Uma

Reputation: 43

difference between running rake command with and without bundle exec

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?

Upvotes: 4

Views: 2257

Answers (4)

Mario Zigliotto
Mario Zigliotto

Reputation: 9025

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

Upvotes: 4

Pravin Mishra
Pravin Mishra

Reputation: 8444

bundle-exec - Execute a command in the context of the bundle

This command executes the command, making all gems specified in the Gemfile(5) available to require in Ruby programs.

It's not for the only rake, instead applicable for rails, rspec, rackup command too.

Essentially, if you would have normally run something like rspec spec/my_spec.rb, and you want to use the gems specified in the Gemfile(5) and installed via bundle install(1), you should run bundle exec rspec spec/my_spec.rb.

Note that bundle exec does not require that an executable is available on your shell's $PATH.

For more detail, have a look to bundle exec doc.

Upvotes: 0

Jyothu
Jyothu

Reputation: 3154

If you use bundle exec before any command in rails, It will search for the Gems Which are mentioned in our Gemfile, in application home folder.

Suppose, You have 2 applications, and using different ruby versions for each of them. Without bundle exec the command might be failed to run, as it may take different version Gem to run that task. But If you start using bundle exec It will take the exact gem version to run the task/application.

I recommend you to use **bundle exec** before any command.

Upvotes: 0

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 18607

BUNDLE_GEMFILE=/path/to/gemfile bundle exec can be used to precede any command (if BUNDLE_GEMFILE is not specified it searches up the file system and uses the first one it finds), not just rake.

Any command you run may invoke executable Ruby commands (such as rake) or require code from Ruby libraries (such as the Rake::Task class), and these things are typically provided by gems. gem env tells you where the gem-provided libraries and executables are. However if you use bundle exec it restricts the available gems to those specified in the Gemfile.lock file associated with the Gemfile your bundle exec context is using.

Using all the available gems on your machine (which can happen if you don't do bundle exec) can be undesirable for a couple reasons:

  • You may have incompatibilities in your full gem set.
  • It's harder to tell exactly what gems you're using, adding some unpredictability to your working environment.

Here's a quick way to see the difference.

  1. gem install thin
  2. Create a directory foo with two files: an empty Gemfile and a file foo.rb with the contents:
    #! /usr/bin/ruby (or whatever the path to your system Ruby is)
    require 'thin'
  3. Make foo.rb executable.
  4. Note that running thin and ./foo.rb from the command line both work, but preceding either with bundle exec will not work.

Upvotes: 2

Related Questions