53

A new Rails project's Gemfile shows:

# Use unicorn as the app server
gem 'unicorn'

rails s --help shows:

Usage: rails server [mongrel, thin, etc] [options]

Yet, doing:

rails s unicorn

I get:

/Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `require': cannot load such file -- rack/handler/unicorn (LoadError)
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `try_require'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:16:in `get'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/server.rb:272:in `server'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands/server.rb:59:in `start'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

I've used unicorn in the past on other projects, but always had to run the unicorn command and specify a config file which is a bit of a pain. I am wondering how I can just simply make it work by using rails s...

Is this possible?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
patrick
  • 9,290
  • 13
  • 61
  • 112
  • I don't think `unicorn` can be called from `rails server` directly. Check this out - https://github.com/samuelkadolph/unicorn-rails – Dogbert Apr 07 '13 at 04:38
  • 2
    Just typing `unicorn` works for me, same amount of keystrokes as `rails s` – mind.blank Apr 07 '13 at 05:10

5 Answers5

61

It looks like the unicorn-rails gem that @Dogbert mentioned can actually be used to make Unicorn the rails server handler.

Simply include gem "unicorn-rails" (and for Rails 4.2.4, gem "rack-handlers") in your Gemfile, run bundle install to install the gem, then you can run:

$ rails server unicorn

Although once unicorn-rails is installed, Unicorn should be the default app server so you could also just run rails server and it should use Unicorn (assuming you don't also have Thin or Mongrel in your Gemfile, in which case they may conflict and you might want to remove the ones you're not using).

Evan
  • 608
  • 6
  • 10
Stuart M
  • 11,458
  • 6
  • 45
  • 59
  • 5
    I find it very interesting that the gemfile has 'unicorn' rather than 'unicorn_rails' as a suggestion. I also find it interesting that this webpage says: "unicorn_rails was made to be an easier transition for users of pre-Rack versions of Rails. The manpage encourages Rails 3 users to use plain unicorn instead." http://blog.engineyard.com/2010/everything-you-need-to-know-about-unicorn – patrick Apr 08 '13 at 05:40
  • 1
    Which Gemfile are you seeing `unicorn` in? I suggested adding the `unicorn-rails` gem to your Gemfile. – Stuart M Apr 08 '13 at 05:46
  • 1
    For Rails 4.2.4, this first requires adding `gem 'rack-handlers'` to your Gemfile along with `gem 'unicorn'` – Evan Sep 18 '15 at 17:10
27

A better option might just be to run the unicorn server directly.

bundle exec unicorn -p 3000 # default port is 8080
Steven Soroka
  • 19,404
  • 4
  • 52
  • 40
19
gem 'rack-handlers'

rails server unicorn
Tom Maeckelberghe
  • 1,969
  • 3
  • 21
  • 24
0

However the answer by Steven is the simplest way to do.

I run unicorn on development environment via a rake task:

lib/tasks/dev_unicorn.rake:

task :server do
  # optional port parameter
  port = ENV['PORT'] ? ENV['PORT'] : '3000'
  puts 'start unicorn development'
  # execute unicorn command specifically in development
  # port at 3000 if unspecified
  sh "cd #{Rails.root} && RAILS_ENV=development unicorn -p #{port}"
end
# an alias task
task :s => :server

run:

rake s

Reference http://jing.io

tokhi
  • 21,044
  • 23
  • 95
  • 105
0

I don't think it is possible to use unicorn as 'rails s'. Use this -

Add gem 'unicorn' to gem file and run bundle install.

and then run any of the following commands -

$ unicorn -p 3000

or

$ unicorn_rails -p 3000

prashant
  • 108
  • 7