2

When I try to install gdal (on OSX 10.11.6) with pip install gdal (or easy_install) the installation fails with following error

extensions/gdal_wrap.cpp:3085:10: fatal error: 'cpl_port.h' file not found #include "cpl_port.h" 

According to this thread https://stackoverflow.com/questions/37700484/python-gdal-does-not-install-on-mac-osx-el-capitan I added the location of the header file to my path variable. My .bash_profile now looks like this

export PATH=$PATH:/Library/Frameworks/GDAL.framework/Programs export PATH=$PATH:/Library/Frameworks/GDAL.framework/Headers 

The error remains the same.

Then I tried following this solution: Python GDAL package missing header file when installing via pip although I do not want to install it in a virtualenv.

After setting CPLUS_INCLUDE_PATH and C_INCLUDE_PATH I ran into following error:

 clang++ -bundle -undefined dynamic_lookup -Wno-error=unused-command-line-argument-hard-error-in-future build/temp.macosx-10.11-x86_64-2.7/extensions/gdal_wrap.o -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -L/Library/Frameworks/GDAL.framework/Versions/2.1/lib -lgdal -o build/lib.macosx-10.11-x86_64-2.7/osgeo/_gdal.so ld: warning: directory not found for option '-L/Library/Frameworks/GDAL.framework/Versions/2.1/lib' ld: library not found for -lgdal clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'clang++' failed with exit status 1 

Any idea how to fix the errors and get a working installation?

7
  • 1
    If you use the GDAL KyngChaos version, the Python bindings are included in the GDAL Framework, look at GDAL install on Mac: pip doesn't see gdal.h Commented Mar 27, 2017 at 15:16
  • @gene thanks, I didn't notice that. Ultimately i want to use gdal2tiles.py which seems executable put gives me the error ImportError: No module named osgeo - any ideas? Commented Mar 27, 2017 at 16:07
  • Is the gdal-py2.7.pth file in /Library/Python/2.7/site-packages/gdal-py2.7.pth ? Commented Mar 27, 2017 at 16:09
  • @gene yes it is. Commented Mar 28, 2017 at 8:05
  • Therefore, you can import osgeo (ogr, gdal) with the Apple Python Commented Mar 29, 2017 at 17:48

7 Answers 7

2

You received the error because the header file was not included in your GDAL build. In order to get all of the required headers, you need to use the latest development branch of GDAL, as it added another header ~3 weeks ago. Set your CC and CXX environmental variables to clang and clang++, then install using Homebrew:

brew install gdal --HEAD 

If you try to build GDAL with gcc-8 instead, it will throw syntax errors. As of June 2018 this provides the development branch of GDAL 2.3.0. To install gdal for your existing Python (e.g., Anaconda Python 3.6) and link to GDAL, follow the method of @Holt, but with the below modifications.

$ cd /private/tmp $ pip download GDAL==2.3 $ tar xzf GDAL-2.3.0.tar.gz $ cd GDAL-2.3.0 

Now, set CC and CXX to gcc-8 and g++-8. Next, install gcc-8 with Homebrew if not currently installed: brew install gcc. Then, make the following change to the setup.cfg file:

gdal_config=/usr/local/bin/gdal-config 

gdal-config contains all of the information needed to link to include and library files. Next, make the following modifications to the setup.py file:

cxx11_flag='-std=c++11 -stdlib=libc++' os.environ['ARCHFLAGS'] = '' name='gdal' 

This sets ARCHFLAGS to blank rather than '-Wunused-command-line-argument-hard-error-in-future', as the latter is incompatible with gcc. Setting stdlib should help clang find the correct C++11 libraries, if used. Last, changing the name from GDAL to gdal lists the package name correctly under conda list. Finally, install the Python package correctly linked to GDAL 2.3.0:

python setup.py install 

As @Holt noted, be sure to load and test the install in another folder.

Rant: It's crazy that this much effort is needed to install gdal for Anaconda Python 3.6 linked to a centralized GDAL 2.3.0 installation, but that is because the conda-forge gdal folks intentionally broke the connection to Homebrew GDAL ~2 years ago in an effort to equally [not] support MacPorts. This is nuts in my opinion, as others also build from source and managing multiple GDAL installations is a pain. The GDAL package for R, rgdal, correctly links to Homebrew GDAL during installation without any hassle. The GDAL library for Anaconda Python should also do this!

1

The osgeo module is in

enter image description here

Therefore the content of the gdal-py2.7.pth should be

import sys; sys.path.insert(0,'/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages') 

Therefore:

