対応方法 for failing to install mysqlclient on m2 Mac with ld: library not found for -lzlib

2023-05-11 19:37 (1 years ago) ytyng

On an M2 Mac, when you run the following command:

pip install mysqlclient

You might encounter the following error:

      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-13-arm64-cpython-310
      creating build/temp.macosx-13-arm64-cpython-310/MySQLdb
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -I/opt/homebrew/opt/openssl/include -I/opt/homebrew/include -I/opt/homebrew/opt/zlib/include -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/opt/homebrew/Cellar/mysql/8.0.33/include/mysql -I/Users/yotsuyanagi/workspace/ranking-html-generator/.venv/include -I/opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c MySQLdb/_mysql.c -o build/temp.macosx-13-arm64-cpython-310/MySQLdb/_mysql.o -std=c99
      clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -L/opt/homebrew/opt/openssl/lib -L/opt/homebrew/lib -L/opt/homebrew/opt/zlib/lib -I/opt/homebrew/opt/openssl/include -I/opt/homebrew/include -I/opt/homebrew/opt/zlib/include build/temp.macosx-13-arm64-cpython-310/MySQLdb/_mysql.o -L/opt/homebrew/Cellar/mysql/8.0.33/lib -L/opt/homebrew/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-13-arm64-cpython-310/MySQLdb/_mysql.cpython-310-darwin.so
      ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib/'
      ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib/'
      ld: library not found for -lzlib
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> mysqlclient

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

This error indicates that the installation failed.

Initially, I had the following environment variables set in .zshrc:

BREW_PREFIX=$(brew --prefix)
export LDFLAGS="-L${BREW_PREFIX}/opt/openssl/lib -L${BREW_PREFIX}/lib"
export CPPFLAGS="-I${BREW_PREFIX}/opt/openssl/include -I${BREW_PREFIX}/include"

When I added the following lines, the installation succeeded (the last two lines are the additions):

BREW_PREFIX=$(brew --prefix)
export LDFLAGS="-L${BREW_PREFIX}/opt/openssl/lib -L${BREW_PREFIX}/lib"
export CPPFLAGS="-I${BREW_PREFIX}/opt/openssl/include -I${BREW_PREFIX}/include"
export MYSQLCLIENT_LDFLAGS="${LDFLAGS} -L${BREW_PREFIX}/opt/zlib/lib"
export MYSQLCLIENT_CFLAGS="${CPPFLAGS} -I${BREW_PREFIX}/opt/zlib/include"
% pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.1-cp310-cp310-macosx_13_0_arm64.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.1

Reference page:

https://github.com/Homebrew/homebrew-core/issues/130258

Addendum

Later, I encountered the following error:

clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -I/opt/homebrew/opt/openssl/include -I/opt/homebrew/include -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/opt/homebrew/opt/openssl/include -I/opt/homebrew/include -I/opt/homebrew/opt/zlib/include -I/Users/yotsuyanagi/workspace/manga-server-django/.venv/include -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-13-arm64-cpython-39/MySQLdb/_mysql.o -std=c99
MySQLdb/_mysql.c:29:10: fatal error: 'mysql.h' file not found
#include "mysql.h"
       ^~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]

I found that mysql.h was located in:

/opt/homebrew/Cellar/mysql-client/8.0.33/include/mysql/
/opt/homebrew/Cellar/mysql/8.0.33/include/mysql/

After adding this path to MYSQLCLIENT_CFLAGS, the build succeeded:

BREW_PREFIX=$(brew --prefix)
export LDFLAGS="-L${BREW_PREFIX}/opt/openssl/lib -L${BREW_PREFIX}/lib"
export CPPFLAGS="-I${BREW_PREFIX}/opt/openssl/include -I${BREW_PREFIX}/include"
export MYSQLCLIENT_LDFLAGS="${LDFLAGS} -L${BREW_PREFIX}/opt/zlib/lib"
export MYSQLCLIENT_CFLAGS="${CPPFLAGS} -I${BREW_PREFIX}/opt/zlib/include -I${BREW_PREFIX}/Cellar/mysql/8.0.33/include/mysql"
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011