31

To use PostgreSql in python I need to

pip install psycopg2   

However, it has dependency on libpq-dev and python-dev. I wonder how can I install the dependencies in alpine? Thanks.

Here is a Dockerfile:

FROM python:2.7-alpine

RUN apk add python-dev libpq-dev
RUN pip install psycopg2

and the output is:

Step 3 : RUN apk add python-dev libpq-dev ---> Running in 3223b1bf7cde WARNING: Ignoring APKINDEX.167438ca.tar.gz: No such file or directory WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory ERROR: unsatisfiable constraints: libpq-dev (missing): required by: world[libpq-dev] python-dev (missing): required by: world[python-dev] ERROR: Service 'service' failed to build: The command '/bin/sh -c apk add python-dev libpq-dev' returned a non-zero code: 2

Felipe Buccioni
  • 19,109
  • 2
  • 28
  • 28
salehinejad
  • 7,258
  • 3
  • 18
  • 26

8 Answers8

44

If you only need to install psycopg2 for python 2.7 on Docker image based on python:2.7-alpine then following code for Dockerfile will be nice for you:

FROM python:2.7-alpine

RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add postgresql-dev

RUN pip install psycopg2
Andrii
  • 822
  • 10
  • 24
29

An explanation before compile/install psycopg2

  • libpq is the client library for PostgreSQL
  • postgresql-dev are the package with the source headers to link libpq in a library/binary in a compilation, in this case when pip compiles psycopg .

I use the following configuration in alpine 3.7, I add some comments to explain it.

# Installing client libraries and any other package you need
RUN apk update && apk add libpq

# Installing build dependencies
# For python3 you need to add python3-dev *please upvote the comment
# of @its30 below if you use this*
RUN apk add --virtual .build-deps gcc python-dev musl-dev postgresql-dev

# Installing and build python module
RUN pip install psycopg2

# Delete build dependencies
RUN apk del .build-deps
Felipe Buccioni
  • 19,109
  • 2
  • 28
  • 28
  • 1
    I think this is a valid solution. psycopg2 needs the psotgresql-libs and that in turns needs the libpq as a RUNTIME (dynamically linked) dependency. – Peter Lada Sep 05 '18 at 02:12
  • 17
    For python3 I had to add `python3-dev` to my dependencies – its30 Jul 17 '19 at 00:13
7

Had problems with running Python 3.7 and PostgreSQL under Alpine Linux in Docker. This article helped https://www.rockyourcode.com/install-psycopg2-binary-with-docker/

The main thing is to reference psypcopg2-binary in your requirements file and install the following packages (in Dockerfile):

RUN apk update && \
apk add --no-cache --virtual build-deps gcc python3-dev musl-dev && \
apk add postgresql-dev
Aleksandr
  • 489
  • 1
  • 4
  • 12
5

Seems like the package you need is libpq not libpq-dev:

https://pkgs.alpinelinux.org/package/edge/main/x86/py2-psycopg2

Have a look at the dependencies at the right

Marco Roy
  • 4,004
  • 7
  • 34
  • 50
Maximilian Kindshofer
  • 2,753
  • 3
  • 22
  • 37
  • i have tried to install lnstall libpq only and it didn't work. – shehata Jan 17 '17 at 03:45
  • Thanks for the the answer; Its looks like libpq is not the solution. Here is a part of the docker response: "Step 3 : RUN apk add libpq ---> Running in 04206f15a8f7 WARNING: Ignoring APKINDEX.167438ca.tar.gz: No such file or directory WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory ERROR: unsatisfiable constraints: libpq (missing): required by: world[libpq]" – salehinejad Jan 17 '17 at 21:57
  • I ended with installing the system package and delete it from requirements.txt – Maximilian Kindshofer Jan 17 '17 at 22:18
5

I couldn't get it to install from python:2.7.13-alpine. Ended up with this:

FROM gliderlabs/alpine:3.3

RUN apk add --no-cache --update \
    python \
    python-dev \
    py-pip \
    build-base

RUN apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add --no-cache --update postgresql-dev && \
    pip install psycopg2==2.7.1
pbatey
  • 1,234
  • 13
  • 13
0

add it in dockerfile

RUN apk update && apk add --no-cache --virtual .build-deps\
    postgresql-dev gcc libpq  python3-dev musl-dev linux-headers\ 
    && pip install --no-cache-dir -r requirements.txt\
    && apk del .build-deps\
    && rm -rf /var/cache/apk/*
0

What helped me was:

RUN apk add --no-cache python3 \
&& python3 -m ensurepip \
&& pip3 install --upgrade pip setuptools \
&& apk add build-base \
&& apk add gcc musl-dev libffi-dev openssl-dev python3-dev \
&& apk add postgresql-dev \
&& rm -r /usr/lib/python*/ensurepip && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache

RUN pip install --trusted-host pypi.python.org psycopg2

Especially apk add gcc musl-dev libffi-dev openssl-dev python3-dev as indicated in Docker: Installing python cryptography on alpine linux distribution

Roxana Tapia
  • 115
  • 9
0

This may have to something with blockage of some countries by docker so you may sure you are using vpn

Farhang Amaji
  • 742
  • 11
  • 24