[Solved] Unable to install mysqlclient package on EC2 instance – Mysql

Ali Hasan
amazon-ec2 amazon-web-services libmysqlclient llama-cpp-python

Quick Fix: To resolve the installation issue on Amazon Linux 2023, follow these steps:

  1. Install pip: sudo dnf install -y pip
  2. Install dependencies: sudo dnf install -y mariadb105-devel gcc python3-devel
  3. Install mysqlclient: pip install mysqlclient

The Problem:

I am unable to install mysqlclient 2.1.0 Python package on an Amazon EC2 instance running Amazon Linux 2023 AMI because the setup.py script fails during installation. The error message is:

error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [16 lines of output]
/bin/sh: line 1: mysql_config: command not found
/bin/sh: line 1: mariadb_config: command not found
/bin/sh: line 1: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-ywdhtbse/mysqlclient_802cda8d3393451492ff091d28d6482f/setup.py", line 15, in <module>
metadata, options = get_config()
File "/tmp/pip-install-ywdhtbse/mysqlclient_802cda8d3393451492ff091d28d6482f/setup_posix.py", line 70, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-ywdhtbse/mysqlclient_802cda8d3393451492ff091d28d6482f/setup_posix.py", line 31, in mysql_config
raise OSError(" not found".format(_mysql_config_path))
OSError: mysql_config not found
mysql_config –version
mariadb_config –version
mysql_config –libs
[end of output]

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

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

The Solutions:


The commands provided in the "Question" section are not the recommended way to install MySQL libraries and tools. It’s always a good practice to follow the official guidelines and use the appropriate repositories and package managers for the distribution you are using.

To install mysqlclient on Amazon Linux 2023, the following steps are recommended:

  1. Install pip: If pip is not already installed, install it using the following command:
sudo dnf install -y pip
  1. Install dependencies: Install the required dependencies for MySQL development, including the MariaDB development library and the GNU C compiler:
sudo dnf install -y mariadb105-devel gcc python3-devel
  1. Install mysqlclient: Finally, install the mysqlclient package using pip:
pip install mysqlclient

By following these steps, you should be able to successfully install mysqlclient on your Amazon Linux 2023 instance.

Solution 2:

If you have upgraded to python 3.11, make the following adjustments to Marcin’s answer:

# install PYTHON AND PIP3.11 (AL 2023 does not have one by default)
sudo dnf install python3.11 -y
sudo dnf install python3.11-pip -y

# install dependencies - NOTICE THE 3.11
sudo dnf install -y mariadb105-devel gcc python3.11-devel

# install mysqlclient
pip install mysqlclient


Unable to install mysqlclient package on Amazon Linux 2023 AMI EC2 instance, pip command returns error message on running it

One possible cause of the error can be missing dependencies. Try the following first:

sudo dnf install -y mariadb105-devel gcc python3-devel

And then try installing mysqlclient again using pip:

Video Explanation:

The following video, titled "How to Install MySQL server in Amazon Linux 2 - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Your browser can't play this video. Learn more.