GDAL Installation for windows [Part 2]

Mostafa Farrag
Towards Dev
Published in
5 min readSep 28, 2022

--

In this article, we will go through installing GDAL for python on Windows

GDAL is partially written in C, C++ and python the reason why it requires some binaries to be installed and accessible in your environment variables

In this article, we will go through installing all of these binaries, adding the binaries to windows environment variables and different options for installing a specific version of gdal.

Python

  • Install the python version you want to work with, and for this article, we will be using python 3.9.13, you can download python from the python.org website here https://www.python.org/downloads/
  • you have to make note of which python version you are using 64-bit or 32-bit

MSVC (Microsoft Visual C++)

MSVC is a compiler for C and C++ by Microsoft, MSVC used to be a stand-alone product but later becomes part of Visual Studio [1], MSVC is a requirement for gdal, as it requires runtime libraries that are installed by MSVC to function correctly

You can install MSVC using Microsoft Visual studio installer (https://visualstudio.microsoft.com/) if you have it already, you can directly install the version of msvc you want

Figure-1 Visual Studio
  • Since I have MSVC 2022 installed on my computer (to check open your Microsoft visual studio installer and press modify, you will end up in the same view as Figure-2)
Figure-2 Microsoft visual studio installer

Downloading windows binaries and python bindings

  • You have to download the compatible windows binaries to both, the python version and the msvc runtime packages you have installed on your computer (from the above steps we have msvc 2019 and python 3.9.* X64)
  • another aspect you have to consider before you choose the binaries is the gdal version, if you need to install gdal because it is a dependency of another package you are using like Fiona https://pypi.org/project/Fiona/, you have to check which version this package (Fiona) requires. In my case, I need to install gdal 3.4.3 as it is a requirement of a Fiona 1.8.21
  • Now we know MSVC is 2022, python is 3.9.13 x64
  • To download the binaries go to https://www.gisinternals.com/release.php, and you will find the latest available version of gdal (in our case 3.5.2 is the latest available version at the time of writing this article).
Figure-3 GDAL binaries for version 3.5.2
Figure-3 GDAL binaries for version 3.4.3-msvc2022-x64
  • The above link in Figure 3 will take you to the list of MSVC 2022 — x64 binaries for different python versions, in our case we have python 3.9
Figure-4 GDAL version 3.4.3 for win x64 python 3.9 that requires msvc 2022 and gdal core component
  • Download the python bindings (GDAL-3.4.3.win-amd64-py3.9.msi) and the binaries-core installer (gdal-304–1930-x64-core.msi) which has most of the GDAL components.

GDAL Binaries

  • After downloading the binaries-core installer (gdal-304–1930-x64-core.msi), install it (if you changed the default installation directory you have to keep in mind the new installation directory)
  • The default installation directory will be in the C Drive in the following path C:\Program Files\GDAL, the installation directory will look like Figure 5
Figure-5 The Installation directory for GDAL binaries.

Adding the binaries to windows environment variables

In order to make the gdal binaries reachable from anywhere, you have to add them to your environment variable.

  • First, add the overall installation directory of the binaries to the Path environment variable
Figure-6 adding gdal installation directory to the Path environment variable.
  • Then create another environment variable and call it “GDAL_DATA”, and in the variable value add the path to the gda_data folder in Figure-5, then it will look like Figure-7.
Figure-7 GDAL_DATA environment variable
  • After that create another variable and give it the name “GDAL_DRIVER_PATH” and in the value field, add the path to the gdalplugins folder in Figure-5 then it will look like Figure-8
Figure-8 GDAL_DRIVER_PATH environment variable
  • with that, you have installed all the binaries and made them accessible to any python environment installed on your computer.
  • the next step is to install the python bindings that connect these gdal binaries and make a usable as python functions (API)

Python Bindings

  • After downloading the python bindings (GDAL-3.4.3.win-amd64-py3.9.msi), install it anywhere on your computer but keep in mind the installation directory, as we have to move them inside the python environment.
  • Installing the binding file will look like Figure-9
Figure-9 The binding default installation directory
  • where the scripts and Lib folders will look like Figure-10
Figure-10 gdal python bindings
  • Now we need to move these bindings to our python environment which will have the same folder structure as the installation directory of the bindings, so just paste the bindings into the root directory of the python environment as in Figure-11.
Figure-11 Python environment directory created using virtualenv
  • Hint: The python bindings can also be installed using pip

pip install gdal==3.4.3

Check the installation

  • to make sure everything is installed correctly open the python console and import gdal, osr, or ogr (as in Figure-12) which are the main components of the OSGeo package and we will have a whole separate article about each one of them.

from osgeo import gdal

Figure-12

--

--