Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@aviatorBeijing

This comment has been minimized.

Copy link

aviatorBeijing commented Nov 27, 2017

Better use following:
RUN pip install --no-cache-dir numpy scipy pandas matplotlib

@windlessuser

This comment has been minimized.

Copy link

windlessuser commented Jun 7, 2018

This was working until about yesterday. Could use some help with a fix. I think something got updated, I'm not sure what.

@stetsonwood

This comment has been minimized.

Copy link

stetsonwood commented Jun 8, 2018

numpy updated and is not compatible with alpine. use numpy==1.14.3 or an older version and it should work

@windlessuser

This comment has been minimized.

Copy link

windlessuser commented Jun 8, 2018

It still fails on older versions of Numpy where it was working before. I think the update was in one of the C headers it needs, During the compile, it complains about some typing issues. I've gone from version 1.14.2 - 1.13.0. Even the last known version that was working 1.14.2

@LKozlowski

This comment has been minimized.

Copy link

LKozlowski commented Jun 8, 2018

I had the same problem as you and found that pandas is still trying to install most recent version of numpy even if you already satisfied its requirement of numpy > 1.9 so I resolved it by:

  • install numpy version < 1.14.4 so 1.14.3 is fine
  • install python-dateutil (which is a pandas dependency)
  • install pandas without dependencies by running pip install --no-deps pandas==0.23.0
  • install other packages
@gbroccolo

This comment has been minimized.

Copy link

gbroccolo commented Jun 12, 2018

Also pytz>=2011k is a Pandas dependency, let's include it together with python-dateuti.

@byaminov

This comment has been minimized.

Copy link

byaminov commented Jun 12, 2018

Thanks a lot for the tip to use numpy==1.14.3! 👍

@tgsmith61591

This comment has been minimized.

Copy link

tgsmith61591 commented Oct 31, 2018

Is this still working? I'm getting:

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]
@flyrev

This comment has been minimized.

Copy link

flyrev commented Nov 12, 2018

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
@mdiazcl

This comment has been minimized.

Copy link

mdiazcl commented Nov 28, 2018

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
@JensRantil

This comment has been minimized.

Copy link

JensRantil commented Jan 30, 2019

gcc is likely not needed as build-base depends on it.

@MartinThoma

This comment has been minimized.

Copy link

MartinThoma commented May 6, 2019

This works for me:

FROM: python:alpine

RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories

# Add HDF5 support
RUN apk add --no-cache --allow-untrusted --repository http://dl-3.alpinelinux.org/alpine/edge/testing hdf5 hdf5-dev
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

The rest could be installed via the dependencies automatically by pip.

@orhun

This comment has been minimized.

Copy link

orhun commented May 7, 2019

@MartinThoma what's the HDF5 library for?

@promehul

This comment has been minimized.

Copy link

promehul commented May 16, 2019

This isn't working..
ERROR: Command "/usr/local/bin/python -u -c 'import setuptools, tokenize;file='"'"'/tmp/pip-install-2u_c2pqi/numpy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-gfsn5nsl/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-2u_c2pqi/numpy/
This ois i am getting..

@ozichert

This comment has been minimized.

Copy link

ozichert commented Jun 26, 2019

First add py-numpy, since directly installing with pip doesn't work.
RUN apk add py-numpy
In the next step install it with pip.
RUN python -m pip install numpy

@felixraboacct

This comment has been minimized.

Copy link

felixraboacct commented Jul 23, 2019

First add py-numpy, since directly installing with pip doesn't work.
RUN apk add py-numpy
In the next step install it with pip.
RUN python -m pip install numpy

pip install numpy fails ... tries to install it normally. And yet without this step Python gives module not found error. So how do you tell Python that the package was already installed via linux package manager?

@Faylixe

This comment has been minimized.

Copy link

Faylixe commented Aug 13, 2019

I figured out how to make numpy package from Alpine repository to work. Actually py-numpy does not really install bumpy but py3-numpy does.

But even though, py3-numpy installs libraries into /usr/lib/python3.7/site-packagesdirectory but default Python module path does not use it :

$ docker run -it python:3-alpine sh
/ # apk add --update --no-cache py3-numpy
...
/ # python
>>> import numpy
>>> ... module not found ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

I fixed the problem by settings $PYTHONPATH environment variable to /usr/liblocated site packages :

FROM python:3-alpine

RUN apk add --update --no-cache py3-numpy
ENV PYTHONPATH=/usr/lib/python3.7/site-packages

EDIT: you might also need to install py-numpy-dev to get Numpy C headers required by other Python compiled libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.