postgresql-libpq icon indicating copy to clipboard operation
postgresql-libpq copied to clipboard

Can't install on Mac OS X: libpq.5.dylib not found

Open ijmorlan opened this issue 11 years ago • 16 comments

I'm trying to install postgresql-libpq on Mac OS X 10.6.8. I get:

00:06 [email protected]$ cabal install postgresql-libpq
Resolving dependencies...
[1 of 1] Compiling Main             ( /var/folders/Lh/LhVs9ls4Hme1jR+t1DJbm++++TQ/-Tmp-/postgresql-libpq-0.8.2.2-42202/postgresql-libpq-0.8.2.2/Setup.hs, /var/folders/Lh/LhVs9ls4Hme1jR+t1DJbm++++TQ/-Tmp-/postgresql-libpq-0.8.2.2-42202/postgresql-libpq-0.8.2.2/dist/setup/Main.o )
Linking /var/folders/Lh/LhVs9ls4Hme1jR+t1DJbm++++TQ/-Tmp-/postgresql-libpq-0.8.2.2-42202/postgresql-libpq-0.8.2.2/dist/setup/setup ...
Configuring postgresql-libpq-0.8.2.2...
Building postgresql-libpq-0.8.2.2...
Preprocessing library postgresql-libpq-0.8.2.2...
dyld: Library not loaded: libpq.5.dylib
  Referenced from: /private/var/folders/Lh/LhVs9ls4Hme1jR+t1DJbm++++TQ/-Tmp-/postgresql-libpq-0.8.2.2-42202/postgresql-libpq-0.8.2.2/dist/build/Database/PostgreSQL/LibPQ_hsc_make
  Reason: image not found
running dist/build/Database/PostgreSQL/LibPQ_hsc_make failed (exit code 5)
command was: dist/build/Database/PostgreSQL/LibPQ_hsc_make  >dist/build/Database/PostgreSQL/LibPQ.hs
cabal: Error: some packages failed to install:
postgresql-libpq-0.8.2.2 failed during the building phase. The exception was:
ExitFailure 1
00:06 [email protected]$ 

Now, my libpq is not in /usr/lib, so maybe that's an issue. But:

00:06 [email protected]$ pg_config --libdir
/Library/PostgreSQL/9.2/lib
00:08 [email protected]$ 

So I figure that the postgresql-libpq configure stuff should be able to find it.

But maybe not, so:

00:08 [email protected]$ cabal install --extra-lib-dirs=/Library/PostgreSQL/9.2/lib postgresql-libpq
Resolving dependencies...
[... same result ...]

I tried symlinking /Library/PostgreSQL/9.2/lib/libpq.5.dylib into /usr/lib, but then it complained about the version of libiconv. Sure enough, the version in /usr/lib is the slightly older version it can't use, while the one in /Library/PostgreSQL/9.2/lib is the needed version. I don't really want to go randomly upgrading stuff in /usr/lib.

Incidentally, my PostgreSQL installation was made by using the one click installer available from http://www.postgresql.org/download/macosx/.

This is one of those confusing cases where I can't seem to find anybody else asking the same question, but I don't see that I'm doing anything special that would give me a different experience from most other people. Any hints would be much appreciated.

ijmorlan avatar Mar 07 '13 05:03 ijmorlan

Thanks for the report; unfortunately I personally won't be able to help you much here. But if you do find a solution, please share, and I would be eager for any patches as well.

We really need to put together a guide for compiling postgresql-libpq on MacOS and Windows.

lpsmith avatar Mar 07 '13 13:03 lpsmith

On Thu, 7 Mar 2013, Leon P Smith wrote:

Thanks for the report; unfortunately I personally won't be able to help you much here. But if you do find a solution, please share, and I would be eager for any patches as well.

We really need to put together a guide for compiling postgresql-libpq on MacOS and Windows.

