1

I'm trying to install mysqlclient on MacOS from requirements.txt of a Python(3.8) project. I created a virtual environment and this is the output while trying to install it:

ld: library not found for -lzlib  
clang: error: linker command failed with exit code 1 (use -v to see invocation)  
error: command '/usr/bin/gcc' failed with exit code 1  
[end of output]

Note that lzlib is already installed. I read that MacOS has clang for compilation of C libraries and I installed gcc using Homebrew. Still the default clang was being picked up as is evident from the following output:

$ gcc -v  
Apple clang version 14.0.3 (clang-1403.0.22.14.1)  
Target: x86_64-apple-darwin22.4.0  
Thread model: posix  
InstalledDir: /Library/Developer/CommandLineTools/usr/bin 



$ gcc-13 -v  
Using built-in specs.  
COLLECT_GCC=gcc-13  
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/13.1.0/bin/../libexec/gcc/x86_64-apple-darwin22/13/lto-wrapper  
Target: x86_64-apple-darwin22  
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-13 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 13.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=x86_64-apple-darwin22 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk  
Thread model: posix  
Supported LTO compression algorithms: zlib zstd  
gcc version 13.1.0 (Homebrew GCC 13.1.0)  

So, I tried to set the alias for gcc in my .zshrc which showed correct output in terminal but the command to install mysqlclient still fails with same error message. I tried to install multiple packages as suggested in different answers but still the error is same.

PiNaKa30
  • 608
  • 7
  • 20

2 Answers2

2

Run brew info zlib to get the path to your zlib.

Likely: "-L/opt/homebrew/opt/zlib/lib"

Open your mysql config file in any editor, example:

> sudo vim /opt/homebrew/opt/mysql-client/bin/mysql_config

In this file you'll find the libraries. I updated zlib to be the direct library

before:

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lzlib  -lzstd -L/opt/homebrew/opt/openssl@1.1/lib -lssl -lcrypto -lresolv"

After

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -L/opt/homebrew/opt/zlib/lib  -lzstd -L/opt/homebrew/opt/openssl@1.1/lib -lssl -lcrypto -lresolv"

Don't know if it's the most elegant solution but this workaround got me to install mysql_client

Eden
  • 21
  • 2
0

The error you're encountering seems to be related to the linker not being able to find the necessary libraries during the installation of mysqlclient.

Ensure that you have the zlib library installed. You mentioned that it's already installed, but double-check to make sure it's available in the default library search path. You can use the brew command to install it if needed:

brew install zlib

Set the LDFLAGS environment variable to include the zlib library path before installing mysqlclient. Open a new terminal window and run the following command:

export LDFLAGS="-L/usr/local/opt/zlib/lib"

Run the installation of mysqlclient again using pip within your virtual environment:

pip install -r requirements.txt

If the above steps don't work, you can try explicitly specifying the location of the zlib library during the installation:

pip install -r requirements.txt --global-option=build_ext --global-option="-L/usr/local/opt/zlib/lib"

By setting the LDFLAGS environment variable or providing the library location explicitly, you're telling the linker where to find the zlib library, which is needed by mysqlclient.