Home > Software engineering >  installing mysqlclient with pipenv never succeed on Mac OS (both M1 and intel CPU)
installing mysqlclient with pipenv never succeed on Mac OS (both M1 and intel CPU)

Time:09-27

I tried to install mysqlclient with pipenv with both of my macbook pros (intel 2018 & M1 2020) but it just keep giving me the error below:

ld: library not found for -lzstd
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1

I come up with a solution which allows me to install mysqlclient explicitly with homebrew after reading this comment, with the following commands:

brew install zstd
CFLAGS="-I$(brew --prefix)/include" 
LDFLAGS="-L$(brew --prefix)/lib" 
brew install mysqlclient

However, I'm still getting the same issue with pipenv, I really want to make it work with pipenv, any idea?

Here's my pipfile (does not contain mysqlclient because it failed):

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pandas = "*"
numpy = "*"
mysql-connector-python = "*"

[dev-packages]

[requires]
python_version = "3.8"

CodePudding user response:

After hours of discovery and reading through every post on the internet, I realised ld: library not found for -lzstd means that the path of the zstd library defined is wrong, so I carefully reviewed my exported paths and bingo.


Here's a summary of what to do to install mysqlclient through pipenv on a M1 mac (For intel, just the homebrew root path is different):

First, install homebrew according to https://brew.sh/ (to install packages in a unified place, or you can choose not to use it, just you have to find out the path of all the required libraries)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Next, use homebrew to install mysql

brew install mysql

And then for some reason the mysqlclient package need openSSL and zstd (you will get an error telling you to install if you don't)

brew install openSSL
brew install zstd

Now, to let the computer know where these required packages are when installing mysqlclient, you need to defined the path explicitly in your .bash_profile/.zshrc. Again, there will be errors/warnings telling you to do this if you don't do this step. For me, since everything is installed through homebrew, they are all located under the homebrew dir so I can quickly add these into my .zshrc, note that LDFLAGS has two paths, you may read through many solutions with only one path on the LDFLAGS but they will not success because two libs are required to be installed before installing the mysqlclient package, that's where I get stuck..

export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L/opt/homebrew/Cellar/zstd/1.5.0/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

You may want to know what are LDFLAGS and CPPFLAGS. According to this page,

LDFLAGS refers for linker flags and is often user defined libraries

CPPLAGS is used by the preprocessor and is often the include directory

So these two flags basically let your install know where the path of your prerequisites are. And each flag can contain multiple paths.

Now you can happily do pipenv install mysqlclient and receive a success message.

  • Related