Error using unicorn with sinatra application

March 16, 2015 2.7k views
Sinatra Ruby Nginx

I have an ruby with sinatra application, and Im trying to run the server with unicorn.

Im using the command:

service unicorn restart
service ngnix restart

and in my logs I get:

/usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'/usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- sinatra (LoadError)
    from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

this problem would be solved running unicorn with bundle exec

how can I do that?

Thanks

3 Answers

How did you install and configure unicorn? Are you using the Ruby on Rails One-Click application? If so, unicorn is started and stopped using an init script located in /etc/init.d/unicorn This can be configured in /etc/default/unicorn The default contents looks like:

# Change parameters below to appropriate values and set CONFIGURED to yes.
CONFIGURED=yes

# Default timeout until child process is killed during server upgrade,
# it has *no* relation to option "timeout" in server's config.rb.
TIMEOUT=60

# Path to your web application, sh'ld be also set in server's config.rb,
# option "working_directory". Rack's config.ru is located here.
APP_ROOT=/home/rails

# Server's config.rb, it's not a rack's config.ru
CONFIG_RB=/home/unicorn/unicorn.conf

# Where to store PID, sh'ld be also set in server's config.rb, option "pid".
PID=/home/unicorn/pids/unicorn.pid
UNICORN_OPTS="-D -c $CONFIG_RB -E production"

PATH=/usr/local/rvm/rubies/ruby-2.1.3/bin:/usr/local/rvm/rubies/ruby-2.0.0-p353/bin:/usr/local/rvm/gems/ruby-2.0.0-p353/bin:/home/unicorn/.rvm/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:$
export GEM_HOME=/usr/local/rvm/gems/ruby-2.1.3
export GEM_PATH=/usr/local/rvm/gems/ruby-2.1.3:/usr/local/rvm/gems/ruby-2.1.3
DAEMON=/usr/local/rvm/gems/ruby-2.1.3/bin/unicorn

To use bundler, you would need to update the DAEMON line. It defines the command that will be run. You'll probably want something like:

DAEMON='/usr/local/rvm/gems/ruby-2.1.3/bin/bundle exec unicorn'

You can use the command which bundle to see the full path of the bundle binary.

For more information about the setup on the Ruby on Rails One-Click app, see this tutorial:

Hey @asb,

At first thanks for the support.

Im running the Ruby on Rails One-Click app,
as you said I tried to change the DAEMON line on the file /etc/default/unicorn to use the bundle exec option.

Now whenever I restart/stop/start my unicorn service I got the following message:

Could not locate Gemfile

I also tried to indicate where the Gemfile location is but not succefull from now..

Do you have any other thoughts in mind?

Thank you!

I solved that issue by changing and adding the ruby version that I was using (2.2.0) into the /etc/defaul/unicorn

# Change parameters below to appropriate values and set CONFIGURED to yes.
CONFIGURED=yes

# Default timeout until child process is killed during server upgrade,
# it has *no* relation to option "timeout" in server's config.rb.
TIMEOUT=60

# Path to your web application, sh'ld be also set in server's config.rb,
# option "working_directory". Rack's config.ru is located here.
APP_ROOT=/home/rails

# Server's config.rb, it's not a rack's config.ru
CONFIG_RB=/home/unicorn/unicorn.conf

# Where to store PID, sh'ld be also set in server's config.rb, option "pid".
PID=/home/unicorn/pids/unicorn.pid
UNICORN_OPTS="-D -c $CONFIG_RB -E production"

PATH=/usr/local/rvm/rubies/ruby-2.1.3/bin:/usr/local/rvm/rubies/ruby-2.0.0-p353/bin:/usr/local/rvm/gems/ruby-2.0.0-p353/bin:/usr/local/rvm/gems/ruby-2.2.0/bin:/home/unicorn/.rvm/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:$
export GEM_HOME=/usr/local/rvm/gems/ruby-2.2.0
export GEM_PATH=/usr/local/rvm/gems/ruby-2.2.0:/usr/local/rvm/gems/ruby-2.2.0
DAEMON=/usr/local/rvm/gems/ruby-2.2.0/bin/unicorn
Have another answer? Share your knowledge.