Install numpy on Docker Alpine

I am trying to install numpy in an Alpine 3.1 based docker container. I am using the following Docker file:

FROM alpine:3.1 RUN apk add --update make cmake gcc g++ gfortran RUN apk add --update python py-pip python-dev RUN pip install cython RUN pip install numpy 

This works fine before pip install numpy when I get the following error:

 error: Command "gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -Inumpy/core/include -Ibuild/src.linux-x86_64-2.7/numpy/core/include/numpy -Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/usr/include/python2.7 -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -c build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.c -o build/temp.linux-x86_64-2.7/build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.o" failed with exit status 1 

easy_install-2.7 numpy gives the same error.

Are there any configuration / installation steps that I am missing?

+21
source share
5 answers

If you do not need to install numpy from pypi , you can install it from alpine repositories. The package is called py-numpy and is located in the testing repository, see here . Minimal Dockerfile example that works for me

 FROM alpine:3.2 ADD repositories /etc/apk/repositories RUN apk add --update python python-dev gfortran py-pip build-base py-numpy@community 

Repositories file contents

 http://dl-cdn.alpinelinux.org/alpine/v3.2/main @community http://dl-cdn.alpinelinux.org/alpine/edge/community 
+25
source

I had slight problems with this, and in short, I would advise you to ask if this is really worth the hassle. Numpy is huge when you start adding things like pandas, gpus and scipy to the stack, so the benefits of building it on alpine are limited, and the savings compared to using Debian, Arch, or even Ubuntu are relatively small when 500 MB of your space is turned on. this library anyway.

Having said that, I put together an image that does this. I needed build time dependencies: musl-dev, linux-headers and g ++. I also had to add openblas from edge for something later on the stack, so it is entirely possible that some dependencies would also be required. But I believe that simply adding the three former libraries with

 apk --no-cache add musl-dev linux-headers g++ 

should be enough to prevent a gcc error from occurring. You can view the image at https://hub.docker.com/r/o76923/alpine-numpy-stack/

+25
source

Try the following:

 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 
+1
source

This size is about 311 MB, according to my docker images :

 FROM python:3.6-alpine RUN apk add g++ RUN pip install numpy 

(Meanwhile, python:3.6 alone is ~ 900 MB)

Have you tried NOT with gcc installed? Could this be controversial? I can’t say for sure. This one worked for me as a minimal installation and wanted to share.

0
source

The package is now available in the Alpine repository: py3-numpy . Although he did not work as in.

Indeed, py3-numpy installs libraries in the /usr/lib/python3.7/site-packages directory, but the path to the Python module does not use it by default:

 $ 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 setting the $PYTHONPATH environment variable for the /usr/lib site packages:

 FROM python:3-alpine RUN apk add --update --no-cache py3-numpy ENV PYTHONPATH=/usr/lib/python3.7/site-packages 
0
source

All Articles