How do I install python on alpine linux?
Asked Answered
A

10

169

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]
Affixation answered 24/6, 2020 at 12:25 Comment(4)
This might help github.com/jfloff/alpine-pythonChokefull
OP specifically asks how to accomplish the task without using a python image.Averroes
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?Pyrrha
@TobiasFeil check out my answer, I think this is a possible alternative.Kaliski
P
247

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
Pusillanimous answered 24/6, 2020 at 12:40 Comment(5)
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]Walther
Interesting, wasn't aware that python3 -m ensurepip exists.Luong
@Walther 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.Bravo
ensurepip is pretty cool, for those interested here is more info on it: docs.python.org/3/library/ensurepip.htmlToomer
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.Triangle
D
123

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.

Dudgeon answered 18/12, 2020 at 23:21 Comment(9)
what's the equivalent of python-dev package for 3?Apterygial
@Apterygial it's python3-dev according to: Alpine Package search: python3-dev*Dudgeon
This is the correct way since 3.12 pkgs.alpinelinux.org/…Berneicebernelle
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.Premier
It seems py3-pip already present on main: pkgs.alpinelinux.org/packages?name=py3-pipEleven
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.Dudgeon
Oops, my mistake. I'm sorry.Eleven
Since Python now makes python3 -m ensurepip fail for system-installed Python, that makes sudo apk add --no-cache py3-pip the way forward.Laura
That link to the community repository is broken, and py3-pip always gets an error says missing.Ponder
A
26

instead of python3-pip install py3-pip

apk add --update python3 py3-pip
Adjective answered 6/5, 2021 at 9:59 Comment(2)
what is the difference?Salesperson
This doesn't work, at all, I get py3-pip missing.Ponder
K
25

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.

Kaliski answered 9/8, 2022 at 15:53 Comment(0)
A
17

You can try this command:

apk add python3
Apollus answered 3/2, 2021 at 8:25 Comment(0)
P
12

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

Pincushion answered 15/4, 2021 at 15:23 Comment(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?Acinaciform
@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/tagsPincushion
R
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
Recommendation answered 22/2, 2022 at 17:23 Comment(0)
A
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.

Acetophenetidin answered 25/6, 2023 at 6:39 Comment(0)
G
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!

Glottal answered 30/11, 2023 at 16:46 Comment(1)
This works. You can verify that pip was installed with python -m pip --version.Bushelman
E
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
Eft answered 7/2 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.