close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Error: pg_config executable not found when installing psycopg2 on Alpine in Docker in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

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

Today I get the following error Error: pg_config executable not found when installing psycopg2 on Alpine in Docker in python.

How To Solve Error: pg_config executable not found when installing psycopg2 on Alpine in Docker Error ?

  1. How To Solve Error: pg_config executable not found when installing psycopg2 on Alpine in Docker Error ?

    To Solve Error: pg_config executable not found when installing psycopg2 on Alpine in Docker Error Simply add these commands (psycopg2 dependencies) before installing dependencies from requirements.txt in Dockerfile

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

    To Solve Error: pg_config executable not found when installing psycopg2 on Alpine in Docker Error Simply add these commands (psycopg2 dependencies) before installing dependencies from requirements.txt in Dockerfile

Solution 1

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.7
FROM python:3-alpine

WORKDIR /usr/src/app

COPY 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-deps

COPY . .

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.7

WORKDIR /usr/src/app

COPY 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-deps

COPY . .

CMD ["python3", "app.py"]

Solution 2

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

# install psycopg2 dependencies
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read