- How to Install Python on Alpine Linux
- How to install Python 3.8 on Alpine Linux not from Python base image
- How do I install a specific version of python on alpine linux (python3.8)?
- Can’t install Python package on Alpine Docker anymore
- Docker Alpine Linux python (missing)
- Use Python package installed with apk in Alpine Linux
- Installing Python on iSH
- How to install python on empty alpine container
- How to install Python 3.8 on Alpine Linux not from Python base image
- 2 Answers 2
- How do I install python on alpine linux?
- 8 Answers 8
How to Install Python on Alpine Linux
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
How to install Python 3.8 on Alpine Linux not from Python base image
In the time of writing, the latest frolvlad/alpine-glibc image is based on Alpine 3.13.2. The current Python 3 version for Alpine 3.13 is 3.8.8 . Therefore, for installing Python 3.8 simply install the python3 package. This is also true for Alpine 3.12 and 3.11.
If you’re using frolvlad/alpine-glibc based on Alpine 3.13, 3.12 or 3.11, try updating the apk database with apk update followed by apk add python3 .
On images based on older Alpine versions, such as Alpine 3.9, you’ll not be able to install a functional Python 3.8, since it depends on musl 1.1.24 which is only available starting Alpine 3.10. Even though it could be installed using the Alpine 3.11+ repositories, it will fail to run due to the said musl dependency:
/ # apk add python3=3.8.2-r2 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.11/main
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
(1/10) Installing libbz2 (1.0.8-r1)
(2/10) Installing expat (2.2.9-r1)
(3/10) Installing libffi (3.2.1-r6)
(4/10) Installing gdbm (1.13-r1)
(5/10) Installing xz-libs (5.2.4-r0)
(6/10) Installing ncurses-terminfo-base (6.1_p20200118-r4)
(7/10) Installing ncurses-libs (6.1_p20200118-r4)
(8/10) Installing readline (8.0.1-r0)
(9/10) Installing sqlite-libs (3.30.1-r2)
(10/10) Installing python3 (3.8.2-r2)
Executing busybox-1.29.3-r10.trigger
Executing glibc-bin-2.29-r0.trigger
OK: 71 MiB in 27 packages
/ # python3 --version
Error relocating /usr/lib/libpython3.8.so.1.0: copy_file_range: symbol not found
How do I install a specific version of python on alpine linux (python3.8)?
Finally I solve it
Probably I lacked context
I’m doing a gitlab.yaml file to build, test and deploy my application in AWS thorugh SAM.
Since one of my lambda functions is a dockerized lambda function I nee sam to be able to access docker as a command so it can run docker pull and docker build
The other lambda functions that I have use python3.8 as runtime so the docker version I was using as a base image pointed to https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/ so everyt time something was installed with apk the version was 3.15 which has python3.10.
A solution to this is use:
image: docker:19.03.15-alpine3.13 as a base image with the service dind like this:
image: docker:19.03.15-alpine3.13
## This will run a Docker daemon in a container (Docker-In-Docker), which will
## be available at thedockerhost:2375. If you make e.g. port 5000 public in Docker
## (`docker run -p 5000:5000 yourimage`) it will be exposed at thedockerhost:5000.
services:
— name: docker:dind
alias: thedockerhost
variables:
# Tell docker CLI how to talk to Docker daemon; see
# https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor
DOCKER_HOST: tcp://thedockerhost:2375/
# DOCKER_HOST: tcp://docker:2375/
# Use the overlays driver for improved performance:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: «»
REPOSITORY_URL: ######.dkr.ecr.region.amazonaws.com/
REGION: us-east-2
deploy:
stage: deploy
before_script:
— apk add —no-cache python3
— apk add —no-cache curl jq
— apk add —no-cache python3-dev py3-setuptools
— apk add —no-cache py3-pip
— apk add —no-cache py-pip
— apk add —no-cache build-base g++ make cmake unzip curl-dev
— apk add —no-cache autoconf automake libtool libexecinfo-dev
— apk add —no-cache git
— pip3 install —no-cache —upgrade wheel
— pip3 install —no-cache awscli —upgrade
— pip3 install —no-cache aws-sam-cli —upgrade
script:
— aws ecr get-login-password —region $ | docker login —username AWS —password-stdin $
— sam build
— sam deploy —no-confirm-changeset —no-fail-on-empty-changeset
only:
— master
This alphine version points to:
which installs python3.8 and now sam is able to package/build the rest of the lambda functions.
Can’t install Python package on Alpine Docker anymore
You are trying to use the python (alias) library instead of python3 .
Try to use apk update && apk upgrade && apk add python3 instead.
Docker Alpine Linux python (missing)
From this issue on the Docker’s repo:
This was «broken» while updating our base from alpine:3.11 to alpine:3.12 .
In order to fix it you need to specify the version of Python directly, e.g.:
apk add python2
// or
apk add python3
Use Python package installed with apk in Alpine Linux
The issue is that python:3-alpine has two Pythons: the one provided by Alpine, and an additional one added by the Python Docker image. Installing packages in one will not be reflected in the other.
- Switch to just alpine base image, FROM alpine:3.10 . Then you’ll just have the Python installed via apk.
- Stop using Alpine, and switch to FROM python:3.7-slim-buster (my personal recommendation: https://pythonspeed.com/articles/base-image-python-docker-images/). This will allow you to pip install numpy without having to compile anything—binary wheels don’t work on Alpine, but will work on the (Debian) Buster image.
Installing Python on iSH
According information that you provided iSH using virtual environment with Alpine Linux x86 under the hood (I little bit simplify explanation, so it is not 100% correct. You can see details here).
So if you want to install pip you have to search how to install pip in Alpine Linux. You will find many answers like that:
apk add --update-cache python3 py3-pip
This information applicable to any other package that you will try to install. Not just pip.
How to install python on empty alpine container
apt-get is Debian/Ubuntu’s package manager. On Alpine you need to stick with apk , the Alpine package manager.
If you run across Docker guides that use apt-get you can’t run the commands directly. You need to look up the equivalent package names and use apk .
# apk update
# apk add python3
How to install Python 3.8 on Alpine Linux not from Python base image
Specifically in this base image https://hub.docker.com/r/frolvlad/alpine-glibc/ how does one add Python 3.8 to it.. Adding python3 installs Python3.6.9. Went through How do I install python on alpine linux? but couldn’t figure it out.
Yeah, tried 38 and 3.8 results in this error: ERROR: unsatisfiable constraints: python38 (missing): required by: world[python38] The command ‘/bin/sh -c apk add —update python38’ returned a non-zero code: 1
Hmm. hub.docker.com/_/python lists 3.8-alpine as a tag that exists, and links to the Dockerfile at github.com/docker-library/python/blob/… . Maybe something there will help you?
You probably have an old version or old tag of this alpine-glibc image because on the latest version, they go FROM alpine:3.13 which will install a Python 3.8. What version or tag of the glibc image ore you using?
That said, alpine tends to have a package of the dependencies per version, if the package manager does not have the version you want and you cannot afford to upgrade your alpine version, you’re left with installing what you want from sources.
2 Answers 2
Another solution, use official Python Docker image and COPY :
FROM python:3.8.13-alpine3.16 as python COPY --from=python /usr/local/bin/python3 /usr/local/bin/python3 COPY --from=python /usr/local/lib/python3.8 /usr/local/lib/python3.8 COPY --from=python /usr/local/lib/libpython3.8.so.1.0 /usr/local/lib/libpython3.8.so.1.0 COPY --from=python /usr/local/lib/libpython3.so /usr/local/lib/libpython3.so
Still, pip install of any dependency was installed under the python3.9-sites. Didn’t seem to have worked @thomas-decaux
In the time of writing, the latest frolvlad/alpine-glibc image is based on Alpine 3.13.2. The current Python 3 version for Alpine 3.13 is 3.8.8 . Therefore, for installing Python 3.8 simply install the python3 package. This is also true for Alpine 3.12 and 3.11.
If you’re using frolvlad/alpine-glibc based on Alpine 3.13, 3.12 or 3.11, try updating the apk database with apk update followed by apk add python3 .
On images based on older Alpine versions, such as Alpine 3.9, you’ll not be able to install a functional Python 3.8, since it depends on musl 1.1.24 which is only available starting Alpine 3.10. Even though it could be installed using the Alpine 3.11+ repositories, it will fail to run due to the said musl dependency:
/ # apk add python3=3.8.2-r2 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.11/main fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz (1/10) Installing libbz2 (1.0.8-r1) (2/10) Installing expat (2.2.9-r1) (3/10) Installing libffi (3.2.1-r6) (4/10) Installing gdbm (1.13-r1) (5/10) Installing xz-libs (5.2.4-r0) (6/10) Installing ncurses-terminfo-base (6.1_p20200118-r4) (7/10) Installing ncurses-libs (6.1_p20200118-r4) (8/10) Installing readline (8.0.1-r0) (9/10) Installing sqlite-libs (3.30.1-r2) (10/10) Installing python3 (3.8.2-r2) Executing busybox-1.29.3-r10.trigger Executing glibc-bin-2.29-r0.trigger OK: 71 MiB in 27 packages / # python3 --version Error relocating /usr/lib/libpython3.8.so.1.0: copy_file_range: symbol not found
How do I install python on alpine linux?
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?
8 Answers 8
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
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 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.
ensurepip is pretty cool, for those interested here is more info on it: docs.python.org/3/library/ensurepip.html
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
Note however, that you need to add the community repository since py3-pip is not present on main .
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 .
instead of python3-pip install py3-pip
apk add --update python3 py3-pip
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-$.tgz \ && tar xzf Python-$.tgz # build python and remove left-over sources RUN cd /opt/Python-$ \ && ./configure --prefix=/usr --enable-optimizations --with-ensurepip=install \ && make install \ && rm /opt/Python-$.tgz /opt/Python-$ -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.