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.
Build image
Basic image use python36-alpine38-bot
if you need to install bash You can use the following Dockerfile:
FROM justgast/python36-alpine38-bot MAINTAINER Rethink RUN echo "http://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories RUN echo "http://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories RUN echo "http://dl-2.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories; RUN echo "http://dl-3.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories; RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories; RUN echo "http://dl-5.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories RUN echo "ipv6" >> /etc/modules RUN apk update \ && apk upgrade \ && apk add --no-cache bash \ bash-doc \ bash-completion \ && rm -rf /var/cache/apk/* \ && /bin/bash
Execute the following command to build the image:
docker build -t alpine-bash:v1 .
Installation data science related dependencies
If you want to install Numpy, Scipy, Matlotlib, Scikit-Learn, Pandas, etc., you need to install additional gcc dependencies.
RUN apk add gcc freetype-dev RUN apk add gfortran musl-dev g++ libgcc libquadmath musl libgfortran RUN apk add lapack-dev
Error exception resolution
Question 1
ModuleNotFoundError: No module named 'Cython' The solution is as follows: pip install cython
Question 2
If it is an offline installation package, it is possible to report the following error: ERROR: Could not find a version that satisfies the requirement scikit-learn==0.20.3 (from -r requirements.txt (line 30)) (from versions: none) ERROR: No matching distribution found for scikit-learn==0.20.3 (from -r requirements.txt (line 30)) The solution is as follows: pip download scikit-learn==0.20.3
Question 3
EnvironmentError: mysql_config not found The solution is as follows: RUN apk add --no-cache mariadb-dev build-base
Question 4
Error when installing pymssql: #include "sqlfront.h" ^ compilation terminated. error: command 'gcc' failed with exit status 1 The solution is as follows: apk add freetds-dev
How do I install a specific version of python on alpine linux (python3.8)?
cat /etc/os-release NAME=»Alpine Linux» VERSION_ID=3.15.0 PRETTY_NAME=»Alpine Linux v3.15″ HOME_URL=»alpinelinux.org» BUG_REPORT_URL=»bugs.alpinelinux.org» as for the python version that gets installed: python3 —version Python 3.9.7
1 Answer 1
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