简体   繁体   中英

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?

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>

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.

http://bundler.io/v1.5/rails3.html - Rails 3附带了捆绑器支持

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM