These are my instructions on how to install GDAL using Conda on a Mac. Before we dive in, let me explain why I am writing this guide. GDAL stands for the āGeospatial Data Abstraction Library**ā** and it is released by the Open Source Geospatial Foundation. For Python, the GDAL package is released with a package called **osgeo**
as well. And as it happens, I need both for a project Iām doing. And I want them neatly wrapped up inside a virtual environment.
One Does Not Simply pip install gdalĀ Source
I started by trying to use pip and virtual-env. This obscure how-to (and first post on Medium!) comes to you after hours of googling and trying to debug the errors I encountered along the way. In the end, I shifted gears, tried something new and switched over to Conda. This was the solution to all my problems. OK, except a few, but I fixed those too.
Conda is great because itās a package manager like pip, but it also manages your virtual environments like virtual-env does. Except it does both way better and itās a joy to use. Treat yo self and switch to Conda.
I wanted to spare you the trouble I experienced so I wrote up the following instructions.
A quick note about the environment Iām working in:
Mac OS 10.12.6 SierraMiniconda2 for Python 2 (Conda 4.3.30) info_Python 2.7.14_
optional python packages:Jupyter notebook, installs with
$ conda install jupyter
nb_conda, makes Jupyter play nice with Conda,$ conda install nb_conda
If you are working in a different development environment, your mileage may vary.
And now, the instructions that I myself needed hours agoā¦
This tutorial assumes you have Conda already installed and a Conda environment already created. Instructions here and here, respectively.
Open up Terminal, run this:
$ source activate [yourEnvironmentName]
For me, [yourEnvironmentName] = geoenv
.
Screenshot. Weāve gone from the global shell to the Conda local environment that was justĀ created.
(We can deactivate with the command $ source deactivate
.)
Still in Terminal, run this command:
$ conda install gdal
Hereās what I got:
Screenshot. Output from āconda installĀ gdalā
Great. As it turns out, for the osgeo subpackage to work, we actually need the dependency **jpeg version 8**
, rather than **9**
. You can read more about how I came to that conclusion towards the end of this post, under #Diagnosing.
For now, all you need to do is run this:
$ conda install -f jpeg=8
The ā-fā flag forces the install (which is really a downgrade of the the jpeg module).
Screenshot. Install jpeg version 8 withĀ Conda
OK, we should have a working version of GDAL now! Letās just test it to make sure.
You can do this in the command line or in a Jupyter notebook. Since I want to make sure gdal will work in Jupyter later, Iām going to test there.
To open a new Jupyter Notebook š, go back to Terminal, run this command:
$ jupyter notebook
This command will open up a new tab in your internet browser with the Jupyter Notebook file viewer. Navigate to the directory where you wish to save your notebook. Now, we want to start a new notebook. Go to the upper righthand corner, click āNewā.
Screenshot. Jupyter Notebook, creating a new notebook, I select āPython [conda env:geoenv]ā
Make sure to choose the Conda environment youāve been working with as the Python Kernel.
Letās go ahead and test! Run these commands in the notebook.
import gdalhelp(gdal)
Screenshot. Import gdal and get the help to make sure itĀ works!
The help for gdal works, so weāre off to the races.
import osgeohelp(osgeo)
Screenshot. Import osgeo and run help(osgeo) to make sure itĀ works!
Success! š¤
Weāve finally got GDAL installed as well as osgeo. Everything is working (for now). This somewhat lengthy post was a joy to write, as this problem caused me innumerable hours of strife. I hope to save you from the same. Thanks in advance for your claps šš½ Let me know if something needs an edit or clarification. With that, Iām off to explore graphs with **networkx**
!
With ā¤ļø,
Filip
p.s.
I generally like to use virtual environments on projects to keep things organized. First, I tried to install GDAL inside a python virtual-env which was a huge fail. There are instructions out there how to do that for Windows and Ubuntu, but I couldnāt get it to work for Mac. Virtual-env was more like virtual-enemy. Some folks on StackOverflow suggested to use Conda instead. I ran into a few snags anyways, so I decided to publish these instructions how to Install GDAL using Python/Conda on Mac. Dear reader, I hope this guide saves you some time.
I need GDAL for a very particular reason. Itās required for and a dependency of the **read_shp()**
function of the networkx Python module. That function reads in an ESRI shapefile (geospatial data) an converts it into a network/graph object. Obviously, you might need GDAL for something else.
To install, I tried using **pip install gdal**
inside a Python virtual environment (a.k.a. virtual-env) at first. That failed. I guess you could say it was only a pip dream, sigh. Or maybe it had something to do with having QGIS via Kyngchaos installed. That distribution includes GDAL not as a Python package, but as a Framework.
Anyways, the bottom line is that I still needed GDAL to work inside a Python virtual environment.
It was not possible for me to get GDAL installed inside a virtual-env using pip. Thatās why I switched to Conda.
When running the install in Conda, I ran into a few issues. Simply running the read_shp
function from **networkx**
was giving me a generic error, much like it was in virtual-env.
ImportError: read_shp requires OGR:
Screenshot. Jupyter notebook. What happens when I try to run the command: G = nx.read_shp(āfile.shpā)
In the screenshot you can see that the code requires **from osgeo import ogr**
which is actually included as part of the GDAL module.
So when we try to **import gdal**
, we can see whatās actually happening:
Library not loaded: @rpath/libjpeg.8.dylib
Error when I try to run import gdal. The library jpeg.8 isĀ missing.
The jpeg8 library is not loading. To investigate, we can check what packages conda has installed:**$ conda list**
Result of the conda list command. Sure enough, we have jpeg=9, rather thanĀ 8.
Moreover, when I uninstall and reinstall only **gdal**
, it actually becomes evident that gdal itself updates jpeg to version 9, only to break later.
GDAL breaks itself. Or rather, GDAL breaks osgeo which itās bundledĀ with(!)
The fix is to simply downgrade jpeg 9 to jpeg 8 after installing gdal. You can find the recipe for that in Step 3 of the #Instructions above. Thanks!