[GIS] Install legacy versions of GDAL with Homebrew

gdalhomebrewinstallationpygdal

I have installed, on a MacOSX, GDAL2 from homebrew with:

brew tap osgeo/osgeo4mac && brew tap --repair
brew install gdal2 --with-complete

And then the python binding:

brew install gdal2-python

When I install pygdal:

pip3 install pygdal

I get this error:

Failed building wheel for pygdal
Running setup.py clean for pygdal
Failed to build pygdal
Installing collected packages: pygdal
Running setup.py install for pygdal ... error
Complete output from command /usr/local/opt/python3/bin/python3.6 -u -c  "import setuptools, tokenize;__file__='/private/var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-build-7ac3hl4r/pygdal/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-5n25357a-record/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build/lib.macosx-10.13-x86_64-3.6
creating build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/gnm.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/__init__.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/gdalnumeric.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/osr.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/gdal.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/ogr.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/gdal_array.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
copying osgeo/gdalconst.py -> build/lib.macosx-10.13-x86_64-3.6/osgeo
running build_ext
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-build-7ac3hl4r/pygdal/setup.py", line 173, in <module>
    cmdclass=dict(build_ext=gdal_ext),
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/site-packages/setuptools/command/install.py", line 61, in run
    return orig.install.run(self)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/command/install.py", line 545, in run
    self.run_command('build')
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/private/var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-build-7ac3hl4r/pygdal/setup.py", line 41, in run
    inst_gdal_version, GDAL_VERSION))
  __main__.GDALConfigError: Version mismatch 2.2.3 != 2.2.1

 ----------------------------------------
Command "/usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-build-7ac3hl4r/pygdal/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-5n25357a-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/zp/dgcny8354972qt2x7s229pnw0000gn/T/pip-build-7ac3hl4r/pygdal/

From its git page, I understand pygdal is only compatible up to GDAL2.2.1 but homebrew installs version 2.2.3.

I have been looking at this tutorial for installing legacy versions but:

brew search gdal2

returns:

osgeo/osgeo4mac/gdal2            
osgeo/osgeo4mac/gdal2-mysql      
osgeo/osgeo4mac/gdal2-sosi
osgeo/osgeo4mac/gdal2-ecwjp2     
osgeo/osgeo4mac/gdal2-ogdi       
osgeo/osgeo4mac/liblas-gdal2
osgeo/osgeo4mac/gdal2-filegdb    
osgeo/osgeo4mac/gdal2-oracle     
osgeo/osgeo4mac/pdfium-gdal2
osgeo/osgeo4mac/gdal2-grass7     
osgeo/osgeo4mac/gdal2-pdf
osgeo/osgeo4mac/gdal2-mrsid      
osgeo/osgeo4mac/gdal2-python

with no sign of legacy versions. Does it just mean that they are not available?

How can I install a legacy version of GDAL with homebrew?

Best Answer

I found a way by using brew edit and modifying the GDAL formula to match and old one from the git repository. Make sure you set the default text editor:

echo "export EDITOR=\"/Applications/TextEdit.app/Contents/MacOS/TextEdit\"" >> ~/.bashrc

Then:

brew edit gdal2

In the editor, I replaced line 4 and 5 with:

url "http://download.osgeo.org/gdal/2.2.1/gdal-2.2.1.tar.gz"
sha256 "61837706abfa3e493f3550236efc2c14bd6b24650232f9107db50a944abf8b2f"
revision 3

Basically, I replaced line 4 to 6 of the current formula with an old commit as you can see here.

After that, I installed as usual:

brew install gdal2
brew link -force gdal2

Make sure that brew doesn't upgrade to the new version:

brew pin gdal2

Finally, in my Python3 virtual environment, I was able to install pygdal:

pip3 install pygdal