57

When I try to run the command:

import psycopg2

I get the error:

ImportError: dlopen(/Users/gwulfs/anaconda/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Users/gwulfs/anaconda/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: image not found

So far I have tried brew install openssl and have referenced (with no luck):

psycopg2 installation error - Library not loaded: libssl.dylib

http://joshuakehn.com/2013/10/13/Postgresapp-and-psycopg2-on-OS-X.html

Psycopg2 image not found

Community
  • 1
  • 1
Gideon
  • 949
  • 1
  • 11
  • 17
  • 4
    If you are using bash (default shell on OS X), add the following line to `~/.bash_profile`: `export DYLD_FALLBACK_LIBRARY_PATH=$HOME/anaconda/lib:$DYLD_FALLBACK_LIBRARY_PATH` . Or if you're using the fish shell, add the following to your `config.fish` (normally located at `~/.config/fish/config.fish`): `set -x DYLD_FALLBACK_LIBRARY_PATH $HOME/anaconda/lib $DYLD_FALLBACK_LIBRARY_PATH` . – Answeror Apr 10 '15 at 16:55
  • 2
    Same problem and solved in this post: http://stackoverflow.com/questions/23944770/uwsgi-can-not-load-libssl-1-0-0-dylib/23945292#23945292 – Xing Shi Jul 18 '15 at 13:59
  • @xing-shi is right! Please don't mess around with symbolic linking (mentioned in other posts with this problem), or even environment variables (`$DYLD_FALLBACK_PATH`). Check out my solution below: http://stackoverflow.com/a/36872624/5076471 – alichaudry Apr 26 '16 at 18:11

20 Answers20

77

Instead of playing with symlinks in system library dirs, set the $DYLD_FALLBACK_LIBRARY_PATH to include the anaconda libraries. eg:

export DYLD_FALLBACK_LIBRARY_PATH=$HOME/anaconda/lib/:$DYLD_FALLBACK_LIBRARY_PATH
X.L.
  • 881
  • 6
  • 6
  • woo This works! I think this should be the accepted answer. The other one looks dangerous. – P i Dec 28 '15 at 21:08
  • 1
    This worked for me too! I'm using anaconda python with psycopg2 on Mac OS X El Capitan. – mgig Jan 14 '16 at 19:28
  • 1
    thank you so much. hit at least 7 answers on stackoverflow before finding this. Same situation with anaconda and El Captain. Glad I read the comments before removing things on the answer above. – Oeufcoque Penteano Apr 18 '16 at 09:53
  • 1
    My anaconda folder is not at $HOME so just note that the export command needs to reflect the anaconda folder location – Seth_P Sep 30 '16 at 18:12
  • The pitfalls of copy-pasta - I was afraid this didn't work for me either ...until I realized I have `anaconda3` not `anaconda`. D'oh. Thanks for the help! – dwanderson Dec 22 '16 at 16:20
  • I put the line in ~/.bash_profile and it worked. Thanks! – makeasy Feb 10 '17 at 21:24
  • Sadly, things broke and I'm back. This no longer works (OS Sierra, Anaconda 3) – dwanderson Feb 16 '17 at 20:02
  • This worked for me like a charm. And I'd been working on it for hours. Thank you. – SonOfSeuss Mar 23 '18 at 03:03
  • 1
    Thanks for the tip. I added openssl lib path rather than Conda lib (/usr/local/Cellar/openssl/1.0.2q/lib/) to this DYLD_FALLBACK_LIBRARY_PATH and that fixed my problem. – sepideh Dec 19 '18 at 14:10
33

After Homebrew wouldn't allow me to force link openssl the following worked fine:

pip install --global-option=build_ext \
            --global-option="-I/usr/local/opt/openssl/include" \
            --global-option="-L/usr/local/opt/openssl/lib" psycopg2

(this installation succeeded in a virtualenv on macOS)

NSTJ
  • 3,687
  • 2
  • 24
  • 34
31

EDIT: potentially dangerous, read comments first!

See a much safer answer below: https://stackoverflow.com/a/30726895/308315


I ran into this exact issue about an hour after you posted it and just figured it out. I am using Mac OS X Yosemite, Python 2.7, and the Postgresql app.

There seems to be a non-working symlink set by default (or I introduced it while troubleshooting), to fix it first remove the incorrect links:

$ sudo rm /usr/lib/libssl.1.0.0.dylib
$ sudo rm /usr/lib/libcrypto.1.0.0.dylib

Then re-link them with (replace YOURUSERNAME with your Mac user name. I found it helpful to use tab to complete each step, to confirm the directory):

$ sudo ln -s /Users/YOURUSERNAME/anaconda/lib/libssl.1.0.0.dylib /usr/lib
$ sudo ln -s /Users/YOURUSERNAME/anaconda/lib/libcrypto.1.0.0.dylib /usr/lib

I believe the other solutions didn't work for you because your version is in anaconda.

Community
  • 1
  • 1
Scott Brenstuhl
  • 529
  • 4
  • 6
  • 1
    The only concern I'd have is that you might now wind up using the anaconda versions of those libraries as opposed to the operating system versions, even for applications that are not python or anaconda. – ipaul Mar 12 '15 at 20:55
  • 3
    Don't rm, just mv. You may find out that you don't actually have files that you want to link to in the next step. – Chris P Jun 25 '15 at 05:19
  • 10
    DON'T DO THIS. You can very easily break your system by doing this. – asmeurer Aug 24 '15 at 16:16
  • When you say DON'T DO THIS you are referring to MV the files not creating the symlink correct? – Dennis Sep 30 '15 at 18:14
  • 2
    X.L.'s answer worked for me. I would also be cautious with this answer. – P i Dec 28 '15 at 21:09
27

After bashing my head against the wall for a couple hours, these two solutions are guaranteed to work:

Option 1. This solves our problem without messing around with environment variables. Run this in your shell:

brew install --upgrade openssl
brew unlink openssl && brew link openssl --force

Boom! This upgrades the symbolic links in /usr/local for libssl and libcrypto. Now import psycopg2 works like a charm.

Option 2. If for some reason you would like to maintain the current symbolic links in usr/local, run this command in your shell:

export DYLD_FALLBACK_LIBRARY_PATH=$HOME/anaconda/lib/:$DYLD_FALLBACK_LIBRARY_PATH

Just make sure to replace $HOME/anaconda/lib above with the actual lib path. In my case, this was $HOME/miniconda2/envs/ali/lib.

This will only work for the shell/bash session you're currently in. To make the change persistent, add the export statement to your ~/.bash_profile or ~/.bashrc file.

Thoughts: IMO #1 is the proper way to deal with this problem, but I left #2 in case some people prefer working with environment variables rather than fixing symbolic links (if, for example, they have software with a dependency on the older openssl file versions).

alichaudry
  • 1,863
  • 1
  • 15
  • 24
  • At brew's output suggestion, I had to change the second line to: – uchuugaka Aug 06 '16 at 13:58
  • `brew unlink openssl && brew link --overwrite openssl --force` – uchuugaka Aug 06 '16 at 13:58
  • 1
    Trying both solution 1 and @uchuugaka's solution failed for me: I'm on OSX Sierra, and when I try to run either of those `brew link` commands, I get an error: `Linking keg-only openssl means you may end up linking against the insecure [...]` – dwanderson Dec 22 '16 at 16:02
  • 1
    Could be new change in homebrew. That project updates a lot. – uchuugaka Dec 25 '16 at 12:48
  • 1
    #1 option doesn't work - Warning: Refusing to link: openssl Linking keg-only openssl means you may end up linking against the insecure, deprecated system OpenSSL while using the headers from Homebrew's openssl. Instead, pass the full include/library paths to your compiler e.g.: -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib – Victor G. Jan 29 '17 at 12:37
12

conda install psycopg works for me. It updates the following packages The following packages will be UPDATED:

conda:      3.19.1-py27_0 --> 4.0.5-py27_0
openssl:    1.0.2f-0      --> 1.0.2g-0
pip:        8.0.2-py27_0  --> 8.1.0-py27_0
setuptools: 19.6.2-py27_0 --> 20.2.2-py27_0
wheel:      0.26.0-py27_1 --> 0.29.0-py27_0
jackyen_2000
  • 129
  • 1
  • 2
  • This works for me too. I had the same issue, I had previously done ```pip install psycopg2``` with the conda pip: ```$ which pip # //anaconda/bin/pip``` – gmajivu Jun 07 '16 at 09:07
9

I was having this issue on Mac, trying ln -s was giving me ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted I didn't want to mess with my system. Instead What worked for me is to simply install psycopg2-binary : pip install psycopg2-binary

This installed psycopg2-binary-2.8.3 version

ajouhari
  • 91
  • 1
  • 1
8

In relation to X.L.'s answer above, I didn't want to use Anaconda when I'm already using pip, so I just gave it the path to the Postgres libraries which worked for me (I'm using PostgreSQL.app on Mac OS 10.10)...

