155

How do I install python3 and python3-pip on an alpine based image (without using a python image)?

 $ apk add --update python3.8 python3-pip
 ERROR: unsatisfiable constraints:
   python3-pip (missing):
     required by: world[python3-pip]
   python3.8 (missing):
     required by: world[python3.8]
4
  • This might help github.com/jfloff/alpine-python Jun 24, 2020 at 12:28
  • 13
    OP specifically asks how to accomplish the task without using a python image.
    – vhs
    Aug 6, 2020 at 6:53
  • 5
    You're obviously interested in installing python3.8, not just any version of python3. However, none of the answers address this. Has anyone figured out how to install a specific minor version of python3? Feb 1, 2022 at 8:25
  • @TobiasFeil check out my answer, I think this is a possible alternative. Aug 9, 2022 at 15:53

9 Answers 9

228

This is what I use in a Dockerfile for an alpine image:

# Install python/pip
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
4
  • 7
    I'm getting: WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz: No such file or directory ERROR: unsatisfiable constraints: python3 (missing): required by: world[python3]
    – Zac
    Nov 16, 2020 at 17:39
  • 6
    Interesting, wasn't aware that python3 -m ensurepip exists.
    – phi
    Feb 18, 2021 at 10:58
  • @Zac This error may occur if you have no community repository enabled in your /etc/apk/repositories. Check this file, uncomment or add community repository and retry.
    – lospejos
    Apr 4, 2021 at 10:33
  • ensurepip is pretty cool, for those interested here is more info on it: docs.python.org/3/library/ensurepip.html
    – Mint
    Sep 15, 2022 at 2:07
112

Take a look at the alpine package repo: https://pkgs.alpinelinux.org/packages So what you are looking for are the python3 and py3-pip packages.

A suitable command to use inside a dockerfile/etc. would be:

apk add --no-cache python3 py3-pip

Explanation of the --no-cache flag

Note however, that you need to add the community repository since py3-pip is not present on main.

7
23

instead of python3-pip install py3-pip

apk add --update python3 py3-pip
1
  • 4
    what is the difference?
    – Fauzan
    Apr 25, 2022 at 7:52
18

Additional option is to build python during image build:

FROM alpine:latest

# you can specify python version during image build
ARG PYTHON_VERSION=3.9.9

# install build dependencies and needed tools
RUN apk add \
    wget \
    gcc \
    make \
    zlib-dev \
    libffi-dev \
    openssl-dev \
    musl-dev

# download and extract python sources
RUN cd /opt \
    && wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz \                                              
    && tar xzf Python-${PYTHON_VERSION}.tgz

# build python and remove left-over sources
RUN cd /opt/Python-${PYTHON_VERSION} \ 
    && ./configure --prefix=/usr --enable-optimizations --with-ensurepip=install \
    && make install \
    && rm /opt/Python-${PYTHON_VERSION}.tgz /opt/Python-${PYTHON_VERSION} -rf
                                                                                                                                                                                                                                               # rest of the image, python3 and pip3 commands will be available

This snippet downloads and builds python of specified version from sources (together with pip). It may be an overkill but sometimes it may come in handy.

0
17

You can try this command:

apk add python3
0
11

You may use the python official image which offers alpine tags as well. You will probably get the most state-of-the-art python install:

e.g.:

FROM python:3-alpine

2
  • Presently, I tried your method as in FROM python:3.9-alpine:edge. But this throws invalid reference format. I require alpine edge because librdkafka 2.0.2 v is available only in alpine edge. Is there any other way to solve this error? Jan 25 at 11:11
  • @potterson11 I guess the tag you used (python:3.9-alpine:edge) does not exists. I have not seen any available edge tags here: hub.docker.com/_/python/tags Jan 25 at 16:39
2

It looks like you're trying to install a specific minor version of Python3 (3.8), you can do this in Alpine by using semver like this which will install a version of python3>=3.8.0 <3.9.0-0:

apk add python3=~3.8
1

I had to install python in an air gap network so I couldn't run apk add. Here's how I got required packages inside an online alpine container:

apk fetch python3 py3-pip libbz2 libexpat libffi gdbm mpdecimal libpanelw readline \
    sqlite-libs py3-setuptools libgcc libstdc++ py3-packaging py3-parsing

And my Dockerfile looks like this:

ENV PYTHONUNBUFFERED=1
COPY ./*.apk .

RUN apk add --allow-untrusted --no-network libgcc* libstdc++* gdbm* libbz2* \
    libexpat* libffi* libpanel* mpdecimal* \
    readline* sqlite* \
    python3-3.11.4-r0.apk py3-parsing* py3-packaging* py3-setuptools* py3-pip-23.1.2-r0.apk && \
    rm *.apk && \
    ln -sf python3 /usr/bin/python

And the dependency hell got resolved.

0

For those looking to install pip in Alpine Linux you might not get py3-pip to install via apk. After you install python: apk add python3, then you run python -m ensurepip. That's what worked for me.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.