Search code examples
rubypostgresqlrubygemssinatrasinatra-activerecord

How to fix broken dependencies for Sinatra on server


I had a Sinatra project running with Ruby v2.4.3.

Using rbenv, I updated the Ruby version on my machine to 2.6.1 and modified the Gemfile of the project, specifying the new version.

Here's the update Gemfile:

source "https://rubygems.org"

ruby '2.6.1'
gem 'sinatra', '1.4.6'
gem 'ralyxa'
gem 'alexa_verifier'
gem 'activesupport'
gem 'pg'
gem 'rake'
gem 'sinatra-activerecord'
gem 'openssl'

group :production do
  gem "puma"
end

Now, when I launch the server with ruby server.rb, I get several error messages:

2: from server.rb:2:in `<main>'
    1: from /Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- sinatra/activerecord (LoadError)

/Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:103:in `require': dlopen(/Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/pg-1.2.2/lib/pg_ext.bundle, 9): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib (LoadError)
  Referenced from: /usr/local/opt/postgresql/lib/libpq.5.dylib
  Reason: image not found - /Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/pg-1.2.2/lib/pg_ext.bundle
/Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:103:in `require': Error loading the 'postgresql' Active Record adapter. Missing a gem it depends on? dlopen(/Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/pg-1.2.2/lib/pg_ext.bundle, 9): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib (LoadError)
  Referenced from: /usr/local/opt/postgresql/lib/libpq.5.dylib
  Reason: image not found - /Users/vimbro/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/pg-1.2.2/lib/pg_ext.bundle

I tried downgrading the Ruby version, removed and reinstalled OpenSSL, updated all gems manually, but nothing worked.


Solution

  • Aha, I have run into this recently too. It happened because the version of OpenSSL distributed with Homebrew has been updated to 1.1. I suspect that your 2.6.1 gems were built before, perhaps on another project, using the old version of OpenSSL hence the message

    Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib (LoadError)`
    

    You should be able to reinstall the gem using:

    gem uninstall pg_ext
    

    If you have multiple versions then you should remove them all then run:

    bundle install
    

    and, fingers crossed, you should be good to go.

    However, I also found that for some gems this still didn't work, perhaps because they had specified the version of OpenSSL to use. If the problem still persists you can still get to the old version of OpenSSL. This is a little drastic, but is probably OK for running things on your own local/dev environment - You have been warned.

    brew uninstall openssl
    brew uninstall openssl
    brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
    

    Yes, run the uninstall twice.

    As per a comment below, this helped when running an older version of Ruby that was compiled with the older version of OpenSSL. With newer Rubies I simply had to reinstall them which picked up the newer version of OpenSSL and then everything else worked. Reinstall the gems too.