安装psycopg2时出错,找不到-lssl的库

tsc*_*zle 87 python postgresql psycopg2

我运行"sudo pip install psycopg2",我得到一堆看起来像的输出

cc -DNDEBUG -g -fwrapv -Os .....
.....
cc -DNDEBUG -g -fwrapv -Os .....
.....
Run Code Online (Sandbox Code Playgroud)

最后它说:

ld: library not found for -lssl

clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: command 'cc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip-uE3thn-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2
Storing debug log for failure in /Users/Tyler/Library/Logs/pip.log
Run Code Online (Sandbox Code Playgroud)

运行easy_install从源代码执行它都会在最后给出同样的错误(关于-lssl找不到库的部分).


运行brew安装(或升级)openssl会产生以下结果

$ brew upgrade openssl
Error: openssl-1.0.1h already installed
Run Code Online (Sandbox Code Playgroud)

谁能帮我吗?

sas*_*sas 216

对于在macOS Sierra 10.12上寻找解决方案的人:我通过安装命令行工具修复了这个问题:

xcode-select --install
Run Code Online (Sandbox Code Playgroud)

在那之后,pip install psycopg2应该工作.

如果没有,你也可以尝试链接brew的openssl:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
Run Code Online (Sandbox Code Playgroud)

通过brew安装openssl.请注意,brew link openssl --force它不再起作用:

$ brew link openssl --force                                                                                 17.5s
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
Run Code Online (Sandbox Code Playgroud)

正如@macho指出如果这仍然不起作用,你可能需要使用--no-cachepip选项,例如

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2
Run Code Online (Sandbox Code Playgroud)

  • xcode-select install对于系统python来说已经足够了.但它不适用于虚拟环境. (9认同)
  • 您可能需要使用 pip 的 --no-cache 选项。例如`env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2` (7认同)
  • 实际上,`xcode-select --install`可能就足够了,这应该使用系统提供的`ssl`.我将在上面更新我的评论. (5认同)
  • `xcode-select --install`为我解决了Sierra的问题,也适用于虚拟环境 (4认同)
  • `xcode-select --install` 对我不起作用。我已经安装了命令行工具。但是,`env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2` 仍然有效。 (4认同)
  • 刚刚获得了macOS Sierra 10.12,刚刚执行了`pip install psycopg2`并收到了同样的错误消息:'ld:找不到-lssl的库'.`xcode-select --install`解决了这个问题. (3认同)
  • 在安装了sierra并为新项目创建新的virtualenv之后.我发现使用xcode命令是不够的,需要使用附加命令链接到brew的openssl包 (2认同)
  • 由于某种原因,我的brew安装的openssl将“@1.1”附加到路径中。我必须使用它才能使其工作: `env LDFLAGS="-I/usr/local/opt/openssl@1.1/include -L/usr/local/opt/openssl@1.1/lib" pip install psycopg2` (2认同)

小智 28

使用 MacOS Catalina 10.15.4,以下是唯一对我有用的命令:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
Run Code Online (Sandbox Code Playgroud)


And*_*rie 19

运行时,brew link openssl我收到以下消息:

$ brew link openssl
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
Run Code Online (Sandbox Code Playgroud)

遵循这个建议这里是pip您需要使用的命令:

$ pip install -r requirements.txt --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"
Run Code Online (Sandbox Code Playgroud)


Ash*_*gal 19

我从brew(brew install openssl)安装了OpenSSL

以下为我工作:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip install psycopg2
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用 Apple Silicon 并通过自制软件安装了 openssl,您的路径将会有所不同。您可以使用“brew --prefix openssl”找到 openssl 的安装位置。 (8认同)
  • 这对我有用,但我使用的是 M1 Mac,因此必须使用通过 home-brew 安装的 openssl 相应地设置环境变量。``` 导出 LDFLAGS="-L/opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/" 导出 CPPFLAGS="-I/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include" ```` (4认同)
  • 扩展@McFizz的技巧,这里是英特尔或苹果芯片的copypasta:```export LDFLAGS =“-L $(brew --prefix openssl)/ lib”导出CPPFLAGS =“-I $(brew --prefix openssl)/ include ”```` (4认同)
  • 这对我使用虚拟环境在 Catalina 上有用。其他答案对我不起作用,因为 xcode-select 已经安装,openssl 是一个不错的版本,并且设置 LD_LIBRARY_FLAG 没有效果。 (3认同)
  • 我有一个很大的 pip 安装过程,其中包含 psycopg2。这是能够使安装过程正常运行的唯一答案。 (2认同)

