Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Installing GDAL (Python 3.6) on Mac OS X

How-To: Install GDAL Python Bindings

I've found two ways to install the GDAL Python bindings on Mac.

Via GDAL Framework / QGIS

First, you can install the GDAL Framework via QGIS (or get it directly), and then do...

pip download GDAL
tar -xpzf tar -xpzf GDAL-2.3.2.tar.gz
cd GDAL-2.3.2
python setup.py build_ext -I/Library/Frameworks/GDAL.framework/Versions/2.3/Headers -L/Library/Frameworks/GDAL.framework/Versions/2.3/unix/lib --gdal-config /Library/Frameworks/GDAL.framework/Versions/2.3/unix/bin/gdal-config 
python setup.py build
python setup.py install

Via Homebrew

Alternatively, install GDAL via brew, then do similar to the above.

brew install gdal --HEAD
pip download GDAL
tar -xpzf tar -xpzf GDAL-2.3.2.tar.gz
cd GDAL-2.3.2
python setup.py build_ext --gdal-config /usr/local/Cellar/gdal/HEAD-41888_2/bin/gdal-config
python setup.py build
python setup.py install

Do an 'ls /usr/local/Cellar/gdal/' if you need to get the revision number.

Check

import osgeo
osgeo.gdal.VersionInfo()

Troubleshooting

I have found that sometimes only one version of Python can correctly import gdal without any dramas. For instance, 2.7 and 3.7 both give a "ModuleNotFoundError: No module named 'osgeo'" error, but 3.6 works.

Kelvins-MacBook-Pro-899:~ kelvin$ python3.6
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import gdal
>>> gdal.VersionInfo()
'2040100'
@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Oct 31, 2019

Where is setup.py?

@kelvinn

This comment has been minimized.

Copy link
Owner Author

@kelvinn kelvinn commented Nov 1, 2019

It should be in the GDAL-2.3.2 folder

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 1, 2019

Hmmm... for the Homebrew case, I'm not seeing it. But I'm using

brew install gdal

without the --HEAD flag since the latter fails on my machine (Catalina).

@kelvinn

This comment has been minimized.

Copy link
Owner Author

@kelvinn kelvinn commented Nov 2, 2019

I've updated the instructions. Give it another go.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 5, 2019

Yes, this worked for me! Thank you for taking the time to flesh out the instructions. Looks like you have a double call to tar -xpzf in both examples, just FYI. I also made sure to pip install GDAL==<gdal_version> where <gdal_version> is what Homebrew installs.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 5, 2019

Hmm. It appears that GDAL doesn't even support Homebrew officially. Their official website only suggests using conda.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 5, 2019

Update: I was having issues with the above on a different Mac, and I actually found that the following works:

brew install gdal
pip install GDAL==<gdal_version>

where <gdal_version> is what Homebrew installs, e.g. 2.4.2. Seems reasonable, but I never thought to try it. I guess I assumed it'd be more complex/painful...

python
>>> from osgeo import gdal
>>> gdal.VersionInfo()
'2040200'
@joaofig

This comment has been minimized.

Copy link

@joaofig joaofig commented Nov 8, 2019

Thank you for posting this last solution! It worked like a charm for me.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 8, 2019

Thanks, and thanks @kelvinn for posting this in the first place. I think installing via conda is probably more robust, but I didn't want to create a new environment just for GDAL. For such a great tool, I really wish the Python component of the Homebrew install worked "out-of-the-box."

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 16, 2019

Update: A command of the form gdal_merge.py -o out.tif in1.tif in2.tif does not work with the above. Ugh.

@kelvinn

This comment has been minimized.

Copy link
Owner Author

@kelvinn kelvinn commented Nov 16, 2019

Liam - what's the error? That looks to be doing raster stuff, which I'm less familiar with.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 17, 2019

Correct, I'm using GDAL for raster processing (satellite images, in my case). Unfortunately the GeoTIFFs I'm using are too large to share, but I'm pretty sure the below error could be reproduced with any two images. First, I install according to the above comment. Then attempting

gdal_merge.py -o out.tif 20190721_210144_1011_3B_AnalyticMS.tif 20190721_210145_1011_3B_AnalyticMS.tif

results in

0Traceback (most recent call last):
  File "/usr/local/bin/gdal_merge.py", line 172, in raster_copy_with_mask
    import numpy as Numeric
ModuleNotFoundError: No module named 'numpy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/gdal_merge.py", line 611, in <module>
    sys.exit(main())
  File "/usr/local/bin/gdal_merge.py", line 596, in main
    fi.copy_into(t_fh, band, band, nodata)
  File "/usr/local/bin/gdal_merge.py", line 343, in copy_into
    nodata_arg)
  File "/usr/local/bin/gdal_merge.py", line 124, in raster_copy
    m_band)
  File "/usr/local/bin/gdal_merge.py", line 174, in raster_copy_with_mask
    import Numeric
ModuleNotFoundError: No module named 'Numeric'

Evidently Python is still screwed up. I tried with a conda install and it worked fine.

@kelvinn

This comment has been minimized.

Copy link
Owner Author

@kelvinn kelvinn commented Nov 17, 2019

gdal_merge.py seems to need numpy, and this is not part of Python's standard library.

You should be able to install it with a 'pip install numpy' or even 'brew install numpy'.

Glad to hear Conda sorted it out for you.

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Nov 18, 2019

@kelvinn I noticed the NumPy ModuleNotFoundError and tried pip install numpy. Still couldn't find it, maybe paths are not taken care of properly. Perhaps I'll try brew install numpy instead.

But yes, conda may be the way to go here... a bit of a pain as I'm trying to install GDAL on a colleague's machine and was hoping to avoid doing a Miniconda install just for GDAL (she needs gdal_merge.py functionality).

@ResearchICTSolutions

This comment has been minimized.

Copy link

@ResearchICTSolutions ResearchICTSolutions commented Dec 6, 2019

I've got the same problem as @liamtoney: I can't use any functionality with .py. I checked for numpy and that is installed. GDAL commands without the .py link work (e.g. gdal_rasterize). Here is my version:
"gdalinfo --version
GDAL 2.4.2, released 2019/06/28"

When I run a .py command this is what I get:
Traceback (most recent call last):
File "/usr/local/bin/gdal_calc.py", line 54, in
from osgeo import gdal
ImportError: No module named osgeo

Any ideas on how to solve?

@ali-cs

This comment has been minimized.

Copy link

@ali-cs ali-cs commented Mar 19, 2020

I am facing this issue,

ocation.py", line 4, in
from osgeo import osr
Any idea to solve it ?

@CJohnsonLehi

This comment has been minimized.

Copy link

@CJohnsonLehi CJohnsonLehi commented Mar 19, 2020

None of this works for gdal 3.0

@kelvinn

This comment has been minimized.

Copy link
Owner Author

@kelvinn kelvinn commented Mar 19, 2020

@ali-cs - which method did you choose, and did the check part work?

@bentaber

This comment has been minimized.

Copy link

@bentaber bentaber commented May 17, 2020

Updated instructions above to use the osgeo tap which is currently at gdal 3.0.4 https://gist.github.com/bentaber/eb3252f151a33df0f598c6aae5635415

@liamtoney

This comment has been minimized.

Copy link

@liamtoney liamtoney commented Jul 8, 2020

I think (emphasis on the think) that now brew install gdal sets up Python correctly. GDAL 3.1.2: https://formulae.brew.sh/formula/gdal

gdal_merge.py shows the right message, didn't try it on data yet tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.