Ok, thanks. I took a closer look at the postgresql-libpq package, in particular the Setup.hs, and I think I believe that you are already doing what I was thinking - you appear to invoke pg_config --libdir as well as pg_config --includedir, and it gets through compilation, so it seems the include dir is being used, just not the libdir.

So now I'm going to try to figure out what "cabal install" is doing and find out why it's not working.

ijmorlan avatar Mar 07 '13 15:03 ijmorlan

for the purposes of building a single package, cabal-install mostly just invokes the Cabal commands in a particular sequence (with some further settings in your ~/.cabal/config file, which cabal proper ignores)

So you should try a vanilla cabal build process, without cabal-install, and try cranking up the verbosity a notch or two.

If worse comes to worse, a handy trick I've found when trying to understand cabal is to put print statements in the callback hooks in Setup.hs. The data structures that these callbacks are passed are quite large, so if you want to examine them I recommend using groom to pretty print them.

That's pretty generic advice, but I hope it helps!

lpsmith avatar Mar 07 '13 17:03 lpsmith

On Thu, 7 Mar 2013, Leon P Smith wrote:

for the purposes of building a single package, cabal-install mostly just invokes the Cabal commands in a particular sequence (with some further settings in your ~/.cabal/config file, which cabal proper ignores)

So you should try a vanilla cabal build process, without cabal-install, and try cranking up the verbosity a notch or two. [....]

Thanks for the hints. Yes, I found the "verbose" flags. I dug around a bit and it turns out that the extra lib dir is not getting set as an rpath in the intermediate executable that hsc2hs generates and runs. By using the Mac OS X install_name_tool utility on the intermediate executable, I was able to get it to run and dump out a bunch of .hs code.

If I can figure out how to tell cabal to arrange for the intermediate executable to be adjusted the same way I did manually, I think I would be able to build the package.

I created a ticket on the GHC Trac site because it seems like hsc2hs perhaps should take responsibility for setting the rpath on the intermediate executable.

http://hackage.haskell.org/trac/ghc/ticket/7749

Of course, I can't really say, because I don't know enough about hsc2hs or Mac OS X library handling.

ijmorlan avatar Mar 07 '13 20:03 ijmorlan

I just read the ticket; my one comment is that I don't know how many users of postgresql-libpq there are on MacOSX. I haven't heard any reports of people using it there, but that doesn't mean there aren't. I don't have a Macintosh at the moment though.

I do know there is at least two people who have tried to use postgresql-libpq on Windows though, one who has managed to get things compiled and one who hasn't. I don't use Windows very often, so I don't know much about this process either.

lpsmith avatar Mar 08 '13 04:03 lpsmith

Any news on this? Got same issue here, running OS X 10.9.2:

Building postgresql-libpq-0.8.2.6...
Preprocessing library postgresql-libpq-0.8.2.6...
dyld: Library not loaded: /usr/local/opt/postgresql/lib/libpq.5.6.dylib
  Referenced from: /private/var/folders/3f/zqnpxfzn4r147hdls9h9vkb80000gn/T/postgresql-libpq-0.8.2.6-21334/postgresql-libpq-0.8.2.6/dist/dist-sandbox-ea416cd3/build/Database/PostgreSQL/LibPQ_hsc_make
  Reason: image not found
running dist/dist-sandbox-ea416cd3/build/Database/PostgreSQL/LibPQ_hsc_make failed (exit code 5)
command was: dist/dist-sandbox-ea416cd3/build/Database/PostgreSQL/LibPQ_hsc_make  >dist/dist-sandbox-ea416cd3/build/Database/PostgreSQL/LibPQ.hs

alanpog avatar Feb 27 '14 21:02 alanpog

Well, again I won't be able to help much, although I would recommend trying this with the verbosity turned up, as well as trying to invoke runghc Setup.hs configure runghc Setup.hs build yourself.

@ijmorlan, did you get this working, and if so, how?

lpsmith avatar Feb 27 '14 22:02 lpsmith

