69

I'm new to linux and VM's in general. Im currently trying to install mysqlclient on a clean Ubuntu Jammy 64 (22.4) vbox instance. The following commands are run beforehand:

    sudo add-apt-repository universe
    sudo apt-get install net-tools -y
    sudo apt-get install python3 -y
    sudo apt-get install python3-pip -y
    pip install pkgconfig
    sudo apt-get install pkg-config -y

When I try to run pip install mysqlclient I get the following error:

vagrant@Ctrl-A-EES:~$ pip install mysqlclient
Defaulting to user installation because normal site-packages is not writeable
Collecting mysqlclient
  Using cached mysqlclient-2.2.0.tar.gz (89 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/usr/lib/python3/dist-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
          main()
        File "/usr/lib/python3/dist-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/usr/lib/python3/dist-packages/pip/_vendor/pep517/in_process/_in_process.py", line 130, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/usr/lib/python3/dist-packages/setuptools/build_meta.py", line 162, in get_requires_for_build_wheel
          return self._get_build_requires(
        File "/usr/lib/python3/dist-packages/setuptools/build_meta.py", line 143, in _get_build_requires
          self.run_setup()
        File "/usr/lib/python3/dist-packages/setuptools/build_meta.py", line 158, in run_setup
          exec(compile(code, __file__, 'exec'), locals())
        File "setup.py", line 154, in <module>
          ext_options = get_config_posix(get_options())
        File "setup.py", line 48, in get_config_posix
          pkg_name = find_package_name()
        File "setup.py", line 27, in find_package_name
          raise Exception(
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]

  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.

Any suggestions would help greatly! Thanks!

Expected install to resolve without issue. Tried to resolve pkg-config dependency, to no avail.

6
  • 26
    Try running sudo apt-get install python3-dev default-libmysqlclient-dev build-essential Commented Jun 30, 2023 at 5:42
  • Wow, that was fast. It worked, thank you! Commented Jun 30, 2023 at 11:38
  • worked for me as well. can you please elaborate ? Commented Jul 14, 2023 at 15:03
  • Thank you for your question. I found this from a similar problem with mysqlclient but it was your package installs that gave me the solution. Commented Jul 24, 2023 at 20:20
  • 7
    If the issue persists, you might need to set the MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS environment variables manually before running the pip install command. You can do this in the terminal: export MYSQLCLIENT_CFLAGS=-I/usr/include/mysql export MYSQLCLIENT_LDFLAGS=-L/usr/lib/x86_64-linux-gnu Commented Nov 29, 2023 at 11:46

8 Answers 8

118

Solution from FlyingTeller:

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

Fixed the dependency.

Sign up to request clarification or add additional context in comments.

4 Comments

I needed to also install pkg-config.
sudo apt install pkg-config was necessary
worked for me too Ubuntu 24.04.
Same on WSL Ubuntu 22.04, pkg-config + default-libmysqlclient-dev worked out, thanks.
63

In my case sudo apt install pkg-config was enough to run pip install mysqlclient.

1 Comment

I needed this only on ubuntu 22 and never on 20
14

The error you are encountering while trying to install mysqlclient using pip3 is related to missing pkg-config and its inability to find the required dependencies for building the package.

pkg-config fails to find mysqlclient and mariadb libraries, which are needed to build the mysqlclient Python package.

Try these solutions:

  1. sudo apt-get update && sudo apt-get install python3-dev default-libmysqlclient-dev

If the above doesn't work:

  1. you can try installing the mysqlclient library by specifying the paths to the MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS environment variables manually, as suggested in the error message.

sudo MYSQLCLIENT_CFLAGS="-I/usr/include/mysql" MYSQLCLIENT_LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lmysqlclient" pip3 install mysqlclient

NOTE: Replace /usr/include/mysql and /usr/lib/x86_64-linux-gnu with the correct paths to the mysqlclient headers and library files on your system if they are located elsewhere. The provided paths are common for Ubuntu-based systems.

1 Comment

It works for me but before that I have search and get path using sudo find / -name libmysqlclient.so for MYSQLCLIENT_LDFLAGS and sudo find / -name mysql.h for MYSQLCLIENT_CFLAGS
12

use this for Debian/Ubuntu

sudo apt-get update
sudo apt-get install gcc libmysqlclient-dev python3-dev

use this for Fedora:

sudo dnf install gcc mysql-devel python3-devel

then run again

pip install mysqlclient

Comments

5

I had to add build-essential and pkg-config to my Dockerfile after upgrading to Python 3.11 from 3.8 and the mysqlclient install started to fail with the same error. This is working for me now.

FROM --platform=linux/amd64 python:3.11-slim as build
...
RUN apt-get update -y
RUN apt-get install pkg-config -y
RUN apt-get install -y python3-dev build-essential
RUN apt-get install -y default-libmysqlclient-dev
...

1 Comment

Your RUN commands should be merged into one line to avoid cache issues. E.g. apt-get update && apt-get install -y pkg-config python3-dev default-libmysqlclient-dev build-essential default-libmysqlclient-dev && pip install mysqlclient. See docs.docker.com/develop/develop-images/instructions/#run
4

Here’s the equivalent command for macOS:

brew install pkg-config python3 mysql-client

Comments

2

you guys can try this

sudo apt install libmysqlclient-dev default-libmysqlclient-dev

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
1

For Rocky 9 the following did the trick:

dnf group install "Development Tools"
dnf install python3-devel mysql-devel pkgconfig

pip install mysql-connector mysqlclient

https://github.com/PyMySQL/mysqlclient

1 Comment

In my case mysql-connector was missing. After installing both packages (mysql-connector mysqlclient) in the system and inside the python environment all worked out (Ubuntu 22.04). Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.