1

I would like to install GDAL and Python bindings on Ubuntu 22.04 without using special PPAs or special Pip wheel files or Conda packages.

I went through a few iterations on a recent Ubuntu installation and so far I have this:

# Ubuntu stuff:
# - for `libgdal-dev` see also `ls -lah /usr/include/gdal/`
# - check what GDAL version you installed: `gdal-config --version` 
#   --> my version is 3.4.1
sudo apt-get install -y libgdal-dev gdal-bin
# Pip stuff
export CPLUS_INCLUDE_PATH=/usr/include/gdal && \
    export C_INCLUDE_PATH=/usr/include/gdal && \
    pip install gdal==$(gdal-config --version)

In my understanding it's important to:

  • match the libgdal-dev version
  • with the Pip GDAL version.

Now, when invoking python (this comes from a clean Conda environment - I would like to avoid conda blobs for GDAL) and attempting at importing GDAL I see this dynamic linking issue:

$ python
Python 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> from osgeo import gdal
Traceback (most recent call last):
  File "/home/<MY_USER>/conda/envs/mypy310/lib/python3.10/site-packages/osgeo/__init__.py", line 29, in swig_import_helper
    return importlib.import_module(mname)
  File "/home/<MY_USER>/conda/envs/mypy310/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 674, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1176, in create_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ImportError: /home/<MY_USER>/conda/envs/mypy310/lib/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /lib/libgdal.so.30)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<MY_USER>/conda/envs/mypy310/lib/python3.10/site-packages/osgeo/__init__.py", line 45, in <module>
    _gdal = swig_import_helper()
  File "/home/<MY_USER>/conda/envs/mypy310/lib/python3.10/site-packages/osgeo/__init__.py", line 42, in swig_import_helper
    return importlib.import_module('_gdal')
  File "/home/<MY_USER>/conda/envs/mypy310/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_gdal'
>>> 
>>> 

For what is worth this Pip alternative also did not help:

pip install \
    --global-option=build_ext \
    --global-option="-I/usr/include/gdal" \
    GDAL==`gdal-config --version`

I checked the site-packages stuff here /home/<MY_USER>/conda/envs/mypy310/lib/python3.10/site-packages:

$ ls -lah | grep osgeo
drwxr-xr-x  3 <MY_USER> docker 4,0K giu 19 11:25 osgeo
drwxr-xr-x  5 <MY_USER> docker 4,0K giu 19 11:25 osgeo_utils
$ ls osgeo/
_gdal_array.cpython-310-x86_64-linux-gnu.so  _gdal.cpython-310-x86_64-linux-gnu.so  gnm.py                                _osr.cpython-310-x86_64-linux-gnu.so
gdal_array.py                                gdalnumeric.py                         __init__.py                           osr.py
_gdalconst.cpython-310-x86_64-linux-gnu.so   gdal.py                                _ogr.cpython-310-x86_64-linux-gnu.so  __pycache__
gdalconst.py                                 _gnm.cpython-310-x86_64-linux-gnu.so   ogr.py                                utils.py

I can see in here a file gdal.py so by looking at the Python error message I tend to think I should attempt at modyfing the file osgeo/__init__.py to import gdal.py instead of _gdal, but here I stop as this looks dangerous.

  • What is the clean / standard way of install GDAL + Python bindings on a normal Linux distro?
  • As an alternative is there an OFFICIAL GDAL wheel I can install with Pip that includes all those links to native libs etc?
9
  • Why don't you just install the GDAL Python package from the Ubuntu repo? sudo apt-get install -y gdal-bin python3-gdal
    – user2856
    Jun 19, 2023 at 10:17
  • I don't think this is possible (unless you compile from source) without adding http://ppa.launchpad.net/ubuntugis/ppa/ubuntu as a PPA - with that PPA gdal and python just works as @user2856 says.
    – Ian Turton
    Jun 19, 2023 at 10:20
  • Don't try to mix conda python and apt packages. Either use a conda environment and conda gdal packages or use the system package manager and the standard apt (deb) packages.
    – user2856
    Jun 19, 2023 at 10:25
  • @IanTurton the ubuntugis PPA is not required, Ubuntu provides GDAL packages
    – user2856
    Jun 19, 2023 at 10:28
  • But then QGis doesn't work I think or you end up with an older one I forget why I had to add ubuntugis
    – Ian Turton
    Jun 19, 2023 at 10:33

1 Answer 1

1

Unless you require a newer version of GDAL than is provided by the official Ubuntu 22.04 repositories, installing the GDAL utilities and the python bindings is as simple as:

sudo apt install gdal-bin python3-gdal

No unofficial PPAs or conda environments required.

This will get you GDAL 3.4.1:

$ python -c "from osgeo import gdal; print(gdal.__version__)"
3.4.1
$ gdalinfo --version
GDAL 3.4.1, released 2021/12/27
6
  • Is there a way to install the GDAL binding via Pip using either a "fat wheel" (including the blobs), or "thin" Pip package but specifying the pointers to the libs/blob installed with Apt? I would like to use an alternative Python environment controlled by me e.g. Conda, without using the Conda packages
    – TPPZ
    Jun 19, 2023 at 17:05
  • Install via apt and set up a venv,
    – user2856
    Jun 19, 2023 at 21:06
  • There's this need to change Python versions and Python dependencies at the same time (think at setup files combined with requirements files). Having the chance to install once with Pip a multitude of dependencies while using a variety of Python versions would be great. OK I take there are no "fat wheels" for Pip ready made for Linux (Ubuntu). Thanks!
    – TPPZ
    Jun 19, 2023 at 21:26
  • Your question specifically states "without using special PPAs or special Pip wheel files etc...". You can create environments with different python versions using virtualenv and you can install gdal from a manylinux wheel via pip but then you have the same issue of installing gdal from some random internet package. Suggest you look at rasterio, it's built on GDAL and is easily pip installable.
    – user2856
    Jun 19, 2023 at 21:36
  • The thing is that I am working on some projects/libraries that rely on GDAL and other dependencies. I am trying to find a way that minimizes the amount of manual steps in favor of things like setup.py and requirements.txt to produce an artefact that could be installed with Pip. I would like to avoid artefact from unofficial Pip sources and I would like to retain the flexibility of conda/virtual environments including specific Python versions. Thanks for the suggested link.
    – TPPZ
    Jun 20, 2023 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.