Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "rails s" and "bundle exec rails s"?

What is the difference between rails s and bundle exec rails s? People seem to say that bundle exec rails s is better, but why? Meanwhile this post says rails s is better.

Which is it?

like image 527
TheOneTeam Avatar asked May 24 '14 15:05

TheOneTeam


People also ask

What does bundle exec rails S 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.

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.

What does Rails console do?

The console command lets you interact with your Rails application from the command line. On the underside, bin/rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.


2 Answers

Sometimes when you install a gem it comes with an executable/binary as well. Examples of these include: rails, rake, rspec, pry, etc. However, when you have multiple versions of a gem installed you then will have multiple versions of these executables sitting around. So if you want to execute one of these binaries for a given rails app you may need to disambiguate which executable you want -- the one for rake v10.1 or the one for rake v10.2, for example. Since the answer to this is discoverable by the version of the gem you have in your Gemfile.lock file (which is created by bundler), bundler supplies a command for executing a binary based on the version that is specified in the current project's Gemfile.lock. This command is bundle exec <command>.

So for most commands you'll want to run bundle exec <command> to make sure you're running the right version for your project (and also to make sure that all dependencies are also loaded from the correct versions specified in your Gemfile.lock). The one notorious exception to this rule is the rails command. The reason being that the first thing the rails command does is load up bundler and check which version of the command to execute. So you'd really just be slowing yourself down to involve bundler in the first place when running the rails command.

So, in short, use:

rails server rails console bundle exec <some command that isn't rails> 
like image 97
pdobb Avatar answered Sep 20 '22 22:09

pdobb


bundle exec ensures you're triggering commands from gems in your Gemfile.

may not be that useful for rails command but is definitely needed for rake for instance.

like image 26
apneadiving Avatar answered Sep 17 '22 22:09

apneadiving