Error: pg_config executable not found when installing psycopg2 on Alpine in Docker Error: pg_config executable not found when installing psycopg2 on Alpine in Docker docker docker

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker


Tested with Python 3.4.8, 3.5.5, 3.6.5 and 2.7.14 (just replace 3 with 2):

# You can use a specific version too, like python:3.6.5-alpine3.7FROM python:3-alpineWORKDIR /usr/src/appCOPY requirements.txt .RUN \ apk add --no-cache postgresql-libs && \ apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \ python3 -m pip install -r requirements.txt --no-cache-dir && \ apk --purge del .build-depsCOPY . .CMD ["python3", "app.py"]

Explanation: to build Psycopg you need the packages gcc musl-dev postgresql-dev. Then you also need that pg_config executable: while simply installing postgresql-dev will work, postgresql-libs does fine too and takes up some 12 MB less space.


Here's the original version of the answer (based on this Dockerfile) where I manually install Python onto a pure Alpine image because at that time Python did not provide the Docker image with Python 3.6 and Alpine 3.7. If you want to install Python 2.7 like that, also do apk add py2-pip (called py-pip in older Alpine repos).

FROM alpine:3.7WORKDIR /usr/src/appCOPY requirements.txt .RUN \ apk add --no-cache python3 postgresql-libs && \ apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev postgresql-dev && \ python3 -m pip install -r requirements.txt --no-cache-dir && \ apk --purge del .build-depsCOPY . .CMD ["python3", "app.py"]


Simply add these commands (psycopg2 dependencies) before installing dependencies from requirements.txt in Dockerfile

# install psycopg2 dependenciesRUN apk updateRUN apk add postgresql-dev gcc python3-dev musl-dev

Source: https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/