Ruby on Rails / PostgreSQL - Library not loaded error when starting server
Asked Answered
D

3

9

Heyo. I apologize in advance if this question has been answered in another thread. I've searched through the site but I didn't find an answer. However, the closest related issue was Ruby on Rails / PostgreSQL - Library not Loaded error when starting server- libq.5.dylib, but it was never resolved, and the answer doesn't help me.

I'm trying to get PostgreSQL running on my Mac OSX 10.6.8. I already have Ruby 1.9.3p392 and Rails 3.2.13 installed.

Since I've 10.6.8, and PostGresApp requires 10.7+, I tried installing PG using the manual installer (v9.2.3) available here: http://www.enterprisedb.com/products-services-training/pgdownload

I first installed it to its default path at: /Library/PostgreSQL/9.2/. I then installed the pg gem (PATH=$PATH:/Library/PostgreSQL/9.2/bin sudo gem install pg). When I tried rails s to launch my localhost, I got the error below.

After some reading, I guessed it might be a path error, so I removed the pg gem, and then used the postgresql uninstaller. I then reinstalled pg in /users/stewartmccoy/Library/PostgreSQL/9.2/...and then reinstalled the pg gem. But I still get the same error.

Any thoughts on how I might correctly install PostgreSQL and get my rails server running?

stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
  Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
  Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
    from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
stewart-mccoys-macbook:footy_subs stewartmccoy$ gem uninstall pgSuccessfully uninstalled pg-0.14.1
stewart-mccoys-macbook:footy_subs stewartmccoy$ PATH=$PATH:/users/stewartmccoy/Library/PostgreSQL/9.2/bin sudo gem install pg
Password:
Fetching: pg-0.14.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed pg-0.14.1
1 gem installed
Installing ri documentation for pg-0.14.1...
Installing RDoc documentation for pg-0.14.1...
stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
  Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
  Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
    from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Dolly answered 19/3, 2013 at 22:58 Comment(2)
thankyou for providing a reasonable level of detail - Pg version, pg package type, host OS, etc.Springfield
possible duplicate of Ruby on Rails / PostgreSQL - Library not Loaded error when starting server- libq.5.dylibObsolescent
S
9

The key part of the error is:

Library not loaded: libpq.5.dylib (LoadError)

This suggests that ruby can't find libpq at runtime. To address that you should probably set the DYLD_LIBRARY_PATH environment variable to point to the lib directory of your PostgreSQL install, either globally or in a wrapper script you use to start Rails. See this superuser question for some more info.

The Pg gem can find the library during compilation and installation because the pg_config executable is on the PATH and it uses that to find libpq. It appears that it doesn't store the path for use at runtime so you have to set the runtime dynamic linker up yourself.

A simple wrapper script (in case you don't want to modify your global environment) is something like:

#!/bin/bash
export DYLD_LIBRARY_PATH=/path/to/pg/lib 
exec rails "$@"

The "$@" basically means "pass all arguments to this script through as if they were passed here directly". It preserves quoting correctly and essentially means that the rails command can't tell you didn't run it directly.

Springfield answered 19/3, 2013 at 23:43 Comment(12)
Thanks Craig. Is there a reason I wouldn't want to globally set the DYLD_LIBRARY_PATH environment variable? If the wrapper script is the best approach, how should I save the script and where? i.e. with what filename and extension and in what directory, so Rails knows to run the script at runtime?Dolly
@stewartmccoy Rails doesn't run the script, you use the script to start Rails. Use it as if it were the rails command. In most cases it's fine to set DYLD_LIBRARY_PATH globally though, the only reason not to is if there are library name conflicts.Springfield
I tried searching SO / googling for how to set DYLD_LIBRARY_PATH globally, but couldn't find a solution that made sense. How can I do that? I assume I would need to set the path to /users/stewartmccoy/Library/PostgreSQL/9.2/ since that's where I have pg installed? And, if I take that approach, will I be able to just use rails s to view apps on localhost?Dolly
@stewartmccoy Yes, if you set it globally you can use the rails command without a wrapper. You want to add the directory containing libpq.5.dylib to the DYLD_LIBRARY_PATH, it'll be in the lib directory of the Pg install. For setting it - put it in your .bashrc or whatever, lots of options. I don't use Apple gear much so I can't be more specific about that part.Springfield
my path for libpq.5.dylib is /Users/stewartmccoy/Library/PostgreSQL/9.2/lib/libpq.5.dylib. I'm still new to the command line, so I'm not quite sure what the command would be to add that directory to DYLD_LIBRARY_PATH. Would it be something like export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_PATH?Dolly
@stewartmccoy Yep. See the linked question for more details.Springfield
Thanks! Resolved that issue :) Now I just have to figure out how to resolve an error asking me to 'Please install the sqlite3 adapter'...lol.Dolly
One last question...I fixed the sqlite3 adapter issue by adding the gem to the Gemfile...however, when I rain rails s again, I once again got the DYLD_LIBRARY_PATH error. Is there a way to make the change permanent, rather than running export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_P‌​ATH every time?Dolly
@stewartmccoy As I said, I don't use Mac OS X so I can't offer you guidance there. Search for "how do I set environment variables permanently in mac os x" or something like that.Springfield
Oh word, I missed the part of your comment about .bashrc. Got it. Thanks again!Dolly
For those that come after, I permanently fixed this by editing .bash_profile in VIM and adding export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_P‌​‌​ATH, and then restarting Terminal before doing rails s.Dolly
In my case, the library was in /Library/PostgreSQL/9.3/lib. I thus solved the issue running export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.3/lib:$DYLD_LIBRARY_PATH.Mikol
G
6

I wanted to put my two cents here to answers above.

Sometimes it's enough to just reinstall pg gem.

gem uninstall pg && gem install pg

In my case I upgraded postgresql server from 9.6 up to 14.x and dylib paths were changed.

/opt/local/lib/postgresql96/libpq.5.dylib -> /opt/local/lib/postgresql14/libpq.5.dylib

The gem uses pg_config to determine paths to /opt/local/lib/postgresql[version] folder (named PKGLIBDIR). To build native library for the gem rake sets paths to dynamic libs by fetching them from pg_config in compile. Once pg gem is compiled the paths are linked forever. So, the reinstallation recompiles and relinks path to the new libraries.

Governance answered 8/9, 2022 at 21:28 Comment(1)
In my case it didn't even upgrade. pg-1.4.5 was simply reinstalled, and then it worked. Strange.Carcinomatosis
L
2

A similar stackoverflow answer proposed removing Postgres.app from your PATH, uninstalling the pg gem, and installing the pg gem again.

(I'm not taking the time to check carefully if that applies to this problem, but it solved a problem I had and I want to spread that solution around since I had trouble finding it.)

Lotty answered 14/6, 2013 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.