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?
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>