Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing psycopg2-binary with Python:3.6.4-alpine doesn't work #684

Closed
barrachri opened this issue Feb 28, 2018 · 20 comments
Closed

Installing psycopg2-binary with Python:3.6.4-alpine doesn't work #684

barrachri opened this issue Feb 28, 2018 · 20 comments
Labels
wheel Bugs related to the wheel distribution, not happening installing from source

Comments

@barrachri
Copy link

barrachri commented Feb 28, 2018

When you try to install the binary version of the lib you get the source version error.

$ docker run -it --rm python:3.6.4-alpine sh
$ pip install psycopg2-binary

Collecting psycopg2-binary
  Downloading psycopg2-binary-2.7.4.tar.gz (426kB)
    100% |████████████████████████████████| 430kB 52kB/s
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2_binary.egg-info
    writing pip-egg-info/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2_binary.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2_binary.egg-info/SOURCES.txt'

    Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.

    For further information please check the 'doc/src/install.rst' file (also at
    <http://initd.org/psycopg/docs/install.html>).
@dvarrazzo
Copy link
Member

Alpine Linux use musl libc and manylinux wheels don't support it: see e.g. pypa/manylinux#37. Not a psycopg problem but a lack of a standard for it, such as PEP 513 defininig manylinux1. Happy to support Alpine as soon as there will be a standard and tools for it.

@nourspace
Copy link

nourspace commented May 25, 2018

I found this solution which worked quite fine.

FROM python:3.6-alpine

RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev

pip install psycopg2-binary

Edit

The above compiles the package from source, so I would rather

FROM python:3.6-alpine
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
pip install psycopg2

not to get confused.

@barrachri
Copy link
Author

But I think you are pseudo-using the binary package and building from the source at the end.

@dvarrazzo
Copy link
Member

@nourchawich your "solution" is about compiling psycopg from source, which is totally supported. Binary packages are not (for the reason described above).

@nourspace
Copy link

Thank for the clarification @barrachri and @dvarrazzo

I just noticed what was actually happening. All I wanted was to have psycopg2 (regardless whether binary or not) installed on an alpine based image, hence considering it a solution.

@cscetbon
Copy link

@nourchawich after it's been built, I suppose we can get rid of those dev packages ?

@siaarzh
Copy link

siaarzh commented Oct 22, 2018

@nourchawich after it's been built, I suppose we can get rid of those dev packages ?

Yes, but make sure the apt add and apt del commands are in one layer. Otherwise you won't make the image any smaller.
E.g.:

RUN apk add --no-cache --virtual .build-deps \
    gcc \
    python3-dev \
    musl-dev \
    postgresql-dev \
    && pip install --no-cache-dir psycopg2 \
    && apk del --no-cache .build-deps

@cscetbon
Copy link

Yeah I did, thanks !

@Cerebus
Copy link

Cerebus commented Jan 13, 2019

Since this is the top google hit for this problem, use a multi-stage build to minimize the final image size.

FROM python:3.6-alpine as base                                                                                                
                                                                                                                              
FROM base as builder                                                                                                          
                                                                                                                              
RUN mkdir /install                                                                                                            
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev                                                             
WORKDIR /install                                                                                                              
COPY requirements.txt /requirements.txt                                                                                       
RUN pip install --install-option="--prefix=/install" -r /requirements.txt                                                     
                                                                                                                              
FROM base                                                                                                                     
                                                                                                                              
COPY --from=builder /install /usr/local                                                                                       
COPY src /app                                                                                                         
RUN apk --no-cache add libpq                                                                                                  
WORKDIR /app

@hugobuddel
Copy link

This is still a bit unclear to me. The point of installing psycopg2-binary is that compilers like gcc etc. are not necessary right?

I'm getting the same message as the original poster. When trying to install psycopg2-binary, it complains about not finding gcc and suggests to, well, install psycopg2-binary (which is what I'm trying to do).

@hugobuddel
Copy link

I'm on Centos 7.4, and apparently I had to yum install postgresql-devel to make psycopg2-binary to work.

@Cerebus
Copy link

Cerebus commented Jan 14, 2019

