mac上pip install mysqlclient报 ld: library not found for -lzlib
1.问题描述
参考官方文档https://pypi.org/project/mysqlclient/ 安装mysqlclient
结果报以下错误:
Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m/mysqlclient
cwd: /private/var/folders/x3/lyq97_4j7ls_vmh9d4yprv0w0000gn/T/pip-install-x6vrl7yx/mysqlclient_cd054a1c4685430784e8bbe47aac9315/
Complete output (43 lines):
mysql_config --version
['8.0.33']
mysql_config --libs
['-L/usr/local/opt/mysql-client/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/usr/local/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
mysql_config --cflags
['-I/usr/local/opt/mysql-client/include/mysql']
....
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/usr/local/opt/mysql-client/include/mysql -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-x86_64-3.6/MySQLdb/_mysql.o -std=c99
gcc -bundle -undefined dynamic_lookup -arch x86_64 -g build/temp.macosx-10.9-x86_64-3.6/MySQLdb/_mysql.o -L/usr/local/opt/mysql-client/lib -L/usr/local/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-10.9-x86_64-3.6/MySQLdb/_mysql.cpython-36m-darwin.so
ld: library not found for -lzlib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1:
....
2.解决过程
通过报错发现是找不到zlib,于是安装zlib
brew install zlib
通过brew info zlib
可以查看zlib的安装路径,不过一般都有软链,可以通过brew info zlib
来查看推荐的配置信息:
% brew info zlib
==> zlib: stable 1.2.13 (bottled), HEAD [keg-only]
General-purpose lossless data-compression library
https://zlib.net/
/usr/local/Cellar/zlib/1.2.13 (13 files, 409KB)
Poured from bottle using the formulae.brew.sh API on 2023-04-13 at 20:29:38
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/zlib.rb
License: Zlib
==> Options
--HEAD
Install HEAD version
==> Caveats
zlib is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
For compilers to find zlib you may need to set:
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"
可以发现有效配置路径是-L/usr/local/opt/zlib/lib
,使用有效路径可以避免后续版本升级的问题。
通过调整.bash_profile
添加:
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
结果发现执行pip install mysqlclient
依然报错
然后查看详细安装过程pip install mysqlclient -v
,发现了关键信息:
......
mysql_config --libs
['-L/usr/local/opt/mysql-client/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/usr/local/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
......
可以发现安装的时候会用到mysql_config
,并且读取zlib还是使用-lzlib
通过查阅资料 pip3 install mysqlclient fails on macOS , Mac 安装 mysqlclient
I follow those step:
i) modify file: /usr/local/bin/mysql_config (which mysql_config)
origin:Create options
libs="-L$pkglibdir"
libs="$libs -l "
modified toCreate options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
ii)add environment veriables(brew info openssl)export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
iii)pip3 install mysqlclientif you still meet the issue:
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
i fix it by install PyMySQLpip3 install PyMySQL
Hope this help later-comers:
相关链接: pip3 install mysqlclient fails on macOS
可以发现需要通过调整mysql_config
来解决这个问题。
但是普通用户无法编辑mysql_config
, 因此mac需要开启root用户:如何在 Mac 上启用 root 用户或更改 root 密码, 然后执行sudo su
切换到root用户,当然光切换到root用户还不够,mysql_config
是只读权限,这里需要chmod 755 mysql_config
来提权,然后才可以编辑。
注意这里有一个注意的地方,mysql_config
路径的问题,一般可以通过%which mysql_config
来定位mysql_config的路径。
一般是/usr/local/opt/mysql-client/bin/mysql_config
这个路径,而不是/usr/local/bin/mysql_config
按如下调整后:
# Create options
libs="-L$pkglibdir"
#libs="$libs -lmysqlclient -lzlib -lzstd -L/usr/local/opt/openssl@1.1/lib -lssl -lcrypto -lresolv"
libs="$libs -lmysqlclient -L/usr/local/opt/zlib/lib -lzstd -L/usr/local/opt/openssl@1.1/lib -lssl -lcrypto -lresolv"
再执行pip install mysqlclient
即可成功安装