Also, I am curious, could you post the output of the pg_config command?

And this may not help, but you might look at this OpenBSD thread, which suggests we should modify Setup.hs to use the output of pg_config --libs to set the Extra-Libraries configuration parameter correctly. This may or may not be involved in a proper solution for MacOS X, but perhaps this thread might give you some clues.

lpsmith avatar Feb 27 '14 23:02 lpsmith

The output of pg_config is copied below:

 $ pg_config
BINDIR = /usr/local/Cellar/postgresql/9.3.3/bin
DOCDIR = /usr/local/Cellar/postgresql/9.3.3/share/doc/postgresql
HTMLDIR = /usr/local/Cellar/postgresql/9.3.3/share/doc/postgresql
INCLUDEDIR = /usr/local/Cellar/postgresql/9.3.3/include
PKGINCLUDEDIR = /usr/local/Cellar/postgresql/9.3.3/include
INCLUDEDIR-SERVER = /usr/local/Cellar/postgresql/9.3.3/include/server
LIBDIR = /usr/local/Cellar/postgresql/9.3.3/lib
PKGLIBDIR = /usr/local/Cellar/postgresql/9.3.3/lib
LOCALEDIR = /usr/local/Cellar/postgresql/9.3.3/share/locale
MANDIR = /usr/local/Cellar/postgresql/9.3.3/share/man
SHAREDIR = /usr/local/Cellar/postgresql/9.3.3/share/postgresql
SYSCONFDIR = /usr/local/Cellar/postgresql/9.3.3/etc
PGXS = /usr/local/Cellar/postgresql/9.3.3/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--disable-debug' '--prefix=/usr/local/Cellar/postgresql/9.3.3' '--datadir=/usr/local/Cellar/postgresql/9.3.3/share/postgresql' '--docdir=/usr/local/Cellar/postgresql/9.3.3/share/doc/postgresql' '--enable-thread-safety' '--with-bonjour' '--with-gssapi' '--with-krb5' '--with-ldap' '--with-openssl' '--with-pam' '--with-libxml' '--with-libxslt' '--with-ossp-uuid' '--with-python' '--with-perl' '--with-tcl' 'CC=clang' 'CFLAGS=-I/usr/local/Cellar/ossp-uuid/1.6.2/include' 'LDFLAGS=-L/usr/local/Cellar/ossp-uuid/1.6.2/lib' 'LIBS=-luuid'
CC = clang
CPPFLAGS = -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2
CFLAGS = -I/usr/local/Cellar/ossp-uuid/1.6.2/include -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL =
LDFLAGS = -L../../../src/common -L/usr/local/Cellar/ossp-uuid/1.6.2/lib -Wl,-dead_strip_dylibs
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgport -lpgcommon -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lm -luuid
VERSION = PostgreSQL 9.3.3

alanpog avatar Feb 28 '14 19:02 alanpog

Running config and build manually I get this, which is really the original error message:

Alans-MacBook-Pro:postgresql-libpq alanpog$ runghc Setup.hs configure
Configuring postgresql-libpq-0.8.2.6...
Alans-MacBook-Pro:postgresql-libpq alanpog$ runghc Setup.hs build
Building postgresql-libpq-0.8.2.6...
Preprocessing library postgresql-libpq-0.8.2.6...
dyld: Library not loaded: /usr/local/opt/postgresql/lib/libpq.5.6.dylib
  Referenced from: /Users/alanpog/code/prowdfunder/postgresql-libpq/dist/build/Database/PostgreSQL/LibPQ_hsc_make
  Reason: image not found
running dist/build/Database/PostgreSQL/LibPQ_hsc_make failed (exit code 5)
command was: dist/build/Database/PostgreSQL/LibPQ_hsc_make  >dist/build/Database/PostgreSQL/LibPQ.hs

alanpog avatar Feb 28 '14 20:02 alanpog

