pythonmysqlamazon-web-servicesamazon-ec2

Unable to install mysqlclient package on EC2 instance


I am trying to install mysqlclient Python package on an Amazon EC2 instance running Amazon Linux 2023 AMI.

When I run pip install mysqlclient, I get the following error message:

Collecting mysqlclient==2.1.0
  Downloading mysqlclient-2.1.0.tar.gz (87 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 87.6/87.6 kB 17.2 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... error
  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.

I have tried the following :

sudo yum -y install mysql which resulted in Last metadata expiration check: 0:41:31 ago on Thu Apr 6 13:36:18 2023. No match for argument: mysql

Can anyone suggest a solution to this problem?

Thanks in advance!

EDIT

Here are all the commands I did :

But with the last one I get the following error :

Collecting importlib-metadata
  Downloading importlib_metadata-6.1.0-py3-none-any.whl (21 kB)
Collecting zipp>=0.5
  Downloading zipp-3.15.0-py3-none-any.whl (6.8 kB)
Building wheels for collected packages: Flask-MySQLdb, mysqlclient
  Building wheel for Flask-MySQLdb (setup.py) ... done
  Created wheel for Flask-MySQLdb: filename=Flask_MySQLdb-1.0.1-py3-none-any.whl size=4675 sha256=e0bb7388b33b749ec52908abdb0e34d15c10544c2c2c96932e9ee90b117e0cfa
  Stored in directory: /home/ec2-user/.cache/pip/wheels/48/ba/f7/32a9b364c18e9e479d5dd05305109e7a72d85d8e29c02a10ea
  Building wheel for mysqlclient (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      mysql_config --version
      ['10.5.5']
      mysql_config --libs
      ['-L/usr/lib64/', '-lmariadb']
      mysql_config --cflags
      ['-I/usr/include/mysql', '-I/usr/include/mysql/mysql']
      ext_options:
        library_dirs: ['/usr/lib64/']
        libraries: ['mariadb']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/include/mysql', '/usr/include/mysql/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,0,'final',0)"), ('__version__', '2.1.0')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-39
      creating build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/connections.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/converters.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/release.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/times.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      creating build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.linux-x86_64-cpython-39
      creating build/temp.linux-x86_64-cpython-39/MySQLdb
      gcc -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(2,1,0,'final',0) -D__version__=2.1.0 -I/usr/include/mysql -I/usr/include/mysql/mysql -I/home/ec2-user/myapp/env/include -I/usr/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-39/MySQLdb/_mysql.o -std=c99
      MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
         46 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/gcc' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Successfully built Flask-MySQLdb
Failed to build mysqlclient
Installing collected packages: certifi, zipp, Werkzeug, urllib3, six, mysqlclient, MarkupSafe, itsdangerous, idna, colorama, click, charset-normalizer, requests, Jinja2, importlib-metadata, Flask, Flask-MySQLdb, Flask-Cors
  Running setup.py install for mysqlclient ... error
  error: subprocess-exited-with-error

  × Running setup.py install for mysqlclient did not run successfully.
  │ exit code: 1
  ╰─> [46 lines of output]
      mysql_config --version
      ['10.5.5']
      mysql_config --libs
      ['-L/usr/lib64/', '-lmariadb']
      mysql_config --cflags
      ['-I/usr/include/mysql', '-I/usr/include/mysql/mysql']
      ext_options:
        library_dirs: ['/usr/lib64/']
        libraries: ['mariadb']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/include/mysql', '/usr/include/mysql/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,0,'final',0)"), ('__version__', '2.1.0')]
      running install
      /home/ec2-user/myapp/env/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-cpython-39
      creating build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/connections.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/converters.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/release.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      copying MySQLdb/times.py -> build/lib.linux-x86_64-cpython-39/MySQLdb
      creating build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-cpython-39/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.linux-x86_64-cpython-39
      creating build/temp.linux-x86_64-cpython-39/MySQLdb
      gcc -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -ftree-vectorize -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(2,1,0,'final',0) -D__version__=2.1.0 -I/usr/include/mysql -I/usr/include/mysql/mysql -I/home/ec2-user/myapp/env/include -I/usr/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-39/MySQLdb/_mysql.o -std=c99
      MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
         46 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/gcc' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> mysqlclient

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

Solution

  • On fresh Amazon Linux 2023 you have to do:

    # install pip (AL 2023 does not have one by default)
    sudo dnf install -y pip
    
    # install dependencies
    sudo dnf install -y mariadb105-devel gcc python3-devel
    
    # install mysqlclient
    pip install mysqlclient