Search code examples
ruby-on-railsbundler

Why is `bundle exec rails s` not the same as `rails s`?


Despite answers like these: rails s or bundle exec rails s

and blog posts like these: https://www.wyeworks.com/blog/2011/12/27/bundle-exec-rails-executes-bundler-setup-3-times/

which all seem to indicate you should simply run rails s rather than bundle exec rails s I still find that occasionally I can't simply run rails s. E.g.

$ rails s
Rails is not currently installed on this system. To get the latest version, simply type:

$ sudo gem install rails

You can then rerun your "rails" command. 

Whereas running this immediately afterwards in the same environment:

$ bundle exec rails s
=> Booting WEBrick
=> Rails 4.0.0.beta1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2015-01-28 12:31:48] INFO  WEBrick 1.3.1
[2015-01-28 12:31:48] INFO  ruby 2.0.0 (2014-11-13) [x86_64-darwin14.0.0]
[2015-01-28 12:31:48] INFO  WEBrick::HTTPServer#start: pid=18959 port=3000

Why is it not the same? Should this behaviour be fixed and, if so, how?

==== UPDATE

In response to @awendt

$ which rails
/Users/snowcrash/.rvm/gems/ruby-2.0.0-p598/bin/rails
$ rails c
Rails is not currently installed on this system. To get the latest version, simply type:

    $ sudo gem install rails

You can then rerun your "rails" command.
$ bundle exec rails c
Loading development environment (Rails 4.0.0.beta1)
2.0.0-p598 :001 > 

Solution

  • Trying to reproduce this, I get (on Ubuntu 12.04.5 LTS):

    me@machine:/some/path$ rails c
    The program 'rails' is currently not installed.  To run 'rails' please ask your administrator to install the package 'rails'
    me@machine:/some/path$ which rails
    me@machine:/some/path$
    

    So in my case, the rails script is not in my $PATH.

    Your setup is different because the message explicitly mentions gems. You might even get this:

    you@machine:/some/path$ which rails
    /usr/bin/rails
    you@machine:/some/path$
    

    because /usr/bin/ is in your $PATH somewhere before other things and you're still invoking the rails script that ships with the OS.

    Anyway, the important part is: bundle exec rails c lets Bundler figure out where to find the Rails gem, instead of letting the shell figure it out.

    You may install gem into a completely arbitrary location, where only Bundler can find them because that location is not in your $PATH but in your $BUNDLE_PATH (see list of available keys for bundle config).

    That's why rails and bundle exec rails are not the same.

    This is not an error, and the behavior should not be fixed. Yes, you shouldn't have to run bundle exec rails because in recent versions, rails does the exact same thing. But the error you're seeing is a different issue.