9

Some guides (example) recommend this to start one's webserver

bundle exec rails server puma

But I've always just started the server with puma directly

bundle exec puma

Does something special happening when firing up puma (or any other server) via rails server?

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

15

When you use rails s <server>, the server is launched from the Rails command and is aware of the Rails environment.

This makes possible, for example, to use any of the features and flags offered by the rails server command.

rails s --help
Usage: rails server [mongrel, thin, etc] [options]
    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=ip                 Binds Rails to the specified ip.
                                     Default: 0.0.0.0
    -c, --config=file                Use custom rackup configuration file
    -d, --daemon                     Make server run as a Daemon.
    -u, --debugger                   Enable ruby-debugging for the server.
    -e, --environment=name           Specifies the environment to run this server under (test/development/production).
                                     Default: development
    -P, --pid=pid                    Specifies the PID file.
                                     Default: tmp/pids/server.pid

    -h, --help                       Show this help message.

For instance, you can attach a debugger to the session passing --debugger or daemonize the server.

The second advantage is that you can version the Puma instance, since you will have to list the gem in the Gemfile. This is already true if you start it with bundle exec like you are doing.

Conversely, when you simply run $ puma (or $ bundle exec puma) you're not passing through the Rails system. Puma will try to find a rack bootstrap file and will use it (it works because Rails provides a config.ru script in the application root.

Generally speaking, there is no real difference if you don't need to pass specific options to the server. I like puma and I tend to use it in some projects even when on production we use Unicorn, thus running $ puma as a standalone command is handy because I don't need to add it to the Gemfile.

However, I would probably go with $ rails s puma if my entire stack uses Puma. This is also the command suggested in the documentation.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • when running rails s puma the config file you have does not get loaded... that is the downside. Here is a quick alias to starting up the sever with a config file (config/puma.rb). JUST COPY PASTE echo 'alias start_puma="bundle exec puma -p 3000 -S ~/puma -C config/puma.rb"' >> ~/.bash_profile && source ~/.bash_profile THEN USE start_puma – blnc Aug 20 '14 at 01:12
  • 2
    The recommended command on the linked page is now `bundle exec puma` (7 December 2018). – vinegarbin Dec 07 '18 at 18:41
  • It's not clear what is recommended from that page. But from what I can gather, `puma` has a separate notion of environment which is used to select a configuration file. And `bin/rails s` picks up this environment automatically (from `RAILS_ENV`). – x-yuri Jun 05 '19 at 19:49
  • ...or possibly not a separate notion of environment. Maybe it just doesn't consider itself `rails`-only web server, and therefore can't rely on `RAILS_ENV` variable. After all, it respects `RACK_ENV`. – x-yuri Jun 05 '19 at 20:39