196

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 Commented Jun 24, 2020 at 12:28
  • 15
    OP specifically asks how to accomplish the task without using a python image. Commented 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? Commented Feb 1, 2022 at 8:25
  • @TobiasFeil check out my answer, I think this is a possible alternative. Commented Aug 9, 2022 at 15:53

14 Answers 14

278

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
Sign up to request clarification or add additional context in comments.

10 Comments

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]
Interesting, wasn't aware that python3 -m ensurepip exists.
In the Alpine 3.19 image, running python3 -m ensurepip gives a long error message starting with "This environment is externally managed". Ih that case, use Linus H.'s answer: apk add --no-cache py3-pip.
Based off of answers by @Linus H. and @Craig S. Anderson, if using Python >=3.12, the last three lines of the Dockerfile should be replaced with RUN apk add --update --no-cache python3 py3-pip. Running pip -V inside of this container shows setuptools installed. Trying to run the pip3 install will result in an externally managed environment error
@majorgear (Sorry for the late comment) This is a pip switch, so it should go into the pip command line: pip instal --break-system-packages ....
|
149

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.

9 Comments

what's the equivalent of python-dev package for 3?
@chovy it's python3-dev according to: Alpine Package search: python3-dev*
This is the correct way since 3.12 pkgs.alpinelinux.org/…
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.
It seems py3-pip already present on main: pkgs.alpinelinux.org/packages?name=py3-pip
|
32

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.

Comments

29

instead of python3-pip install py3-pip

apk add --update python3 py3-pip

2 Comments

what is the difference?
This doesn't work, at all, I get py3-pip missing.
17

You can try this command:

apk add python3

Comments

13

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 Comments

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

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

Comments

2

For those of you who have tried the above

apk add py3-pip

and get

ERROR: unable to select packages:
  py3-pip (no such package):
    required by: world[py3-pip]

Check /etc/apk/repositories and ensure that the line that ends in /community is not commented out (starts with a #). If it is, remove the #, save, run apk update, and retry the installation.

Comments

2

Alpine linux container or bare installation, simple way as root:

apk add python3

apk add py3-setuptools

apk add py3-pip

You can try to use --update or --no-cache too.

Comments

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.

Comments

1

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!

1 Comment

This works. You can verify that pip was installed with python -m pip --version.
0

I tried many answers from different sources, eventually I copied the repository file and checked the naming convention by extracting the compressed file

wget https://dl-cdn.alpinelinux.org/alpine/edge/main/aarch64/APKINDEX.tar.gz
tar -xvf  APKINDEX.tar.gz
cat APKINDEX | grep python3 

and the found the line.

D:python3 python3~3.11

and the following worked

apk --update --no-cache add python3~3.11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main

Comments

0

You checkout docker hub, for an image of specific python and alpine, if you want do not want to use the latest version of python or alpine everytime. https://hub.docker.com/_/python/tags

example : docker pull python:3.11.9-alpine3.18 | you can use this base image to have python 3.11.9 and alpine 3.18

Comments

-2

Only solution that worked for me

(running docker on linux)

sudo nano /etc/docker/daemon.json

Add the following

{
  "dns": ["8.8.8.8"]
}

Restart docker

sudo service docker restart

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.