from osgeo import gdal gdal.__file__ '/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages/osgeo/gdal.pyc' # or import gdal gdal.__file__ '/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages/osgeo/gdal.pyc' 
1

As of today (September 2017), the GDAL package only provides built-in support for python 2.7. Here is how you can install it for python 3+:

  1. Find the exact version of GDAL you are using:
$ ls /Library/Frameworks/GDAL.framework/Versions 2.1 Current 
  1. Download (not install!) the GDAL package using pip (>= 9.0.01) — it is important to download the exact same version of the python GDAL package as the one you have installed, otherwise this will not work:
$ cd /tmp $ pip3 download GDAL==2.1 
  1. Decompress the downloaded archive and go into the corresponding folder
$ tar xzf GDAL-2.1.0.tar.gz $ cd GDAL-2.1.0 
  1. Install GDAL:
$ python3 setup.py install \ --include-dirs=/Library/Frameworks/GDAL.framework/Versions/2.1/unix/include \ --library-dirs=/Library/Frameworks/GDAL.framework/Versions/2.1/unix/lib 

If python3 is your default, replace pip3 and python3 by pip and python. Do not forget to replace 2.1.0 and 2.1 in the above commands by your GDAL version.

Once you are done, do not test the python installation in the GDAL installation directory, you would get horrible errors. Go somewhere else and try to import a GDAL package:

$ cd /tmp $ python3 >>> import osgeo >>> osgeo.version_info sys.version_info(major=3, minor=5, micro=4, releaselevel='final', serial=0) 

1 If you are using pip < 9.0.0, you can see this answer for a way to replace the given commands.

5
  • 1
    Hi there, I failed to get this to run using pip 10.0.1 and python 3.6.5. The command line didn't recognize --include-dirs as an option. When I tried to run it without the two optional arguments, I also ran into an issue where my c++ was out of date. I'm currently running High Sierra Commented Jun 11, 2018 at 0:44
  • @brianbancroft Which version of GDAL are you using? Commented Jun 11, 2018 at 7:53
  • @brianbancroft What happens if you run python3 setup.py build_ext ... with include-dirs and library-dirs? If it works, you can then python3 setup.py install (without the extra arguments). Commented Jun 11, 2018 at 19:35
  • @brianbancroft You can also try python3 setup.py build_ext --gdal-config=Path where Path is the path to the gdal-config binary (somewhere on your system). Commented Jun 11, 2018 at 19:36
  • @Holt That does not work: extensions/gdal_wrap.cpp:3173:10: fatal error: 'cpl_vsi_error.h' file not found #include "cpl_vsi_error.h" Commented Jun 26, 2018 at 2:24
1

If you landed here trying to install geopandas, what worked for me on a 10.13 mac is:

python3.7 -m pip install cython python3.7 -m pip install git+https://github.com/jswhit/pyproj.git 

brew went ahead and tried to install a whole slew of dependencies, while what I needed was just pyproj to install geopandas.

1

I made a tutorial https://github.com/felipunky/GISPython scroll down to the Mac section.

An extract:

1.) Download and install python 2.7.

2.) Go to terminal, you can do so by pressing at the same time the Command and Space keys, than type terminal.

3.) Go to http://www.kyngchaos.com/software/frameworks/ and download and install the GDAL 1.11 version.

4.) Now open the file explorer and press the Command, Shift and G key at the same time, this should enable you to search a directory. Type /Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages and press enter. We need these files to be on our python site-packages folders. So copy all the files and folders.

5.) Repeat the Command, Shift and G press to go to another directory, this time is our python's directory which is at: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ once you are inside the directory paste all the files you have already copied from step 4.).

6.) Check if we have succesfully installed the library: go to Terminal and type: python than press enter, now type: import gdal if there are no errors we can continue with step 7.).

7.) Install homebrew: Here is a detailed guide on how to install homebrew: http://osxdaily.com/2018/03/07/how-install-homebrew-mac-os/

8.) Go to Terminal and type:

brew install matplotlib

brew install numpy

brew install geos

brew install proj

9.) Test by typing python on Terminal to access the python console. Then type:

import gdal

If there are no errors you are good to go.

1
  • Updated with the extract from the link. Commented Nov 27, 2018 at 1:59
0

Try this:

brew install gdal brew upgrade gdal pip install gdal 
-2

Use Anaconda: https://www.anaconda.com

Open up Terminal: conda install gdal

2
  • Anaconda's gdal package almost never works on MacOS. Commented Jun 26, 2018 at 2:19
  • This answer is incomplete but worked fine for me. I installed Anaconda, created my new conda environment using Pycharm and successfully installed GDAL 2.3.3. Commented Jan 3, 2019 at 2:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.