@hugobuddel The problem is when pip decides it can't install the psycopg2-binary wheel it falls back on building from source, which naturally requires dev packages. On alpine that's because of muslc instead of glibc. I should think this shouldn't happen on CentOS but maybe there's another conflict.

@Pk13055
Copy link

Pk13055 commented Oct 4, 2019

Building on @Cerebus's answer, to get this to work with pipenv, you can try something like this:

FROM python:3.6-alpine as base

FROM base as builder

RUN mkdir /install
RUN apk update \
 && apk add postgresql-dev gcc python3-dev musl-dev
RUN pip install pipenv  # to lock and create requirements.txt
WORKDIR /install

# creation of requirements.txt
COPY Pipfile ./Pipfile
RUN pipenv lock -r > requirements.txt

# everything else stays the same
RUN pip install --install-option="--prefix=/install" -r ./requirements.txt

FROM base

COPY --from=builder /install /usr/local
COPY .  /app
RUN apk --no-cache add libpq
WORKDIR /app

@nourspace
Copy link

While it is possible to use multi-stage builds or add libraries to compile from source, I recently started using the slim version instead of alpine.

It installs psycopg2-binary without issues.

FROM python:3.6-slim

pip install psycopg2-binary

I found this article to be very useful in deciding which Python base image to use for production.
https://pythonspeed.com/articles/base-image-python-docker-images/

kaxil added a commit to astronomer/airflow that referenced this issue Dec 31, 2019
uselessscat added a commit to uselessscat/flask-postgis-example that referenced this issue Jan 30, 2020
@dvarrazzo dvarrazzo added the wheel Bugs related to the wheel distribution, not happening installing from source label Feb 15, 2020
kaxil added a commit to astronomer/airflow that referenced this issue Feb 28, 2020
@finete
Copy link

finete commented Mar 4, 2020

@Pk13055 I would avoid using pipenv as it seems to be dead

@Pk13055
Copy link

Pk13055 commented Mar 8, 2020 via email

kaxil added a commit to astronomer/airflow that referenced this issue Mar 23, 2020
psycopg/psycopg2#684 (comment) - Not supported on Alpine
(cherry picked from commit f9822ea)
(cherry picked from commit 947c05f)
@Asday
Copy link

Asday commented May 29, 2020

@MosheVai pipenv appears to have pulled a Lazarus as of a few hours ago.

@Budgiebrain994
Copy link

Alpine Linux use musl libc and manylinux wheels don't support it: see e.g. pypa/manylinux#37. Not a psycopg problem but a lack of a standard for it, such as PEP 513 defininig manylinux1. Happy to support Alpine as soon as there will be a standard and tools for it.

Looks like musllinux support has been added to manylinux: pypa/manylinux#1135

Perhaps this issue could have a revisit?

@LukasWestholt
Copy link

FROM python:3.10.4-alpine
RUN apk add --no-cache --virtual build-dependencies libpq-dev build-base \
    && pip install -r requirements.txt \
    && apk del --no-cache build-dependencies

seems to work too.

@vaibhavgupta082
Copy link

vaibhavgupta082 commented Mar 31, 2023

not able to install psycopg2 on my docker environment

requirement.txt
Django>=3.2.4,<3.3 djangorestframework>=3.12.4,<3.13 psycopg2>=2.8.6,<2.9

dockerfile
`
FROM python:3.9-alpine3.13
LABEL maintainer="*******************"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./app /app
WORKDIR /app
EXPOSE 8000

ARG DEV=false
RUN python -m venv /py &&
/py/bin/pip install --upgrade pip &&
apk add --update --no-cache postgresql-client &&
apk add --update --no-cache --virtual .tmp-build-deps
build-base postgresql-dev gcc python3-dev musl-dev &&
/py/bin/pip install -r /tmp/requirements.txt &&
if [ $DEV = "true" ];
then /py/bin/pip install -r /tmp/requirements.dev.txt ;
fi && \
rm -rf /tmp &&
apk del .tmp-build-deps &&
adduser
--disabled-password
--no-create-home
django-user

ENV PATH="/py/bin:$PATH"

USER django-user
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wheel Bugs related to the wheel distribution, not happening installing from source
Projects
None yet
Development

No branches or pull requests