export DYLD_FALLBACK_LIBRARY_PATH=/Library/PostgreSQL/9.5/lib:$DYLD_FALLBACK_LIBRARY_PATH
Jim Bob
  • 419
  • 7
  • 5
4

I had to vary Scott Brennstuhl's answer a little: 1. Remove broken symlinks:

$ sudo rm /usr/lib/libssl.1.0.0.dylib
$ sudo rm /usr/lib/libcrypto.1.0.0.dylib
$ sudo rm /usr/lib/libpq.5.dylib
  1. Relink with postgres' included drivers:
$ sudo ln -s   /Applications/Postgres.app/Contents/Versions/9.4/lib/libssl.1.0.0.dylib /usr/lib    
$ sudo ln -s /Applications/Postgres.app/Contents/Versions/9.4/lib/libcrypto.1.0.0.dylib /usr/lib
$ sudo ln -s /Applications/Postgres.app/Contents/Versions/9.4/lib/libpq.5.dylib  /usr/lib
josliber
  • 41,865
  • 12
  • 88
  • 126
tyton
  • 49
  • 2
4

My flavor of setup was a little different than the OP: I'm using Postgres.app on Mac and am within a virtualenv; but the symptoms were similar.

For me, this occurred right after updating my Postgres.app from 9.3 to 9.5 on my local, and the error clearly showed the psycopg2 path for libssl.1.0.0.dylib was pointing to the old 9.3 data directory location (the image referenced in this error?). Adding weird things to my ENV or removing symlinks I'm not sure the impact of definitely didn't feel right to me. I solved it by uninstalling then re-installing psycopg2 the same way I had when it was working - something that doesn't feel very dangerous at all:

 # In my virtualenv
 pip uninstall psycopg2
 pip install psycopg2