Ok, so hsc2hs takes a .hsc source file and generates a C program that when compiled and run, generates a Haskell program. It looks like this C program has been successfully compiled, but fails to run because it's looking for /usr/local/opt/postgresql/lib/libpq.5.6.dylib when (inferring from your pg_config output) it looks more like it's actually located at something like /usr/local/Cellar/postgresql/lib/libpq.5.6.dylib Referring to a key paragraph from ijmorlan above:

Thanks for the hints. Yes, I found the "verbose" flags. I dug around a bit and it turns out that the extra lib dir is not getting set as an rpath in the intermediate executable that hsc2hs generates and runs. By using the Mac OS X install_name_tool utility on the intermediate executable, I was able to get it to run and dump out a bunch of .hs code.

I'm a little unclear on all the details here, but RPATH is a hard-wired dylib search path, embedded in the binary. I'm hoping that a cabal build afterwards wouldn't rebuild that executable, but I don't know.

Alternatively, you might be able to fix this by adjusting your DYLD_LIBRARY_PATH environment variable regardless of what cabal decides to do; take a look at man dyld for more info. In any case, setting DYLD_LIBRARY_PATH should hopefully work even if cabal rebuilds the C program every time.

That still leaves the question of what happens to executables that use postgresql-libpq, as you probably don't want to use DYLD_LIBRARY_PATH "in production". You could probably adjust the RPATH yourself. As for a proper fix, I'm still pretty much in the dark.

lpsmith avatar Feb 28 '14 21:02 lpsmith

I've experienced similar problems on OS X 10.8.5. The workaround I've found that resolves this problem for me (this may not be universal) is to create symbolic links to libpq.dylib and libpq.5.dylib in /usr/local/lib. Then, I have to export DYLD_LIBRARY_PATH=/usr/local/lib, and provide the --extra-lib-dirs=/usr/local/lib command line parameter to Cabal install and configure commands.

By setting export DYLD_PRINT_LIBRARIES=true I've been able to confirm that /usr/local/lib/libpq.dylib is being loaded when the postgresql-libpq-0.9.0.1 package is loaded, and that the shared libraries that libpq.dylib depends on are being loaded from /Library/PostgreSQL/9.2./lib. The correct shared libraries that libpq.dylib depends on are being loaded, because they are specified with the @loader_path variable in libpq.dylib, which can be verified with otool -L libpq.dylib.

Notably, the workaround does not work if /Library/PostgreSQL/9.2/bin/pg_config is on the path. With the wrong version of pg_config on the path, not the version in /Library/PostgreSQL/9.2/bin/, the build does produce the following error.

ld: warning: ignoring file /usr/lib/libpq.dylib, file was built for unsupported file format ( 0xcf 0xfa 0xed 0xfe 0x 7 0x 0 0x 0 0x 1 0x 3 0x 0 0x 0 0x 0 0x 6 0x 0 0x 0 0x 0 ) which is not the architecture being linked (i386): /usr/lib/libpq.dylib

This error isn't surprising, because /usr/lib/libpq.dylib is 64-bit only. I haven't looked into why this doesn't fail the build, but it doesn't.

If /Library/PostgreSQL/9.2/bin/pg_config is on the path, I find I get library not found/image not found errors for libiconv.2.dylib that I have not been able to resolve.

Hopefully this helps until a real solution is found.

c-lewis avatar Dec 12 '14 16:12 c-lewis

Hello all,

I had a somewhat similar issue trying to install this library using stack, but as this is the only relevant result I found while googling, I thought it might be useful to post what I did to resolve it here. My issue is different because instead of Reason: image not found for libpq.5.dylib, I was getting the following error:

