Search code examples
ruby-on-railsrubybundlerunicorn

Unicorn not using rbenv version or rails -v version


Just got done installing a new version of ruby via rbenv onto my server.

rbenv versions returns:

system
2.2.3
* 2.3.3 (set by /opt/pesto/current/config/.ruby-version)

I am trying to restart my unicorn service (sudo service unicorn restart):

Couldn't restart.

In desperation, I try to start (sudo service unicorn start):

Could not find erubis-2.7.0 in any of the sources
Run `bundle install` to install missing gems.

I already ran bundle install. When I do rbenv local 2.3.3 as well as rbenv global 2.3.3, then gem list, I see:

 'erubis (2.7.0)'

In plain sight. That is to say - this gem is definitely there.

So I saw if it was doing something weird with my other version. I changed to rbenv global 2.2.3 and rbenv local 2.2.3 and did:

gem install erubis -v 2.7.0

After doing that, now the error is complaining about another, different gem that can ALSO already be found in my 2.3.3 gem list... so I have concluded that it is definitely looking only at the 2.2.3 gem list, since adding the gem to 2.2.3 fixed the issue.

Simply running bundle install for 2.2.3 is not an option - I need dependencies that are only available in my 2.3.3 distribution. What I really want is for unicorn to refer to what's available in 2.3.3 only, and to quit looking at 2.2.3 and what gems are available there.

Here is my unicorn.rb:

app_path = File.expand_path(File.dirname(__FILE__) + '/..')
# Set the working application directory
# working_directory "/path/to/your/app"
working_directory app_path

before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
end


# Set the location of the unicorn pid file. This should match what we 
 put in the
# unicorn init script later.
pid (ENV['UNICORN_PID_PATH'] || "#{app_path}/tmp/") + 'unicorn.pid'

# You should define your stderr and stdout here. If you don't, stderr 
defaults
# to /dev/null and you'll lose any error logging when in daemon mode.
stderr_path(app_path + '/log/unicorn.log') unless ENV['RAILS_ENV'] == 
'development'
stdout_path(app_path + '/log/unicorn.log') unless ENV['RAILS_ENV'] == 
'development'

# Unicorn socket
listen('127.0.0.1:3000', backlog: 64, :tcp_nopush => true) if 
ENV['RAILS_ENV'] == 'development'
list en(app_path + '/tmp/unicorn.sock', backlog: 64) unless 
ENV['RAILS_ENV'] == 'development'
listen(8080, :tcp_nopush => true) unless ENV['RAILS_ENV'] == 
'development'

# Number of processes
# worker_processes 4
worker_processes (ENV['RAILS_ENV'] == 'production' ? 16 : 1)

# Time-out
timeout 300

# Load the app up before forking.
preload_app true

# Garbage collection settings.
 GC.respond_to?(:copy_on_write_friendly=) &&
  GC.copy_on_write_friendly = true

# If using ActiveRecord, disconnect (from the database) before 
forking.
before_fork do |server, worker|
   defined?(ActiveRecord::Base) &&
     ActiveRecord::Base.connection.disconnect!
end

# After forking, restore your ActiveRecord connection.
after_fork do |server, worker|
  defined?(ActiveRecord::Base) &&
    ActiveRecord::Base.establish_connection
end

I don't see where the ruby version it is referring to is referenced in this config file. As such, I'm at odds a bit on figuring out how to get unicorn to use my newer ruby.


Solution

  • The rails version that unicorn uses can be edited in /etc/init.d/unicorn. I changed it and it worked.