Ubuntuのpipでmysqlclientをインストールした際に遭遇したエラーとその対応

概要

Ubuntu環境でpipで mysqlclient をインストールしようとした際に遭遇したエラーとその対応のメモ

pipのインストール

以下でpipコマンドをインストール

apt -y install python3-pip

pipで mysqlclient をインストール

pip3 install mysqlclient

出力されたエラー

Collecting mysqlclient
  Downloading mysqlclient-2.0.3.tar.gz (88 kB)
     |████████████████████████████████| 88 kB 352 kB/s
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-pnhw9vzq/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-pnhw9vzq/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-pnhw9vzq/mysqlclient/pip-egg-info
         cwd: /tmp/pip-install-pnhw9vzq/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-pnhw9vzq/mysqlclient/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-pnhw9vzq/mysqlclient/setup_posix.py", line 70, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-pnhw9vzq/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.

対応

pipで mysqlclient をインストールする前に ibmysqlclient-dev パッケージをインストール

apt -y install libmysqlclient-dev

再度pipで mysqlclient をインストール

pip3 install mysqlclient

インストールに成功した際に出力されたメッセージ

Collecting mysqlclient
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.0.3-cp38-cp38-linux_x86_64.whl size=109284 sha256=ff4cf549254f27c167fc3afe5d6d436a841136151c625a6649bab998d07f2cbe
  Stored in directory: /root/.cache/pip/wheels/3a/c1/c3/5a19639a551c921c2c2b39468f4278ce5aa27b4e386a4158e4
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.0.3