Search code examples
pythonmysqlflaskubuntu-20.04flask-mysql

Error while installing flask-mysqldb on ubuntu 20.04


I am building a flask project and I want to use MySQL database. I need flask-mysqldb as an ORM for that. So as a good practice, I created a virtual environment, install flask and other dependencies, and then for installing flask-mysqldb, I used pip install flask-mysqldb. This suddenly gives me bunch of errors. At first I realized, I dont have MySQL installed on my system. So even after installing it these errors won't go away. I also tried pip install Flask-MySQLdb don't know how this is different from the first command but same errors.

I tried searching for errors online regarding flask-mysqldb, but most of them relate to some mysql.h file error which doesn't show in my terminal logs.

(env) ck@ck-IdeaPad-Gaming-3-15ARH05:~/projects/opensoft/roomAlloc$ pip install Flask-MySQLdb
Collecting Flask-MySQLdb
  Using cached Flask-MySQLdb-0.2.0.tar.gz (2.1 kB)
Requirement already satisfied: Flask>=0.10 in ./env/lib/python3.8/site-packages (from Flask-MySQLdb) (2.0.2)
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
    ERROR: Command errored out with exit status 1:
     command: /home/ck/projects/opensoft/roomAlloc/env/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-bra67idg/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-bra67idg/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-bra67idg/mysqlclient/pip-egg-info
         cwd: /tmp/pip-install-bra67idg/mysqlclient/
    Complete output (15 lines):
    /bin/sh: 1: mysql_config: not found
    /bin/sh: 1: mariadb_config: not found
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-bra67idg/mysqlclient/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-bra67idg/mysqlclient/setup_posix.py", line 70, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-bra67idg/mysqlclient/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
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Please help me out, you can comment if you need more information.


Solution

  • I had the same error 2 minutes ago and find this command to solve the problem!

    sudo apt-get install libmysqlclient-dev

    the just try to install again and should work fine.

    Credits:

    I found this command here (In Spanish):

    https://programmerclick.com/article/1483761552/