150

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]
Cutaraca
  • 2,069
  • 3
  • 14
  • 21

8 Answers8

219

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
Terry Spotts
  • 3,527
  • 1
  • 8
  • 21
  • 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 '20 at 17:39
  • 6
    Interesting, wasn't aware that `python3 -m ensurepip` exists. – phi Feb 18 '21 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 04 '21 at 10:33
  • `ensurepip` is pretty cool, for those interested here is more info on it: https://docs.python.org/3/library/ensurepip.html – Mint Sep 15 '22 at 02:07
109

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.

Linus H.
  • 1,185
  • 1
  • 7
  • 6
  • what's the equivalent of `python-dev` package for 3? – chovy Dec 25 '20 at 23:28
  • 2
    @chovy it's `python3-dev` according to: [Alpine Package search: python3-dev*](https://pkgs.alpinelinux.org/packages?name=python3-dev*) – Linus H. Dec 26 '20 at 22:11
  • 1
    This is the correct way since `3.12` https://pkgs.alpinelinux.org/contents?file=pip3&path=&name=&branch=v3.12 – 1efty Jan 08 '21 at 17:18
  • If you come across this error `importlib.metadata.PackageNotFoundError: pip` while trying to run e.g. `pip3 install` and you are using `alpine:3.13` docker image, try upgrading to `alpine:3.14`. – Ewa Dec 05 '21 at 19:05
  • 1
    It seems py3-pip already present on main: https://pkgs.alpinelinux.org/packages?name=py3-pip – johnlinp Sep 07 '22 at 16:19
  • Sorry @johnlinp, but I can not reproduce your findings. When opening the link you have provided, it clearly states, that `py3-pip` is only available in the `community` repo. – Linus H. Sep 10 '22 at 12:21
  • Oops, my mistake. I'm sorry. – johnlinp Sep 11 '22 at 16:54
23

instead of python3-pip install py3-pip

apk add --update python3 py3-pip
17

You can try this command:

apk add python3
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
adobean
  • 314
  • 2
  • 12
16

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.

Monsieur Merso
  • 1,459
  • 1
  • 15
  • 18
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

Vincent Pazeller
  • 1,448
  • 18
  • 28
  • 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? – potterson11 Jan 25 '23 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: https://hub.docker.com/_/python/tags – Vincent Pazeller Jan 25 '23 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
TomNash
  • 3,147
  • 2
  • 21
  • 57
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.

Rm4n
  • 623
  • 7
  • 14