Ubuntu – Ubuntu 20.04 ImportError: No module named gdal

20.04gispython-2.7

I am trying to use python2 bindings for gdal on Ubuntu 20.04 LTS, which does not have the python-gdal package in it's repository, to run an old python2 application.

I tried to download and install it from Ubuntu 18.04 LTS repository:

sudo wget -c http://archive.ubuntu.com/ubuntu/pool/universe/g/gdal/python-gdal_2.2.3+dfsg-2_amd64.deb

sudo apt install ./python-gdal_2.2.3+dfsg-2_amd64.deb

But got the error:

The following packages have unmet dependencies. python-gdal : Depends: gdal-abi-2-2-3 but it is not installable Depends: libgdal20 (>= 2.2.2) but it is not installable E: Unable to correct problems, you have held broken packages.

I also run sudo apt-get dist-upgrade and apt-cache policy python-gdal libgdal20 with output:

python-gdal: Installed: (none) Candidate: (none) Version table: libgdal20: Installed: (none) Candidate: (none) Version table:

Is there any way to use python-gdal without conflicts to run the app on Ubuntu 20?

Best Answer

Installing GDAL for Python 2 is possible, but we need to get many dependencies manually:

mkdir -p ~/Downloads/gdal
cd ~/Downloads/gdal

wget -c http://security.ubuntu.com/ubuntu/pool/main/j/json-c/libjson-c3_0.12.1-1.3ubuntu0.3_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/a/armadillo/libarmadillo8_8.400.0+dfsg-2_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/p/proj/libproj12_4.9.3-2_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/libg/libgeotiff-dfsg/libgeotiff2_1.4.2-2build1_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/h/hdf5/libhdf5-100_1.10.0-patch1+docs-4_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/m/minizip/libminizip1_1.1-8build1_amd64.deb
wget -c http://security.ubuntu.com/ubuntu/pool/main/m/mysql-5.7/libmysqlclient20_5.7.31-0ubuntu0.18.04.1_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/n/netcdf/libnetcdf13_4.6.0-2build1_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/ogdi-dfsg/libogdi3.2_3.2.0+ds-2_amd64.deb
wget -c http://security.ubuntu.com/ubuntu/pool/main/p/poppler/libpoppler73_0.62.0-2ubuntu2.10_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/q/qhull/libqhull7_2015.2-4_amd64.deb

wget -c http://archive.ubuntu.com/ubuntu/pool/universe/g/gdal/gdal-data_2.2.3+dfsg-2_all.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/g/gdal/libgdal20_2.2.3+dfsg-2_amd64.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/g/gdal/python-gdal_2.2.3+dfsg-2_amd64.deb

sudo apt install ./*.deb

And then pin GDAL package version with single long command below:

cat <<EOF | sudo tee /etc/apt/preferences.d/pin-gdal
Package: gdal-data
Pin: version 2.2.3+dfsg-2
Pin-Priority: 1337

Package: libgdal20
Pin: version 2.2.3+dfsg-2
Pin-Priority: 1337

Package: python-gdal
Pin: version 2.2.3+dfsg-2
Pin-Priority: 1337
EOF

Then it should work.