3

I am running into this issue when I try to run pip install mysqlclient. I have already installed all the libraries necessary with brew and/or pip. I tried reinstalling zlib but nothing seems to be working. I tried downloading on brew mysql, mysql-client, etc... but I am not getting any results.

Collecting mysqlclient
  Using cached mysqlclient-2.1.1.tar.gz (88 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [43 lines of output]
      mysql_config --version
      ['8.0.33']
      mysql_config --libs
      ['-L/opt/homebrew/Cellar/mysql/8.0.33/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/opt/homebrew/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
      mysql_config --cflags
      ['-I/opt/homebrew/Cellar/mysql/8.0.33/include/mysql']
      ext_options:
        library_dirs: ['/opt/homebrew/Cellar/mysql/8.0.33/lib', '/opt/homebrew/opt/openssl@1.1/lib']
        libraries: ['mysqlclient', 'zlib', 'resolv']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/opt/homebrew/Cellar/mysql/8.0.33/include/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,1,'final',0)"), ('__version__', '2.1.1')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-10.9-universal2-cpython-311
      creating build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/connections.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/converters.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/release.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying MySQLdb/times.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      creating build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-10.9-universal2-cpython-311
      creating build/temp.macosx-10.9-universal2-cpython-311/MySQLdb
      clang -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/opt/homebrew/Cellar/mysql/8.0.33/include/mysql -I/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-universal2-cpython-311/MySQLdb/_mysql.o -std=c99
      clang -bundle -undefined dynamic_lookup -arch arm64 -arch x86_64 -g -L/opt/homebrew/opt/zlib/lib build/temp.macosx-10.9-universal2-cpython-311/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-10.9-universal2-cpython-311/MySQLdb/_mysql.cpython-311-darwin.so
      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: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

I have tried:

  1. Hey. I have an error when try to install mysqlclient to my Django project on Mac

  2. Installing like this

    $ brew install mysql
    $ pip install mysqlclient
    
  3. Alternative install

    $ brew install mysql-client
    $ echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
    $ export PATH="/usr/local/opt/mysql-client/bin:$PATH"
    $ pip install mysqlclient
    

I have seen this:

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

but I have a Mac so it doesn't solve my problem.

Machavity
  • 30,841
  • 27
  • 92
  • 100

5 Answers5

2

After installing zlib, you need to set the DYLD_LIBRARY_PATH environment variable. Open a Terminal window and enter that command:

export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH. 

It will tells your system where to find the zlib library.

Of course, if /usr/local/lib is not the path, change it

Richard
  • 994
  • 7
  • 26
  • so in my case I would do: export DYLD_LIBRARY_PATH=/opt/homebrew/opt/zlib/lib:$DYLD_LIBRARY_PATH. – Tanner Green May 04 '23 at 22:39
  • Yes. And you can test it by running the command echo $DYLD_LIBRARY_PATH after. It should output the path to the zlib library directory along with any other paths that were already in the DYLD_LIBRARY_PATH variable – Richard May 04 '23 at 22:43
  • ➜ ~ brew info zlib ==> zlib: stable 1.2.13 (bottled), HEAD [keg-only] General-purpose lossless data-compression library https://zlib.net/ /opt/homebrew/Cellar/zlib/1.2.13 (13 files, 399.0KB) When I get the info here and I add this to the filepath, it still won't find the library? – Tanner Green May 04 '23 at 22:55
  • wait, did you used `/opt/homebrew/opt/zlib/lib` OR `/opt/homebrew/Cellar/zlib/1.2.13/lib` ? You have to use the second one. Only the direct link to the path will work – Richard May 06 '23 at 12:58
2

It seems to be a MySQL 8.0.33 issue

The problem is that most (all?) other platforms with 8.0.33 deployed seem to do so with the MySQL-bundled zlib rather than an external library, and so mysql_config doesn't reference zlib at all

Machavity
  • 30,841
  • 27
  • 92
  • 100
1

I have come across this same issue for myself today. As it is an older django project I was able to install mysql-client@5.7:

brew install mysql-client@5.7

part of my .zprofile:

# homebrew ssl
export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

# homebrew zlib
export LDFLAGS="$LDFLAGS -L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/homebrew/opt/zlib/include"

# homebrew mysql
export PATH="/opt/homebrew/opt/mysql-client@5.7/bin:$PATH"
export LDFLAGS="$LDFLAGS -L/opt/homebrew/opt/mysql-client@5.7/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/homebrew/opt/mysql-client@5.7/include"

# gdal
export GDAL_LIBRARY_PATH="/opt/homebrew/opt/gdal/lib/libgdal.dylib"
export GEOS_LIBRARY_PATH="/opt/homebrew/opt/geos/lib/libgeos_c.dylib"

at the time of writing this is the only other version of the mysql-client I can see.

then doing the pip install that I needed on my environment:

pip install mysqlclient==1.4.6

I had a successful install, I don't know if this would work for newer versions.

Itergator
  • 299
  • 3
  • 16
0

Can you try this

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
ytyng
  • 575
  • 6
  • 8
0

It is a problem with the mysql server offered by brew:

brew uninstall mysql
brew install mysql@5.7
brew link mysql@5.7

after that, your gem build should work out of the box, if it doesn't you can try to pass all the flags:

export LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include -I$(brew --prefix)/include"
export MYSQLCLIENT_LDFLAGS="${LDFLAGS}"
export MYSQLCLIENT_CFLAGS="${CPPFLAGS}"
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/:$(brew --prefix zlib)/lib


pip install mysqlclient
rorra
  • 9,593
  • 3
  • 39
  • 61