11

I am trying to install seaborn with this Dockerfile:

FROM alpine:latest

RUN apk add --update python py-pip python-dev 

RUN pip install seaborn

CMD python

The error I get is related to numpy and scipy (required by seaborn). It starts with:

/tmp/easy_install-nvj61E/numpy-1.11.1/setup.py:327: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates

and ends with

File "numpy/core/setup.py", line 654, in get_mathlib_info

RuntimeError: Broken toolchain: cannot link a simple C program

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-DZ4cXr/scipy/

The command '/bin/sh -c pip install seaborn' returned a non-zero code: 1

Any idea how I can fix this?

Community
  • 1
  • 1
e h
  • 8,435
  • 7
  • 40
  • 58

1 Answers1

24

To fix this error, you need to install gcc: apk add gcc.

But you will see that you will hit a new error as numpy, matplotlip and scipy have several dependencies. You need to also install gfortran, musl-dev, freetype-dev, etc.

Here is a Dockerfile based on you initial one that will install those dependencies as well as seaborn:

FROM alpine:latest

# install dependencies
# the lapack package is only in the community repository
RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk --update add --no-cache \ 
    lapack-dev \ 
    gcc \
    freetype-dev

RUN apk add python py-pip python-dev 

# Install dependencies
RUN apk add --no-cache --virtual .build-deps \
    gfortran \
    musl-dev \
    g++
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h

RUN pip install seaborn

# removing dependencies
RUN apk del .build-deps

CMD python

You'll notice that I'm removing the dependencies using apk-del .build-deps to limit the size of your image (http://www.sandtable.com/reduce-docker-image-sizes-using-alpine/).

Personally I also had to install ca-certificates but it seems you didn't have this issue.

Note: You could also build your image FROM the python:2.7-alpine image to avoid installing python and pip yourself.

Niels
  • 1,513
  • 3
  • 20
  • 29
Céline Aussourd
  • 10,214
  • 4
  • 32
  • 36
  • Fantastic, that worked. It took forever to build the container but finally it completed (weighs in at 693.3 MB). Thanks for a great answer (solved my problem and also taught me something about Docker best-practice). – e h Jul 25 '16 at 16:36
  • 1
    @emh You're welcome. I also have to build images containing scipy and I confirm they take ages to build from scratch. It's also difficult to keep them small. At least they are smaller than Ubuntu-based images! – Céline Aussourd Jul 25 '16 at 16:47
  • I couldn't find `lapack-dev` in the testing repository ATM (using `python:alpine` docker image alias for `python:3.6-alpine`), so I needed to use http://dl-4.alpinelinux.org/alpine/latest-stable/community to get the `lapack-dev` package – Shadi Jan 12 '17 at 09:00
  • @shadi You're right, the package moved to the community repository, I'll update the answer – Céline Aussourd Jan 12 '17 at 10:55
  • Recently `numpy` was updated and compilation fails. As written [here](https://gist.github.com/orenitamar/f29fb15db3b0d13178c1c4dd611adce2) you can do `pip install numpy==1.14.3`, but then beware that if you start installing also `pandas` then it will try to bring the latest version of `numpy` again. – Bulat Jun 12 '18 at 15:55