4

I need to install Shapely into Python+Alpine based docker container. What I already have inside the container:

make, gcc, geos-dev, musl-dev

What I got with this command: pip install shapely

Collecting shapely
  Downloading https://files.pythonhosted.org/packages/a2/fb/7a7af9ef7a35d16fa23b127abee272cfc483ca89029b73e92e93cdf36e6b/Shapely-1.6.4.post2.tar.gz (225kB)
     |████████████████████████████████| 235kB 641kB/s 
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vj51jvsy/shapely/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vj51jvsy/shapely/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-vj51jvsy/shapely/pip-egg-info
         cwd: /tmp/pip-install-vj51jvsy/shapely/
    Complete output (11 lines):
    Failed `CDLL(libgeos_c.so.1)`
    Failed `CDLL(libgeos_c.so)`
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-vj51jvsy/shapely/setup.py", line 80, in <module>
        from shapely._buildcfg import geos_version_string, geos_version, \
      File "/tmp/pip-install-vj51jvsy/shapely/shapely/_buildcfg.py", line 167, in <module>
        fallbacks=['libgeos_c.so.1', 'libgeos_c.so'])
      File "/tmp/pip-install-vj51jvsy/shapely/shapely/_buildcfg.py", line 161, in load_dll
        libname, fallbacks or []))
    OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so']
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I googled around much time but can't find any solving.

Dockerfile

FROM python:3.7-alpine

RUN ["mkdir", "/home/test"]
WORKDIR /home/test

RUN echo "http://mirror.leaseweb.com/alpine/edge/testing" >> /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN ["apk", "add", "make", "gcc", "geos-dev", "musl-dev"]
RUN ["pip3", "install", "virtualenv", "--no-warn-script-location", "--disable-pip-version-check"]
RUN ["virtualenv", ".env", "--python=python3"]
RUN ["pip3", "install", "shapely"]

CMD ["/bin/sh"]

Version of Python inside the container

/home/test # /usr/local/bin/python
Python 3.7.5 (default, Oct 21 2019, 20:13:45) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

GEOS version

/home/test # geos-config --version
3.7.2

UPDATED

Now you must put this repo beneath others:

--repository http://dl-cdn.alpinelinux.org/alpine/edge/community
lovesuper
  • 323
  • 3
  • 12

2 Answers2

3

This will work:

FROM python:3.7-alpine

RUN ["mkdir", "/home/test"]
WORKDIR /home/test

RUN echo "http://mirror.leaseweb.com/alpine/edge/community" >> /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk add --virtual .build-deps \
        --repository http://dl-cdn.alpinelinux.org/alpine/edge/community \
        --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
        gcc libc-dev geos-dev geos && \
    runDeps="$(scanelf --needed --nobanner --recursive /usr/local \
    | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
    | xargs -r apk info --installed \
    | sort -u)" && \
    apk add --virtual .rundeps $runDeps
RUN ["pip", "install", "shapely"]

CMD ["/bin/sh"]

see this

UPDATE: As per comments, geos-dev has moved to the community repo.

Adam Hopkins
  • 6,837
  • 6
  • 32
  • 52
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • Thank you. You are genius! You saved my time so much. – lovesuper Nov 05 '19 at 14:09
  • @lovesuper How is this working for you? It seems like the latest version of geos on the edge repository is not compatible with shapely. Our build stopped working on the same error you got. – Hien Nov 07 '19 at 23:18
  • What does runDeps="$(scanelf --needed --nobanner --recursive /usr/local \ | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \ | xargs -r apk info --installed \ | sort -u)" && \ is exactly mean? – varnothing Nov 22 '19 at 09:48
  • 1
    that will extract the required libs to install , the list is `expat gdbm libbz2 libcrypto1.1 libffi libnsl libssl1.1 libtirpc libuuid musl ncurses-libs readline sqlite-libs xz-libs zlib` – LinPy Nov 22 '19 at 10:12
  • geos-dev has been moved to community from testing repository. which also, didn't solve my problem. Had to switch to Debian image – varnothing Dec 03 '19 at 11:58
1

I hit this problem with my build as well. My workaround was to switch from Alpine to Debian (not ideal, but it allowed me to use an older version of geos), since it appears the latest version of geos is incompatible with shapely.

Acsmall
  • 11
  • 3