Skip to content

Instantly share code, notes, and snippets.

@orenitamar
Last active March 22, 2024 05:13
Show Gist options
  • Star 87 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save orenitamar/f29fb15db3b0d13178c1c4dd611adce2 to your computer and use it in GitHub Desktop.
Save orenitamar/f29fb15db3b0d13178c1c4dd611adce2 to your computer and use it in GitHub Desktop.
Installing numpy, scipy, pandas and matplotlib in Alpine (Docker)
# Below are the dependencies required for installing the common combination of numpy, scipy, pandas and matplotlib
# in an Alpine based Docker image.
FROM alpine:3.4
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install numpy scipy pandas matplotlib
@rendorHaevyn
Copy link

Thanks @orenitamar.

@mkdthanga: The following worked for me - I simply used python3-alpine build, and removed python, python-dev and py-pip from the apk command (line 5, OP):

FROM python:3-alpine
WORKDIR /usr/source/app
COPY requirements.txt ./

RUN apk update
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk --no-cache --update-cache add gcc gfortran build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install --no-cache-dir -r requirements.txt

@PJCAfonso
Copy link

I'm getting

fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
fetch http://dl-8.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  so:libgfortran.so.5 (missing):
    required by:
                 openblas-ilp64-0.3.3-r2[so:libgfortran.so.5]
                 openblas-ilp64-0.3.3-r2[so:libgfortran.so.5]
                 openblas-0.3.3-r2[so:libgfortran.so.5]
                 openblas-0.3.3-r2[so:libgfortran.so.5]
The command '/bin/sh -c apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev' returned a non-zero code: 4

Had the same issue... changed the repos from dl-8 to CDN and it worked!

Here's the new Dockerfile

FROM alpine:3.4
RUN echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/main" > /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories
RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install numpy scipy pandas matplotlib

Thanks man your answers solved my problem!

@aboutmedicine
Copy link

I had various problems on the way starting FROM alpine, FROM python:alpine, but with the following I had a smooth docker build experience:

FROM python:slim
pip install numpy scipy

I assume you can add matplotlib and pandas as extra packages without problems.
See https://pythonspeed.com/articles/alpine-docker-python/ for my choice to start from python:slim.

Thanks - this is a great option for my use case

@pPanda-beta
Copy link

In case you cant move to slim-buster, the natural answer to this is
pip install --extra-index-url https://alpine-wheels.github.io/index ...

@samanthahw21
Copy link

Hi, can anyone help. I tried to execute the line in the Docker file but it takes very long to run .
Can anyone can help and what should i do ?
i didn't include panda version in my requirements - i tried but it run error . should i include and what version ? thanks

Pull a pre-built alpine docker image with nginx and python3 installed

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
ENV LISTEN_PORT=8000
EXPOSE 8000
COPY /app /app
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install pandas

@Frikster
Copy link

Thanks @orenitamar.

@mkdthanga: The following worked for me - I simply used python3-alpine build, and removed python, python-dev and py-pip from the apk command (line 5, OP):

FROM python:3-alpine
WORKDIR /usr/source/app
COPY requirements.txt ./

RUN apk update
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk --no-cache --update-cache add gcc gfortran build-base wget freetype-dev libpng-dev openblas-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install --no-cache-dir -r requirements.txt

This leads to ERROR: Failed building wheel for pyarrow for me

@PaleNeutron
Copy link

PaleNeutron commented Jul 19, 2022

@Frikster If you are using python:3-alpine which means you prefer the latest python3.10 version, you could consider using package from alpine instead of from pip.

This way need python package from alpine!!!!

FROM alpine:3.16
RUN apk --no-cache --update-cache add  python3 py3-pip py3-arrow  py3-pandas # and py3-anything package need to be compiled
RUN pip install --no-cache-dir -r requirements.txt

@Tejas9019
Copy link

=> ERROR [6/8] RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev 5.3s

[6/8] RUN apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev:
nity/x86_64/APKINDEX.tar.gz
#11 5.137 WARNING: Ignoring http://dl-8.alpinelinux.org/alpine/edge/community: DNS lookup error
#11 5.215 ERROR: unable to select packages:
#11 5.252 python (no such package):
#11 5.252 required by: world[python]
#11 5.252 python-dev (no such package):
#11 5.252 required by: world[python-dev]


executor failed running [/bin/sh -c apk --no-cache --update-cache add gcc gfortran python python-dev py-pip build-base wget freetype-dev libpng-dev openblas-dev]: exit code: 2

I am getting error like this pls can anyone help me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment