Ruby 'pg' gem linking to wrong copy of libpq.5.dylib (on OSX)
Both the cause of the problem and an easy solution became apparent when I ran bin/pg_config
at the command line.
pg_config
generates the variables that are used to control compilation and linking. Of particular interest are LIBDIR and LDFLAGS. LIBDIR specifies the location for static libraries, while LDFLAGS provides locations to search for dynamic libraries. On my system, LIBDIR was set correctly to /LibraryPostgreSQL/9.3/lib
, but LDFLAGS was set as follows:
LDFLAGS = -L../../../src/common -L/usr/local/lib -L/opt/local/20140109/lib -Wl,-dead-strip-dylibs
Since libpq.5.dylib
was not present at any of these locations, the gem failed to find it, and instead found an older version that happened to be installed at /usr/lib
.
One way to fix this would be to inject the correct file location into LDFLAGS, possibly by modifying the code in extconf.rb
that generates the config file. However, a much easier fix in this case is just to add a symlink in /usr/local/lib
to the correct file location:
/usr/local/lib> ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib libpq.5.dylib
If you run into a similar issue, just examine the output of pg_config
, and see if you can place a symlink to the correct file location in one of the directories that is already specified by LDFLAGS.
rails - postgres error: Reason: Incompatible library version: libpq.5.dylib requires version 1.0.0 or later,
I ran into this also, but was able to fix it following the instructions on python pip install psycopg2 install error.
First, make sure you have the most recent version of OpenSSL installed:
MacBook Pro:~> openssl version -a
OpenSSL 1.0.0c 2 Dec 2010
built on: Mon Jan 3 17:26:21 PST 2011
platform: darwin64-x86_64-cc
options: bn(64,64) rc4(ptr,char) des(idx,cisc,16,int) idea(int) blowfish(idx)
compiler: /usr/bin/gcc-4.2 -fPIC -fno-common -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -arch x86_64 -O3 -DL_ENDIAN -DMD32_REG_T=int -Wall
OPENSSLDIR: "/opt/local/etc/openssl"
...and note the OPENSSLDIR. On my system, it's in /opt/local/, because I installed it via MacPorts. I just needed to update the symlinks in /usr/lib/ for libssl.dylib and libcrypto.dylib so that they pointed to the correct versions in /opt/local/lib instead of the old version in usr/lib:
MacBook Pro:~> ls -la /usr/lib/libssl.dylib
lrwxr-xr-x 1 root wheel 33 Aug 17 12:25 /usr/lib/libssl.dylib -> /opt/local/lib/libssl.1.0.0.dylib
MacBook Pro:~> ls -la /usr/lib/libcrypto.dylib
lrwxr-xr-x 1 root wheel 36 Aug 17 12:28 /usr/lib/libcrypto.dylib -> /opt/local/lib/libcrypto.1.0.0.dylib
You can create the links by using the ln
command:
sudo ln -s /path/to/postgres/install/lib/libcrypto.dylib /usr/lib/libcrypto.dylib
sudo ln -s /path/to/postgres/install/lib/libssl.dylib /usr/lib/libssl.dylib
Impossible to Install PG gem on my mac with Mavericks
If you want to avoid using MacPorts, you can download the Postgres App and place it into the Application directory.
Then, specify the location of newly downloaded pg_config
:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
If you run in to missing headers problem, try specifying the include
directory of the app:
gem install pg -- --with-pg-include='/Applications/Postgres.app/Contents/Versions/latest/include/'
Library not loaded: /usr/local/lib/libpq.5.4.dylib
Some time after I posted this question, I found that libpq.5.4.dylib
resides in /Library/PostgreSQL/9.1/lib/
.
So, I created the following link:lrwxr-xr-x 1 sathishvc admin 43 Jan 28 23:40 /usr/local/lib/libpq.5.4.dylib -> /Library/PostgreSQL/9.1/lib/libpq.5.4.dylib
.
This solved the problem then.
Ruby on Rails / PostgreSQL - Library not loaded error when starting server
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.
Library not loaded: libpq.5.dylib
You may need to remove and re-install the pg
gem, so it's compiled against the correct version of Postgres.
Installing pg gem; ERROR: Failed to build gem native extension
Using homebrew fixed this for me:
gem uninstall pg
brew install apple-gcc42
gem install pg
EDIT: I also manually installed "devtools"
xcode-select --install
Related Topics
How to Run Ruby on Rails with Ruby 1.9X
Replacing Text in One CSV Column Using Fastercsv
How to Build a Ruby Hash Out of Two Equally-Sized Arrays
Find Out If an Ip Is Within a Range of Ips
Ruby: How to Concatenate Array of Arrays into One
Are There Any Ipython-Like Shells for Ruby or Rails
How to Access Url Helper from Rails Module
Implementation of "Remember Me" in a Rails Application
How to Convert a Comma-Separated String into an Array
Can You Use Semicolons in Ruby
Invalid Multibyte Char (Utf-8) Error, Ruby
Why Doesn't 'User Installation Directory' Match with the Ruby Version
Manually Retry Job in Delayed_Job
Installing Ruby 1.9.1 on Ubuntu
Attr_Accessor on Class Variables
Engine's Assets with Rails 3.1
Force Bundle Install to Use Https:// Instead of Git:// for Github-Based Gems