Error installing mysqlclient on Mac OS to run Django

I’m using a Mac (latest os) to setup Django and use mysql. I have mysql installed and when I try to pip install mysqlclient, I get the following error:

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/opt/mysql-client/lib’, ‘-lmysqlclient’, ‘-lzlib’, ‘-lzstd’, ‘-L/opt/homebrew/opt/openssl@1.1/lib’, ‘-lssl’, ‘-lcrypto’, ‘-lresolv’]
mysql_config --cflags
[‘-I/opt/homebrew/opt/mysql-client/include/mysql’]
ext_options:
library_dirs: [‘/opt/homebrew/opt/mysql-client/lib’, ‘/opt/homebrew/opt/openssl@1.1/lib’]
libraries: [‘mysqlclient’, ‘zlib’, ‘resolv’]
extra_compile_args: [‘-std=c99’]
extra_link_args:
include_dirs: [‘/opt/homebrew/opt/mysql-client/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-x86_64-cpython-39
creating build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/init.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/exceptions.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/connections.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/converters.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/cursors.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/release.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
copying MySQLdb/times.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb
creating build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/init.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/CR.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/ER.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
copying MySQLdb/constants/FLAG.py → build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/constants
running build_ext
building 'MySQLdb.mysql’ extension
creating build/temp.macosx-10.9-x86_64-cpython-39
creating build/temp.macosx-10.9-x86_64-cpython-39/MySQLdb
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/steve/opt/anaconda3/include -arch x86_64 -I/Users/steve/opt/anaconda3/include -fPIC -O2 -isystem /Users/steve/opt/anaconda3/include -arch x86_64 -I/opt/homebrew/opt/mysql-client/include -Dversion_info=(2,1,1,‘final’,0) -D__version
=2.1.1 -I/opt/homebrew/opt/mysql-client/include/mysql -I/Users/steve/opt/anaconda3/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-x86_64-cpython-39/MySQLdb/_mysql.o -std=c99
clang -bundle -undefined dynamic_lookup -Wl,-rpath,/Users/steve/opt/anaconda3/lib -L/Users/steve/opt/anaconda3/lib -L/Users/steve/opt/anaconda3/lib -Wl,-rpath,/Users/steve/opt/anaconda3/lib -L/Users/steve/opt/anaconda3/lib -L/opt/homebrew/opt/mysql-client/lib -I/opt/homebrew/opt/mysql-client/include build/temp.macosx-10.9-x86_64-cpython-39/MySQLdb/_mysql.o -L/opt/homebrew/opt/mysql-client/lib -L/opt/homebrew/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-10.9-x86_64-cpython-39/MySQLdb/_mysql.cpython-39-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’ve searched and tried many different suggestions, but none of them have worked and I’m currently not able to run the Django server using mysql. Any help on this issue is greatly appreciated.

I’ve experienced this issue when installing in a cpanel environment in the past, but not on my mac (not the latest os).

I do know that some devs have sidestepped the issue by using the other mysql driver (the one from Oracle). May be worth giving that a try.

https://dev.mysql.com/downloads/connector/python/

1 Like