Error Installing mysqlclient on Ubuntu 16.04 using pip and Python 3.6

Question:

I am getting a strange error when trying to install mysqlclient on Ubuntu 16.04 Xenial with pip + Python 3.6:

 pip install mysqlclient

Output:

 _mysql.c:40:20: fatal error: Python.h: No such file or directory
 compilation terminated.
 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Following the installation requirements, I have tried installing the required libraries, but not luck so far.

sudo apt-get install python3-dev libmysqlclient-dev

Does someone know the workaround for this issue?

Asked By: Ander

||

Answers:

I found the problem, seems like for installing mysqlclient in python3.6 the library python3.6-dev is required.

Just open a terminal and run the following command:

sudo apt-get install python3.6-dev libmysqlclient-dev

You may get the following error when trying to install the library:

Reading state information...
E: Unable to locate package python3.6-dev
E: Couldn't find any package by glob 'python3.6-dev'
E: Couldn't find any package by regex 'python3.6-dev'

If that is the case, just try adding the following repository first:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update

After doing so, you should be able to install both python3.6 and python3.6-dev packages.

Answered By: Ander

I had trouble installing mysqlclient on python 3.6.3, so
I downgraded to version 3.6.2 and it worked.

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev  libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
cd /usr/bin
sudo wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
sudo tar xzf Python-3.6.2.tgz
cd Python-3.6.2
sudo ./configure --enable-optimizations
sudo make altinstall

python3.6 should point at 3.6.2

python --version # Python 3.6.2

now install mysqlclient

python3.6 -m pip install mysqlclient

read more

Answered By: Bar Horing Amir

I had faced same issue:

#sudo pip3 install mysqlclient fails with mysql_config not found
sudo apt-get install libmysqlclient-dev
#without pip3 it will not going to work for python3
sudo pip3 install mysqlclient

Solved using above way.

reference link

Answered By: chocha ashish

I’m getting this error

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

1 Solution

Open new terminal and run this command

sudo apt-get install libssl-dev

after that install mysqlclient

pip install mysqlclient

2 Solution

Install MySQL database connector

First thing we will need to do is install python3-dev. You can install python3-dev by running the following command:

sudo apt-get install python3-dev

Once python3-dev is installed, we can install the necessary Python and MySQL development headers and libraries:

sudo apt-get install python3-dev libmysqlclient-dev

Then, we will use pip3 to install the mysqlclient library from PyPi. Since our version of pip points to pip3, we can just use pip.

pip install mysqlclient

Reference Link :
How To Create a Django App and Connect it to a Database

Even though I’m late, I’d like to address a few things while answering your question.

Some suggests to install python3-dev, some suggests libmysqlclient-dev etc. But they might already have installed some of the packages thats required while installing and only installing the mentioned package made it work for them. However, it might not be the case with everyone.

You can refer to the mysqlclient’s official documentation and scroll down to installation instructions for Linux – mysqlclient

Generally, these are the packages required before you can mysqlclient

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

Now, since you mentioned you ran this.

sudo apt-get install python3-dev libmysqlclient-dev

Looking at the documentation, you are missing these packages.

  • default-libmysqlclient-dev
  • build-essential

So, the answer for your question is

sudo apt-get install default-libmysqlclient-dev build-essential

And then you shoud install mysqlclient

pip install mysqlclient

Hope that answers.

Answered By: Jayanta Madhav
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.