Kho*_*Ngo 19

MacOS 12 Monterey - M1 芯片组的解决方案:

xcode-select --install
brew install openssl
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
echo 'export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/opt/openssl@3/lib/' >> ~/.zshrc
Run Code Online (Sandbox Code Playgroud)

  • 感谢分享,这对我有用!MBP 16" M1 最大。 (2认同)

小智 15

适用于 MacOS BigSur 的解决方案(2021 年 5 月)

我也有类似的问题。但就在我把电脑从办公桌上扔掉之前,我找到了一个在 MacOS BigSur 上适用的解决方案:

我的 OpenSSL 安装已损坏。

brew reinstall openssl
Run Code Online (Sandbox Code Playgroud)

重新安装完成后会弹出3条需要执行的命令:

命令如下所示:

不要只是复制

echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
Run Code Online (Sandbox Code Playgroud)

这对我有用!不能保证这也适合您。

brew reinstall可能不是最好的解决方案,但它有效并提供了必要的命令。


Isa*_*lip 11

对我有用的是链接openssl的命令中提供的提示,

$ brew link openssl
Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

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

$ pip install psycopg2
Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/23/7e/93c325482c328619870b6cd09370f6dbe1148283daca65115cd63642e60f/psycopg2-2.8.2.tar.gz
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... done
Successfully installed psycopg2-2.8.2
Run Code Online (Sandbox Code Playgroud)


McF*_*izz 10

如果你:

  • 正在尝试安装psycopg2在 Python 内部venv并且
  • 正在尝试安装psycopg2在 Apple Silicon(新 ARM CPU)上

然后,正如 IamAshay 在他们的回答中指出的那样,我们需要导出一些变量以指向我们的 OpenSSL 安装。

Apple Silicon 上的安装目录显然与 Apple Intel 机器不同。无论如何,要查找系统上 OpenSSL 的安装位置(通过自制程序),您可以运行

brew --prefix openssl
Run Code Online (Sandbox Code Playgroud)

就我而言,我们有/opt/homebrew/opt/openssl@1.1. 所以这意味着我们需要导出的变量是

export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl/include"
Run Code Online (Sandbox Code Playgroud)

然后我们就可以运行

pip install psycopg2
Run Code Online (Sandbox Code Playgroud)


Env*_*vil 6

这是新的macOs版本的问题,pip不能安装cryptography。解决我的问题的是向安装命令提供 env :

brew install openssl
env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" <YOUR COMMAND HERE>
Run Code Online (Sandbox Code Playgroud)

您可以替换<YOUR COMMAND HERE>pip install cryptography, 或者pip install <SOMETHING THAT REQUIRES cryptography>例如。

归功于本文:修复 macOS Sierra 致命错误:找不到“openssl/opensslv.h”或“openssl/aes.h”文件


pmn*_*eve 5

在莫哈韦沙漠,我将它们添加到.bash_profile

export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/curl/lib -L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/curl/include -I/user/local/opt/openssl/include"
Run Code Online (Sandbox Code Playgroud)

然后能够在python 3.7.4 virtualenv中安装psycopg 2.8.3。

重新安装xcode和命令行工具之后。

以上所有答案都对您有所帮助!


bey*_*int 5

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

为我工作


小智 -5

我已经设法通过使用来修复它:

brew unlink openssl && brew link openssl --force
Run Code Online (Sandbox Code Playgroud)

我不确定这与我之前在 OpenSSL 上所做的尝试中进行的 brew 卸载/升级有何不同。我的假设是这些操作留下了一些“错误”的共享库,这些库阻止了它的工作。请注意,这也解决了安装 python 加密模块的问题。