Search code examples
ruby-on-railsrubyruby-on-rails-4passengerrbenv

Switch from RVM to rbenv passenger preload/bundle errors on app start RubyVersionMismatch


It looks like the Phusion Passenger preloader or bundler is calling the system version of kernel_require.rb rather than the rbenv version of kernel_require.rb and bundler complains about RubyVersionMismatch when I startup my Rails 4.1 application. This is my local dev box but I also have a "local" environment for deployment using Apache 2.2.27 (macports).

I completely removed RVM per the instructions, installed rbenv per the instructions then installed Ruby 2.1.2, Rails 4.1.5, Passenger, and did passenger-install-apache2-module. All these were done in my deploy account then I created a blank testing application. When the application starts up the Apache error log reports Bundler::RubyVersionMismatch.

Note that the application seems to work just fine.

log lines referencing rbenv ruby:

log:1 /path/to/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.7.2/lib/bundler/definition.rb:385:in `validate_ruby!': Your Ruby version is 2.0.0, but your Gemfile specified 2.1.2 (Bundler::RubyVersionMismatch)
log:2 /path/to/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.7.2/lib/bundler.rb:117:in `setup'
log:3 /path/to/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.7.2/lib/bundler/setup.rb:17:in `<top (required)>'

log lines referencing mac system ruby:

log:4 /System/Library/Frameworks/Ruby.framework/Versions/2.0/.../ruby/2.0.0/rubygems/.../kernel_require.rb:55:in `require'
log:5 /System/Library/Frameworks/Ruby.framework/Versions/2.0/.../ruby/2.0.0/rubygems/.../kernel_require.rb:55 in `require'
log:6 Pool2/SmartSpawner.h:298: Preloader for /path/to/my/deploy/rails/testing/current started on PID 33863

Environment:

OSX 10.9.4
Apache 2.2.27 installed via macports to /opt/local/apache2 etc
rbenv local 2.1.2  
rbenv global 2.1.2
rbenv which ruby /path/to/.rbenv/versions/2.1.2/bin/ruby
rbenv whence passenger 2.1.2
rails -v 4.1.5
bundler version 1.7.2
using mina v0.3.0, mina-rsync for deploy

Passenger config for apache:

LoadModule passenger_module /path/to/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/passenger-4.0.50/buildout/apache2/mod_passenger.so
PassengerRoot /path/to/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/passenger-4.0.50
PassengerDefaultRuby /path/to/.rbenv/versions/2.1.2/bin/ruby

Solution

  • I did not set rbenv local or global in the deploy account.

    • fix and clean: probably overkill
    • removed rbenv from deploy account:

      rm -rf ~/.rbenv
      
    • removed rbenv setup from .bash_profile

    • install rbenv per instructions:

      git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
      echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
      $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
      
    • logout/login

    • add rbenv setup to .bash_profile
    • install rbenv-build:

      git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
      
    • install Ruby 2.1.2 and Bundler:

      rbenv install 2.1.2 
      gem install bundler
      
    • set rbenv global:

      rbenv global 2.1.2
      gem install passenger 
      passenger-install-apache2-module
      
    • copy suggested passenger config to my local /path/do/apache2/conf.d/passenger.conf

    • sudo apachectl restart
    • remove testing app from local staging server:

      rm -rf /path/to/my/deploy/testing/*
      

    Returning back to the development account:

    • recreate testing app

      rails new .... testing
      
    • cd testing

      rake db:create  
      rake db:migrate
      git init
      git add .
      git commit -am 'initial'
      git push origin master
      
      mina -v setup server=local
      mina -v deploy server=local
      
    • tail -f /path/to/apache2/logs/error_log

    I hit my app on my local server and RubyVersionMismatch is no longer in the error log, and the application starts normally.