Skip to content

Instantly share code, notes, and snippets.

@kelvinn
Last active July 28, 2024 14:08
Show Gist options
  • Save kelvinn/f14f0fc24445a7994368f984c3e37724 to your computer and use it in GitHub Desktop.
Save kelvinn/f14f0fc24445a7994368f984c3e37724 to your computer and use it in GitHub Desktop.
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'
@ResearchICTSolutions
Copy link

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
Copy link

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
Copy link

None of this works for gdal 3.0

@kelvinn
Copy link
Author

kelvinn commented Mar 19, 2020

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

@bentaber
Copy link

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

@liamtoney
Copy link

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.

@zhongyunshun
Copy link

Got an error of "tar: Can't specify both -z and -z" when I run tar -xpzf tar -xpzf GDAL-2.3.2.tar.gz in the first way. How can I solve it?

@Faz95210
Copy link

@zhongyunshun Just remove one tar -xpzf

@kikiyang
Copy link

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'

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'

This works for me too!

@bobleujr
Copy link

👍 to the above solution

brew install gdal
pip install GDAL==<gdal_version>

@mairuian
Copy link

mairuian commented Jun 1, 2023

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'

thank you, it is still work on MacOS Monterey 12.6.6

@bextla20
Copy link

Hi, am trying install gdal in my macbook, but faced error. Also osgeo has same issue

@LoganParker
Copy link

LoganParker commented Apr 22, 2024

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'

thank you, it is still work on MacOS Monterey 12.6.6

Worked for me on Sonoma 14.4.1 with gdal version 3.8.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment