17

I installed Python 2.7.15rci and Python 3.6.7 on Ubuntu. When i did 'pip list' on virtualenv it returns me:

Django (2.1.5)
pip (9.0.1)
pkg-resources (0.0.0)
pytz (2018.9)
setuptools (39.0.1)
wheel (0.32.3)

I'm trying to install mysqlclient (pip install mysqlclient) and returns an error.

  unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
  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/david/env/project/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-pq18uxjj/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-y28h4ou0-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/david/env/project/include/site/python3.6/mysqlclient:
    /usr/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    creating build
    creating 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/_exceptions.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
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/MySQLdb
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,4,1,'final',0) -D__version__=1.4.1 -I/usr/include/mysql -I/home/david/env/project/include -I/usr/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o
    unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

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

So, I have tried different methods found like:

sudo apt-get install python-dev
sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev

and some more... but none of them work for me and the problem persists. Any suggestions?

Thank you!

11
  • you need to install default-libmysqlclient-dev according to the docs, not libmysqlclient-dev.
    – dirkgroten
    Commented Jan 24, 2019 at 16:33
  • 1
    I installed default-libmysqlclient-dev too but it still does not work ...
    – MAss
    Commented Jan 24, 2019 at 16:47
  • You say you've tried sudo apt-get install python-dev python3-dev, are those still installed? Or did you uninstall each time? All of these need to be installed.
    – dirkgroten
    Commented Jan 24, 2019 at 16:50
  • 1
    this is strange, i just installed it without any issue on Ubuntu 18.0.4 with the default python (3.6). Exactly by installing python3-dev gcc default-libmysqlclient-dev. Those were enough.
    – dirkgroten
    Commented Jan 24, 2019 at 17:43
  • 1
    I don't know why I didn't have those libraries installed but I installed libssl-dev and it worked! Thank you! ;)
    – MAss
    Commented Jan 25, 2019 at 7:51

8 Answers 8

29

gcc is part of the build-essential package, which you should install before anything else:

sudo apt-get install build-essential

UPDATE: Following @bosnjak's suggestion, you also might need to install the openssl developer tools:

sudo apt-get install libssl-dev
3
  • 8
    Even after installing all of the above, it didn't work for me in Ubuntu 18.04, until I did sudo apt install libssl-dev, which fixed the issue.
    – bosnjak
    Commented Dec 9, 2019 at 10:25
  • @bosnjak solution worked thanks !! I was trying to install on fresh ubuntu 18.04 installation
    – psakrii
    Commented Dec 11, 2019 at 5:27
  • @bosnjak Yes! That's exactly what the problem was. The traceback states cannot find -lssl, so you really need to install libssl-dev to build mysqlclient on Ubuntu.
    – drec4s
    Commented Jan 21, 2020 at 12:56
22

I specified the particular version of python it's 3.7 And that helped me

I have installed these ones:

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

And then installed mysqlclient by:

pip install mysqlclient
3
  • 1
    this helped me again :)
    – Nestor
    Commented Jul 9, 2020 at 14:45
  • Nestor, I was glad to help you! It helps me sometimes as well :-)
    – AlexKh
    Commented Jul 9, 2020 at 20:17
  • 1
    Thanks, that's fixed my issue. you saved my a lot of time :)
    – satyajit
    Commented Jul 8, 2021 at 15:57
10

I turned out, that the solution is slightly different for almost every Python version. For those using different versions, such as Python 2.x, 3.5 or 3.7 I found this:

Part I

For Python 2.x use:

  $ sudo apt-get install python-dev

For Python 2.7 use:

  $ sudo apt-get install libffi-dev

For Python 3.x use:

  $ sudo apt-get install python3-dev

For Python 3.4 use:

  $ sudo apt-get install python3.4-dev

For Python 3.7 use:

  $ sudo apt-get install python3.7-dev

For Python 3.8 use:

  $ sudo apt-get install python3.8-dev

For Python 3.9 use:

  $ sudo apt-get install python3.9-dev

Part II

If this still doesn't help, others have pointed out to install

  $ sudo apt-get install build-essential

and finally

  $ sudo apt-get install libssl-dev

But the last two didn't help myself - but hoping this might help you!

3
  • 1
    this fixed it for me on python3.8 and ubuntu 22.04 Commented Jul 29, 2022 at 21:41
  • 1
    The last one helped me! Using python 3.8.15. Ubunt 22. Thanks!
    – Diablo
    Commented Nov 29, 2022 at 13:21
  • 1
    Adding libssl-dev as suggested in part II was the step that fixed it for me, Linux Mint 21, Python 3.10.12 (also using repo.mysql.com in apt settings, not sure if that's relevant) Commented Jan 22 at 16:19
2

This issue was persistent in Ubuntu and python 3+. In my case, I was using Ubuntu 18.04 and found this solution to be useful for python version 3.7.5

Step 1. Install libpython3.7-dev via sudo apt

> sudo apt-get install libpython3.7-dev

Step 2: Install mysqlclient

> python3 -m pip install mysqlclient==1.4.6
1
  • Thanks, that's fixed my issue. you saved my a lot time.
    – satyajit
    Commented Jun 24, 2021 at 11:26
2

You may do this before:

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

Source: https://pypi.org/project/mysqlclient/

1
  • particularly default-libmysqlclient-dev has helped. Commented Feb 24, 2021 at 5:53
0

I wanted to install the 'Modoboa' mail-server package (https://github.com/modoboa/modoboa) on my Ubuntu 18.04 with Apache as my webserver, instead of nginx. I needed mysqlclient for Python 2.7. The following packages worked for me:

sudo apt-get install libssl-dev gcc python2.7-dev libmysqlclient-dev
0

I'm using the python3.8. Similarly to david's answer, use the following command then it works.

$ sudo apt-get install python3.8-dev

0

CentOS 7, MariaDB 10.1, Python 3.11, latest possible version - 2.1.1:

pip install mysqlclient==2.1.1

If you try to install a later version, you will get an error:

× Building wheel for mysqlclient (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [51 lines of output]

...

src/MySQLdb/_mysql.c:539:49: error: ‘MYSQL_OPT_SSL_ENFORCE’ undeclared (first use in this function) mysql_optionsv(&(self->connection), MYSQL_OPT_SSL_ENFORCE, (void *)&enforce_tls);

...

ERROR: Failed building wheel for mysqlclient

...

Failed to build mysqlclient ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.