Then I was all good!

cwnewhouse
  • 169
  • 1
  • 7
  • Worked for me once I cleaned pip cache (`pip cache purge`) - the built version in cache was still pointing to the old libssl path. – tmuguet Jul 21 '20 at 10:03
3

Do the following to resolve Library not loaded:libssl.1.0.0.dylib error if you have openssl in /usr/local/Cellar directory

  1. sudo cp /usr/local/Cellar/openssl/<<version>>/lib/libssl.1.0.0.dylib /usr/lib

  2. After doing step 1, if you still get Library not loaded:libcrypto.1.0.0.dylib error. Do the following
        sudo cp /usr/local/Cellar/openssl/<<version>>/lib/libcrypto.1.0.0.dylib /usr/lib

Community
  • 1
  • 1
yottabytt
  • 1,774
  • 15
  • 14
3

brew reinstall openssl postgres did the trick for me

drtf
  • 1,728
  • 2
  • 18
  • 18
  • I'm using a remote postgres server, but this still did the trick for me. The postgres installation corrected or added the necessary symlinks, I imagine. I like this, as I want homebrew to manage those libs as far as possible. – dbenton Jun 24 '20 at 16:38
3

I had the same problem when I updated openssl from 1.0.0 to 1.1.1d, and this fixed my problem:

brew upgrade postgresql
onodi
  • 56
  • 3
2

I tried pip install psycopg2 which was giving similar issues. Then I tried conda install psycopg2, which worked! Also make sure the pip you are using belongs to anaconda (which pip)

2

So first for me openssl re-install never worked. It was quite irritating that all of the above answers failed for me. To be sure that it's a openssl issue, first, install psycopg2-binary using pip

  pip install psycopg2-binary

After installing psycopg2-binary, if you're getting error like ld: library not found for -lssl then do the following

  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

if these didn't work then you can try to upgrade psycopg2 and re-check that issue still there or not.

  pip install psycopg2 --upgrade

if all the above didn't worked then only try reinstalling openssl as mentioned in all above answers.

Nishant Singh
  • 353
  • 2
  • 5
