Rails PG - Library not loaded: /usr/local/lib/libpq.5.4.dylib
Last night was a long night. It was filled with long meetings, and complex feature developments followed by a macOS update and a Homebrew upgrade. I woke up excited to complete the feature I was working on last night. I ran RSpec with was greeted with an error I didn't notice yesterday. I did a rails c with similar results.
Library not loaded: /usr/local/lib/libpq.5.4.dylib
If you experienced the same, the chances are, you ran brew upgrade. This in turn upgraded your PostgresSQL installation invalidating the compiled native extension during gem installation. You can uninstall and re-install the gem to fix this issue.
gem uninstall pg
bundle
I ran the spec file and it failed again, but that was because of a silly error. Hope this post saved you a few minutes of googling.
Extra
The error I got running rails c.
/Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require': dlopen(/Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg_ext.bundle, 0x0009): Library not loaded: '/usr/local/opt/postgresql/lib/libpq.5.dylib'
Referenced from: '/Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg_ext.bundle'
Reason: tried: '/usr/local/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file), '/usr/local/Cellar/postgresql@14/14.5_5/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file) - /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg_ext.bundle (LoadError)
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg.rb:49:in `block in <module:PG>'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg.rb:37:in `block in <module:PG>'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg.rb:42:in `<module:PG>'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/pg-1.4.3/lib/pg.rb:6:in `<main>'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler/runtime.rb:55:in `each'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler/runtime.rb:55:in `block in require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler/runtime.rb:44:in `each'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler/runtime.rb:44:in `require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/bundler-2.3.18/lib/bundler.rb:187:in `require'
from /Volumes/Dev/Work/Edge/edge/config/application.rb:7:in `<top (required)>'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:92:in `require'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:92:in `preload'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:166:in `serve'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:148:in `block in run'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:142:in `loop'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application.rb:142:in `run'
from /Users/jdeen/.rvm/gems/ruby-3.1.2@edge-app/gems/spring-4.1.0/lib/spring/application/boot.rb:19:in `<top (required)>'
from <internal:/Users/jdeen/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
from <internal:/Users/jdeen/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
from -e:1:in `<main>'
About the Author
Ziyan Junaideen -
Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.