Gym: Unable to install mysqlclient on Ubuntu LTS 16.04 LTS on Python3.6

Created on 21 Oct 2017  路  21Comments  路  Source: openai/gym

I installed and created a virtual environment on my Linux Ubuntu LTS 16.04 server. MySQL database is successfully installed and the database server is working. While trying to migrate django I get error that mysqlclient is not installed. When I try to install mysqlclient using the below command,
pip install mysqlclient
I am getting the below error,

Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Building wheels for collected packages: mysqlclient
Running setup.py bdist_wheel for mysqlclient ... error
Complete output from command /home/ubuntu/django_project/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-m44_w0__/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmprdsdub84pip-wheel- --python-tag cp36:
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-3.6
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/home/ubuntu/django_project/include -I/usr/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o
_mysql.c:32:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1


Failed building wheel for mysqlclient
Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... error
Complete output from command /home/ubuntu/django_project/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-m44_w0__/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-hellghhe-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ubuntu/django_project/include/site/python3.6/mysqlclient:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-3.6
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/home/ubuntu/django_project/include -I/usr/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o
_mysql.c:32:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------

Command "/home/ubuntu/django_project/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-m44_w0__/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-hellghhe-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ubuntu/django_project/include/site/python3.6/mysqlclient" failed with error code 1 in /tmp/pip-build-m44_w0__/mysqlclient/

Please help me to solve the issue. I need urgent solution of this.

Most helpful comment

This is how I fix it.
sudo apt-get install python3.6-dev libmysqlclient-dev
after that I activate the virtual environment and execute the following command
pip install mysqlclient

All 21 comments

This is how I fix it.
sudo apt-get install python3.6-dev libmysqlclient-dev
after that I activate the virtual environment and execute the following command
pip install mysqlclient

Thank yo so much waqas80 for your input.
Some months later it has helped me a lot.
:-)

Worked for me. Thank you!

Excellent waqas80!

What about Centos yum repository?

thank @waqas80 work like a charm

For CentOS users try:
yum install python36-devel

kudos waqas80

This is how I fix it.
sudo apt-get install python3.6-dev libmysqlclient-dev
after that I activate the virtual environment and execute the following command
pip install mysqlclient

You are a life saver bro :+1: @waqas80

Thank you so much for your help. @waqas80

Thankyou so much very helpful even for ubuntu 18.04

Same here, but solved in another way. In my case I missed these two lines in the error messages:

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto

So just sudo apt-get install libssl-dev then there you go.

@Whtie4622 You're my hero

@Whtie4622 It should be sudo apt-get install libssl-dev

This has worked perfectly well for me.

Same here, but solved in another way. In my case I missed these two lines in the error messages:

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto

So just sudo apt-get install libssl-dev then there you go.

This worked perfectly well for me. Thanks a million

works fine .. thanks

nice, thx

this worked form me:
sudo apt-get install default-libmysqlclient-dev

Great Stuff

Okay I'll add something if this helps anyone in future.

I have faced similar problem while trying to use mysql from XAMPP. I am using an arch based distro (manjaro 20.1) so I'll need to install this library libmysqlclient-dev from aur apparently.
This package also needs the mysql to be built before. I don't think my tiny local machine is up to that challenge. So, I have installed mysql (from mariadb) using sudo pacman -S --needed mysql. And the library has been quietly installed without throwing angry error messages.

Thanks. work for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbbc1117 picture lbbc1117  路  3Comments

julian-ramos picture julian-ramos  路  4Comments

mdavis-xyz picture mdavis-xyz  路  3Comments

david8373 picture david8373  路  4Comments

zhan0903 picture zhan0903  路  4Comments