--  While building package postgresql-libpq-0.9.1.1 using:
      /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/stack1880/postgresql-libpq-0.9.1.1/.stack-work/dist/x86_64-osx/Cabal-1.22.4.0/setup/setup --builddir=.stack-work/dist/x86_64-osx/Cabal-1.22.4.0 build --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
    Logs have been written to: PROJECT_LOCATION/.stack-work/logs/postgresql-libpq-0.9.1.1.log

    [1 of 1] Compiling Main             ( /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/stack1880/postgresql-libpq-0.9.1.1/Setup.hs, /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/stack1880/postgresql-libpq-0.9.1.1/.stack-work/dist/x86_64-osx/Cabal-1.22.4.0/setup/Main.o )
    Linking /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/stack1880/postgresql-libpq-0.9.1.1/.stack-work/dist/x86_64-osx/Cabal-1.22.4.0/setup/setup ...
    Configuring postgresql-libpq-0.9.1.1...
    Building postgresql-libpq-0.9.1.1...
    Preprocessing library postgresql-libpq-0.9.1.1...
    dyld: Library not loaded: libiconv.2.dylib
      Referenced from: /private/var/folders/wb/vvtxjd7n2qz02f571yjyr9j40000gs/T/stack1880/postgresql-libpq-0.9.1.1/.stack-work/dist/x86_64-osx/Cabal-1.22.4.0/build/Database/PostgreSQL/LibPQ_hsc_make
      Reason: Incompatible library version: LibPQ_hsc_make requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0
    running .stack-work/dist/x86_64-osx/Cabal-1.22.4.0/build/Database/PostgreSQL/LibPQ_hsc_make failed (exit code -5)
    command was: .stack-work/dist/x86_64-osx/Cabal-1.22.4.0/build/Database/PostgreSQL/LibPQ_hsc_make  >.stack-work/dist/x86_64-osx/Cabal-1.22.4.0/build/Database/PostgreSQL/LibPQ.hs

Because I had installed Postgresql 9.4 using the one-click installer on OSX, all relevant Postgresql files were in /Library/PostgreSQL/9.4/. Before I even received this error, I had to add /Library/PostgreSQL/9.4/bin to my PATH so pg_config could be found.

Anyway, the installer was finding libiconv.2.dylib, but that was the wrong one:

$ otool -L  /usr/lib/libiconv.2.dylib
/usr/lib/libiconv.2.dylib:
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.0.0)

The one I needed was in /Library/PostgreSQL/9.4/lib:

otool -L  /Library/PostgreSQL/9.4/lib/libiconv.2.dylib 
/Library/PostgreSQL/9.4/lib/libiconv.2.dylib:
    libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

Recent restrictions to OSX make it impossible to just go into /usr/lib and symlink the proper one in, but that didn't seem like that great of an idea anyway. Thus, inspired by the response from @c-lewis above, I symlinked all the dylibs into /usr/local/lib:

$ cd /usr/local/lib
$ sudo ln -s /Library/PostgreSQL/9.4/lib/*dylib .

Finally, I used stack to install with the extra-lib-dirs argument and it worked fine:

stack install --extra-lib-dirs=/usr/local/lib postgresql-libpq-0.9.1.1

I am not sure how relevant this will be to the Reason: image not found issue for a different dylib, but I thought there was a chance it might help someone, and so it seemed worth posting here.

erewok avatar Apr 03 '16 19:04 erewok

Thank you @pellagic-puffbomb —this saved my day. 👍

buggymcbugfix avatar Dec 28 '16 03:12 buggymcbugfix

Hi @pellagic-puffbomb. Thanks for your helpful answer!

For my own understanding, could you please explain why did you need to run stack install --extra-lib-dirs=/usr/local/lib postgresql-libpq-0.9.1.1?

Thanks

kevinmeredith avatar Apr 25 '17 12:04 kevinmeredith

@kevinmeredith stack didn't seem to be able to find the dylib without my telling it to also look in /usr/local/lib. I found out that I could also add that to extra-lib-dirs in my stack.yaml for my project and that I could also add it to my global (for my user) stack.yaml.

erewok avatar Apr 26 '17 02:04 erewok