Setting up a GDAL in a Python virtual environment

As I remember from a few years ago, installing GDAL into a Python virtual environment was a terrible mess and I resorted to installing it using conda. It worked perfectly fine, but these days I much prefer pipenv to conda.

Turns out there is this great package called pygdal that solves the problem as follows.

I am on Ubuntu 18.04:

$ lsb_release -d
Description:    Ubuntu 18.04.3 LTS

First installing libgdal-dev and noting the version number.

$ apt show libgdal-dev
Package: libgdal-dev
Version: 2.2.3+dfsg-2
# blah blah
Depends: libgdal20 (= 2.2.3+dfsg-2), # and a whole bunch more
# blah blah
$ sudo apt-get install libgdal-dev

Once installed, verifying the gdal version.

$ gdal-config --version
2.2.3

And now, in my pipenv, trying to install the corresponding pygdal:

$ pipenv install pygdal==2.2.3

but that crashes with the error

CRITICAL:pipenv.patched.notpip._internal.index:Could not find a version that satisfies the requirement pygdal==2.2.3 (from versions: 1.8.1.0, 1.8.1.1, 1.8.1.2, 1.8.1.3, 1.8.1.5, 1.9.2.0, 1.9.2.1, 1.9.2.3, 1.9.2.5, 1.10.0.0, 1.10.0.1, 1.10.0.3, 1.10.0.5, 1.10.1.0, 1.10.1.1, 1.10.1.3, 1.10.1.5, 1.11.0.0, 1.11.0.1, 1.11.0.3, 1.11.0.5, 1.11.1.0, 1.11.1.1, 1.11.1.3, 1.11.1.5, 1.11.2.1, 1.11.2.3, 1.11.2.5, 1.11.3.3, 1.11.3.5, 1.11.4.3, 1.11.4.5, 1.11.5.3, 1.11.5.5, 2.0.0.3, 2.0.0.5, 2.0.1.3, 2.0.1.5, 2.0.2.3, 2.0.2.5, 2.0.3.3, 2.0.3.5, 2.1.0.3, 2.1.0.5, 2.1.1.3, 2.1.1.5, 2.1.2.3, 2.1.2.5, 2.1.3.3, 2.1.3.5, 2.2.0.3, 2.2.0.5, 2.2.1.3, 2.2.1.5, 2.2.2.3, 2.2.2.5, 2.2.3.3, 2.2.3.5, 2.2.4.3, 2.2.4.5, 2.3.0.4, 2.3.0.5, 2.3.1.4, 2.3.1.5, 2.3.2.4, 2.3.2.5, 2.4.0.4, 2.4.0.5, 2.4.1.4, 2.4.1.5, 2.4.2.5, 3.0.0.5, 3.0.1.5)

So I take the latest version

$ pipenv install pygdal==2.2.3.5

And that's it! Works like a charm.