|

How To Install GDAL for Python with Anaconda

The Geospatial Data Abstraction Library (GDAL) is a fundamental package for spatial analysis with Python. GDAL is a powerful package with a lot of functionality. However, it is notoriously difficult to install. There are two reliable ways to install the GDAL python package: from the conda-forge channel using the conda installer or using pip to install a precompiled wheel. This guide will demonstrate how to successfully install GDAL from the conda-forge channel to an Anaconda (conda) environment. You can check out the video at the end of this article for a demonstration. Once you get GDAL installed, check out our tutorial to get started using GDAL with Python.

Open the Anaconda Prompt

Start by opening your Anaconda Prompt. If you’re using Mac or Linux this will probably be your regular terminal window. If using Windows, you’ll need to have the Anaconda distribution directories added to your path variable or open the Anaconda Prompt (recommended).

Create a Conda Environment

Now, make sure you’re installing GDAL to an environment. I’ve run into trouble installing GDAL to the base environment, but haven’t had issues when I’ve created a new conda environment.

To create an anaconda environment, enter the following code.

conda create --name pygdal

Here, pygdal is the name of the environment that was created. Now activate the environment using conda activate env-name. Like so.

conda activate pygdal

Install GDAL with conda install

Next comes the GDAL install. Make sure your pygdal environment has been activated. You should see (pygdal), or your environment’s name, to the left of the current working directory in the command prompt. At this point you’re ready to perform the GDAL installation.

GDAL can be installed using the conda install command. At the time of writing, GDAL is not available from the main install channel, so we’ll specify the conda-forge channel which contains a GDAL distribution. Type the following code to perform the install.

conda install -c conda-forge gdal

You should be given a prompt asking if you want to install/upgrade/downgrade the listed packages. Select the ‘yes’ option and GDAL will be installed in your environment. It is important to note that depending on the other modules you are using there could be discrepancies with GDAL’s requirements that prevent a successful install.

Make sure you specify the conda-forge channel using the -c flag. If you do not specify the channel and just use conda install gdal you will probably get something like the following.

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: |
Found conflicts! Looking for incompatible packages.

In my experience, it’s not worth trying to resolve these errors or resolve conflicts with any incompatible packages. If I ever get messages like this I either create a new environment to install GDAL or, if that doesn’t work, reinstall Anaconda.

Verify the Installation

Make sure you can import GDAL into an interactive session to confirm it has been appropriately installed. From the command prompt type python (or python3 if you also have a version of Python 2 on your machine). You will now see this symbol >>>, which indicates you have started the interactive session.

Your first instinct will be to type import gdal this will produce the following error.

>>>import gdal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gdal'

Not to worry. The GDAL distribution from conda-forge installs GDAL as part of the OSGEO library, which includes GDAL, OGR, and OSR. Just use from osgeo import gdal.

>>>from osgeo import gdal
>>>

No error message is given. GDAL has been successfully imported.

Check out our guide to getting started with GDAL to make the most of your new installation.

Want to learn more about GDAL?

I have a full-length course on using GDAL for raster analysis with Python. The course contains six hours of video instruction and is complete with code and example datasets so you can easily follow along and replicate the concepts that are taught. Check it out at geospatialschool.com!

Still Not Working?

Unfortunately, this method is not 100% foolproof. There are instances where the above method may not install GDAL correctly, or where GDAL may not work after being installed. Some of those instances include:

  • You have other modules installed that conflict with GDAL
  • You are using a python version that doesn’t support the current GDAL release
  • There is a problem with your conda environment
  • There is a problem with your Anaconda installation

The first thing I would try is to create a new conda environment and specify a python version. For example, the following code would create a conda environment with python version 3.8.

conda create -n pygdal38 python=3.8

If that doesn’t work there are few more things you can try to reconcile these problems. First, create a new environment and install GDAL before any other packages. Second, remove and reinstall Anaconda. Third, try to reconcile any dependency issues. Fourth, download a GDAL wheel file and install it with pip.

I’ve spent a lot of hours troubleshooting GDAL installation with Python and one of these solutions usually works for me. I hope this workflow helps pass on some of my hard-earned knowledge so that you spend more time writing code and less time pulling out your hair.

Whether you’re looking to take your GIS skills to the next level, or just getting started with GIS, we have a course for you! We’re constantly creating and curating more courses to help you improve your geospatial skills.

All of our courses are taught by industry professionals and include step-by-step video instruction so you don’t get lost in YouTube videos and blog posts, downloadable data so you can reproduce everything the instructor does, and code you can copy so you can avoid repetitive typing


My Recommended Equipment

Computer: Dell XPS

Mouse: Logitech M557 Bluetooth Mouse

External Hard Drive: Seagate Portable 2TB


This article contains affiliate links. When you click on links in this article Open Source Options may make a commission on any sales. This does not have any impact on the price you pay for products.

Similar Posts