Installing mysqlclient on Ubuntu 22.04 / 24.04

Error Like:

Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

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

Solution:

sudo apt install libmysqlclient-dev
# OR
sudo apt install libmariadb-dev-compat

You can use the mysql_config command to retrieve this information.

Now you need to Set the MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS environment variable.

mysql_config --cflags

# OUTPUT: -I/usr/include/mysql 

mysql_config --libs

# OUTPUT: -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lz  -lzstd -lssl -lcrypto -lresolv -lm

Set the MYSQLCLIENT_CFLAGS environment variable. Replace '' with the path you obtained from the mysql_config --cflags command. Run the following command: export MYSQLCLIENT_CFLAGS="-I"

export MYSQLCLIENT_CFLAGS="-I/usr/include/mysql"

Set the MYSQLCLIENT_LDFLAGS environment variable. Replace '' with the path you obtained from the mysql_config --libs command. Run the following command: export MYSQLCLIENT_LDFLAGS="-L" Note: If your MySQL installation includes multiple libraries, separate each library path with a space.

export MYSQLCLIENT_LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lz  -lzstd -lssl -lcrypto -lresolv -lm"

Now, when you install packages that require the MySQL client library.

pip install mysqlclient

Reference: https://stackoverflow.com/questions/76875507/can-not-install-apache-airflow-providers-mysql-pkg-config-error

Additional:

sudo apt-get install pkg-config build-essential
sudo bash -c "apt-get update && apt-get -y upgrade && apt-get -y autoremove && apt-get -y clean"