pipでpsycopg2をインストールしようとするとCommand errored out with exit status 1とエラーが出る


DjangoでPostgresを使おうと思い、調べているとpsycopg2というパッケージが必要とのことで

$ pip install psycopg2

とコマンドを打ちました。するとエラーが発生。

$ pip install psycopg2
Collecting psycopg2
  Using cached psycopg2-2.9.1.tar.gz (379 kB)
Using legacy 'setup.py install' for psycopg2, since package 'wheel' is not installed.
Installing collected packages: psycopg2
    Running setup.py install for psycopg2 ... error
    ERROR: Command errored out with exit status 1:
     command: /home/(username)/django/myvenv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-3167j200/psycopg2_ce00c2bb1d2342c1811e76887cb05c30/setup.py'"'"'; __file__='"'"'/tmp/pip-install-3167j200/psycopg2_ce00c2bb1d2342c1811e76887cb05c30/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-c1vq00ua/install-record.txt --single-version-externally-managed --compile --install-headers /home/(username)/django/myvenv/include/site/python3.8/psycopg2
         cwd: /tmp/pip-install-3167j200/psycopg2_ce00c2bb1d2342c1811e76887cb05c30/
    Complete output (38 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/psycopg2
    copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/errors.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
    copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
    running build_ext
    building 'psycopg2._psycopg' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/psycopg
    gcc -pthread -B /home/(username)/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPSYCOPG_VERSION=2.9.1 (dt dec pq3 ext lo64) -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=120007 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/(username)/django/myvenv/include -I/home/(username)/anaconda3/include/python3.8 -I. -I/usr/include/postgresql -I/usr/include/postgresql/12/server -I/usr/include/libxml2 -I/usr/include/mit-krb5 -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:28:
    ./psycopg/psycopg.h:36:10: fatal error: libpq-fe.h: No such file or directory
       36 | #include <libpq-fe.h>
          |          ^~~~~~~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/(username)/django/myvenv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-3167j200/psycopg2_ce00c2bb1d2342c1811e76887cb05c30/setup.py'"'"'; __file__='"'"'/tmp/pip-install-3167j200/psycopg2_ce00c2bb1d2342c1811e76887cb05c30/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-c1vq00ua/install-record.txt --single-version-externally-managed --compile --install-headers /home/(username)/django/myvenv/include/site/python3.8/psycopg2 Check the logs for full command output.

どうやらlibpq-fe.hってのが無いらしい。ということで調べていると以下の記事を見つけました。
https://stackoverflow.com/questions/56230187/no-such-file-error-libpq-fe-h-when-installing-postgresql-package-for-r-on-linux
最終的に

sudo apt install libpq-dev

で解決しました。