psycopg2のインストールエラー ld: library not found for -lssl
Python3.8にpsycopg2をインストールしようとしたところ、エラーが出て失敗した。 インストールしようとしたのはpsycopg2の2.8で、最新の2.9は普通にインストールできた。
大量のエラーメッセージが表示されるが重要なのは以下の部分。
ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'clang' failed with exit status 1
どうやらビルド時にリンカ(ld)がOpenSSLのライブラリ(lssl)を見つけられないらしい。
自分の環境だとHomebrewでインストールしたOpenSSLが /usr/local/opt/openssl
に存在するのでldがこの場所も探すように設定すれば良さそう。
環境変数 LIBRARY_PATH
に /usr/local/opt/openssl/lib
を追加するか環境変数 LDFLAGS
に -L/usr/local/opt/openssl/lib
を設定したらビルド及びインストールに成功した。
set -x LIBRARY_PATH $LIBRARY_PATH:/usr/local/opt/openssl/lib # for bash users # export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib
set -x LDFLAGS -L/usr/local/opt/openssl/lib # for bash users # export LDFLAGS=-L/usr/local/opt/openssl/lib
自分はfishを使っているので最終的にはユニバーサルな環境変数として設定して永続化した。(追記にしていないのは他に設定しているパスがなかったので)
set -Ux LIBRARY_PATH /usr/local/opt/openssl/lib
ちなみにpsycopg2にはビルドが不要で互換性のあるpsycopg2-binaryというパッケージがある。しかし、公式のドキュメントを読むと開発やテスト向けには良いがプロダクション向けではないので注意が必要。
psycopg vs psycopg-binary The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.
If you are the maintainer of a published package depending on psycopg2 you shouldn’t use psycopg2-binary as a module dependency. For production use you are advised to use the > source distribution.
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.