1

I am planning to build NumPy on Alpine Linux container. To do so I will first install the following packages.

apk add --no-cache --virtual build-dependencies musl-dev linux-headers g++

My question is, does NumPy rely on these packages (i.e. musl-dev linux-headers g++) to run or is it just for the build, can I just delete them after build? ( i.e. apk del build-dependencies ) Likewise same question for pandas which needs more of these build dependencies.

jadeidev
  • 194
  • 3
  • 13
  • 1
    If you build libraries statically, you no longer need the build requirements after the build has completed successfully. This is not the case with dynamic libs. – MattDMo Dec 28 '21 at 21:37
  • How do i know if a lib is dynamic or not – jadeidev Dec 28 '21 at 21:48
  • Static libraries on Linux end with `.a`, while dynamic libraries end with `.so`. – MattDMo Dec 28 '21 at 21:51
  • Thank you for all your help, do you know how to get this information in Linux Alpine – jadeidev Dec 28 '21 at 22:10
  • There are `manylinux` wheels for NumPy [available from PyPI](https://pypi.org/project/numpy/#files). See https://stackoverflow.com/q/32923952/1426065 for info on unzipping wheels and seeing what files are contained within them. You may need to unzip the wheel and then unzip the `.egg` file inside, if it contains one. – MattDMo Dec 28 '21 at 22:15
  • I would assume that the `manylinux` wheels have statically-linked libraries in them. – MattDMo Dec 28 '21 at 22:15

1 Answers1

2

No.

Building numpy from source numpy-1.21.5.zip in alpine:

apk add python3 python3-dev cython py3-setuptools gcc gfortran g++
unzip numpy-1.21.5.zip
cd numpy-1.21.5
python3 setup.py build
python3 setup.py install

After this, the only runtime requirements are of course python and libc.musl-x86_64.so.1 which is part of musl. And it is a base package already required by python3.

~ # find /usr/lib/python3.9/site-packages/numpy-1.21.5-py3.9-linux-x86_64.egg/numpy/ -name "*.so" -print -exec sh -c 'readelf -d {} | grep NEEDED' \;
/usr/lib/python3.9/site-packages/numpy-1.21.5-py3.9-linux-x86_64.egg/numpy/core/_multiarray_tests.cpython-39-x86_64-linux-musl.so
 0x0000000000000001 (NEEDED)             Shared library: [libc.musl-x86_64.so.1]
/usr/lib/python3.9/site-packages/numpy-1.21.5-py3.9-linux-x86_64.egg/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-musl.so
 0x0000000000000001 (NEEDED)             Shared library: [libc.musl-x86_64.so.1]
...

Edit: In order to make numpy faster, you need openblas (and openblas-dev at build time).

apk add openblas-dev

At runtime, numpy shared libraries will link to libopenblas.so.3 (part of openblas)

~ # readelf -d /usr/lib/python3.9/site-packages/numpy-1.21.5-py3.9-linux-x86_64.egg/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-musl.so

Dynamic section at offset 0x40bb40 contains 20 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libopenblas.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libc.musl-x86_64.so.1]
...
Balaïtous
  • 826
  • 6
  • 9