How do I 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 ${REGION} | docker login --username AWS --password-stdin ${REPOSITORY_URL}
- sam build
- sam deploy --no-confirm-changeset --no-fail-on-empty-changeset
only:
- master
This alphine version points to:https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
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
toalpine: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.
Some options:
- 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
Related Topics
Find Nearest Value in Numpy Array
How to Load All Modules in a Folder
Allowing Ctrl-C to Interrupt a Python C-Extension
How to Properly Write to Fifos in Python
Finding the Command for a Specific Pid in Linux from Python
Kill Python Interpeter in Linux from the Terminal
Pairing Bluetooth Devices with Passkey/Password in Python - Rfcomm (Linux)
How to Use Tensorflow Without Cuda on Linux
Can't Build Matplotlib (Png Package Issue)
Is There a Simple Way to Remove Multiple Spaces in a String
How to Prompt For User Input and Read Command-Line Arguments
Run a Python Script in Terminal Without the Python Command
Mismatch Between Sys.Executable and Sys.Version in Python
Linux/Python: Encoding a Unicode String for Print
How to Benchmark Part of Tensorflow Graph
Error: Command 'Gcc' Failed with Exit Status 1 on Centos
What's the Advantage of Queues Over Pipes When Communicating Between Processes