1

I am using Mac OS Sierra: and got this error:

 Library not loaded: libssl.1.1.dylib

I found this library in

 /Library/PostgreSQL/11/lib

I found this solution in internet:

 export DYLD_LIBRARY_PATH=/Library/PostgreSQL/11/lib

But this is not a permanent solution. As I have to run the above command whenever I restart my server.

Add the line: export DYLD_LIBRARY_PATH=/Library/PostgreSQL//lib
to ~/.bash_profile

Community
  • 1
  • 1
1

The solution that worked for me (https://stackoverflow.com/a/30726895 did not) was installing psycopg2-binary with: pip install psycopg2-binary==2.7.6

This is on MacOS Mohave.

willk
  • 3,028
  • 1
  • 21
  • 41
1

Simple solution: this pip command will install a package at a specific location:

pip install psycopg2-binary -t PATH

Where PATH is a path that you specify.

To test this, install the package in a folder on your desktop. Then put a python script in the same folder that will import psycopg2. It should work with the script being in the same location as the psycopg2 package.

Comments:

This reason we need psycopg2-binary, according to old documentation that I found online:

"The binary packages come with their own versions of a few C libraries, among which libpq and libssl , which will be used regardless of other libraries available on the client: upgrading the system libraries will not upgrade the libraries used by psycopg2 . Please build psycopg2 from source if you want to maintain binary upgradeability."

Source: https://access.crunchydata.com/documentation/psycopg2/2.7.3/install.html#install-from-source

I realized the issue has been that python's site package directory needs to be referenced. The location for this on my computer is:

/Users/my_name/Library/Python/3.7/lib/python/site-packages

Now, if you want to use IDLE or say PyCharm as I have been, the installation of psycopg2-binary needs to target this site directory. Additionally, you'll find two folders that appear after installing psycopg2-binary called: psycopg2, psycopg2_binary-2.8.4.dist-info

I spent a long time investigating this issue. The other methods out there were not not resolving the issue as seen above regarding lib ssl and reason image not found.

Setup: macOS Catalina, Python 3.7, PyCharm/IDLE project on Desktop, openssl@1.1/1.1.1d

mybrave
  • 1,285
  • 3
  • 14
  • 32
Swan Toma
  • 116
  • 5
0

After trying for more than a day I came to the below solution.

  • brew reinstall openssl@1.0
  • disable csrutil -> google it how to disable it, so that we could copy something
    to /usr/lib
  • copy libssl.1.0.0.dylib to /usr/lib I did- sudo cp /usr/local/Cellar/openssl/1.0.2s/lib/libssl.1.0.0.dylib /usr/lib
  • copy libcrypto.1.0.0.dylib to /usr/lib I did- sudo cp /usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.1.0.0.dylib /usr/lib

Similarly, if you face issue for Library not loaded: libssl.1.0.0.dylib just change the version from 1.0 to 1.1 of openssl and copy libssl.1.1 instead libssl.1.0 and libcrypto.1.1 instead libcrypto.1.0.0

Done you are all set to enjoy psycopg2 in mac.

pyAddict
  • 1,146
  • 10
  • 13
0

Homebrew upgrades default openssl from v1.0 to v1.1. If you tried @Scott solution to upgrade openssl:

brew install --upgrade openssl
brew unlink openssl && brew link openssl --force

you may run into ssh problem. You need to upgrade openssh as well.

brew upgrade openssh

according to this blog: https://blog.junjizhi.com/all/2019/12/17/git-fetch-libssl-error.html

user10375
  • 453
  • 1
  • 4
  • 9
0

I encountered this problem after running brew upgrade, which updated openssl from 1.0.0 to 1.1.1. That causes the error in the question because I'm using pyscopg 2.7.4, which is pinned to openssl@1.0. More recent versions of psycopg (>2.8) are pinned to openssl@1.1.

So your options in this situation are to upgrade your psycopg version to 2.8.1 or later (in your Pipfile/Pipfile.lock, etc.), or force brew to keep using openssl@1.0 with this command, taken from https://github.com/Homebrew/homebrew-core/issues/47348#issuecomment-560001981:

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/64555220bfbf4a25598523c2e4d3a232560eaad7/Formula/openssl.rb -f
Edward D'Souza
  • 1,745
  • 19
  • 21