Search code examples
dockerdockerfilegdal

Install gdal in a docker python image


I'm trying to install gdal in a python 3.10.8 docker image from Cloud Build in Google Cloud but I cannot find any working solution so far. I have tried different approaches from the internet but I always receive the same error:

Step #0: Step 5/9 : ENV PATH $PATH:/usr/bin
Step #0:  ---> Running in 7b48f619d4a1
Step #0: Removing intermediate container 7b48f619d4a1
Step #0:  ---> b4313bd792ac
Step #0: Step 6/9 : ENV PATH $PATH:/usr/share/lintian/overrides/gdal-bin
Step #0:  ---> Running in 1745e30e8a86
Step #0: Removing intermediate container 1745e30e8a86
Step #0:  ---> 1f7ec2e34cd8
Step #0: Step 7/9 : ARG CPLUS_INCLUDE_PATH=/usr/include/gdal
Step #0:  ---> Running in 4d3ccf1e9efa
Step #0: Removing intermediate container 4d3ccf1e9efa
Step #0:  ---> 787d1acef1f3
Step #0: Step 8/9 : ARG C_INCLUDE_PATH=/usr/include/gdal
Step #0:  ---> Running in 00340d417631
Step #0: Removing intermediate container 00340d417631
Step #0:  ---> 1922c706cabe
Step #0: Step 9/9 : RUN pip install GDAL
Step #0:  ---> Running in 71a5e16d25d4
Step #0: Collecting GDAL
Step #0:   Downloading GDAL-3.7.0.tar.gz (775 kB)
Step #0:      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 775.5/775.5 kB 40.0 MB/s eta 0:00:00
Step #0:   Preparing metadata (setup.py): started
Step #0:   Preparing metadata (setup.py): finished with status 'error'
Step #0:   error: subprocess-exited-with-error
Step #0:   
Step #0:   × python setup.py egg_info did not run successfully.
Step #0:   │ exit code: 1
Step #0:   ╰─> [84 lines of output]
Step #0:       WARNING: numpy not available!  Array support will not be enabled
Step #0:       running egg_info
Step #0:       creating /tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info
Step #0:       writing /tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info/PKG-INFO
Step #0:       writing dependency_links to /tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info/dependency_links.txt
Step #0:       writing requirements to /tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info/requires.txt
Step #0:       writing top-level names to /tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info/top_level.txt
Step #0:       writing manifest file '/tmp/pip-pip-egg-info-5k_eohyx/GDAL.egg-info/SOURCES.txt'
Step #0:       Traceback (most recent call last):
Step #0:         File "/tmp/pip-install-2kmkdthm/gdal_7fd12a6a1cfc4f828e9ac25e1118c961/setup.py", line 87, in fetch_config
Step #0:           p = subprocess.Popen([command, args], stdout=subprocess.PIPE)
Step #0:         File "/usr/local/lib/python3.10/subprocess.py", line 971, in __init__
Step #0:           self._execute_child(args, executable, preexec_fn, close_fds,
Step #0:         File "/usr/local/lib/python3.10/subprocess.py", line 1847, in _execute_child
Step #0:           raise child_exception_type(errno_num, err_msg, err_filename)
Step #0:       NotADirectoryError: [Errno 20] Not a directory: 'gdal-config'

The dockerfile that I'm using is as follows:

FROM python:3.10.8-buster

#update pip
RUN pip install --upgrade pip

RUN apt-get update &&\
    apt-get install -y binutils libproj-dev gdal-bin

ENV PATH $PATH:/usr/bin
ENV PATH $PATH:/usr/share/lintian/overrides/gdal-bin

ARG CPLUS_INCLUDE_PATH=/usr/include/gdal
ARG C_INCLUDE_PATH=/usr/include/gdal
RUN pip install GDAL

I tried to set up the PATH variable by myself but unfortunately I was unsuccessful.

Can someone point me in the right direction? Do I need to set some variable right or maybe install something more? Why is it asking for the gdal-config all the time? Shouldn't it look for the gdal-bin?


Solution

  • The immediate problem here is that you need the gdal-dev package if you expect to compile code that uses GDAL:

    apt -y install libgdal-dev
    

    However, after installing that you're going to run into additional problems, such as this one. The GDAL package in the python:3.10.8-buster image is only 2.4.0, and you need at least 3.6.0, or you need to install an older version of the python GDAL package.


    You can alleviate this issue by using a more recent Python image, and making sure you install the Python gdal package corresponding to the version of gdal installed via apt:

    $ docker run -it --rm python:3.11 bash
    root@47dd9a0e2dc1:/# apt update
    [...]
    root@47dd9a0e2dc1:/# apt -y install libgdal-dev
    [...]
    root@47dd9a0e2dc1:/# pip install gdal==3.6.0
    [...]
    Installing collected packages: gdal
    Successfully installed gdal-3.6.0