Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To configure GDAL for Django on a Mac, follow these steps:

  1. Install Homebrew package manager if you haven't already. Open Terminal.app and execute the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  1. Install GDAL library using Homebrew by running the following command in Terminal:

install gdal

  1. Configure GDAL path in Django settings.py file. Open Terminal and navigate to your Django project folder. Open settings.py file and add the following code:
import os

GDAL_LIBRARY_PATH = os.environ.get('GDAL_LIBRARY_PATH')
if GDAL_LIBRARY_PATH is not None:
    try:
        # Add GDAL library path to the system path
        os.environ['PATH'] = GDAL_LIBRARY_PATH + ':' + os.environ['PATH']
        # Add GDAL library path to the library search path
        os.environ['DYLD_LIBRARY_PATH'] = GDAL_LIBRARY_PATH + ':' + os.environ['DYLD_LIBRARY_PATH']
        # Add GDAL library path to the loader search path
        os.environ['LD_LIBRARY_PATH'] = GDAL_LIBRARY_PATH + ':' + os.environ['LD_LIBRARY_PATH']
        # Verify if GDAL library is accessible
        from django.contrib.gis import gdal
    except:
        pass
  1. Export GDALLIBRARYPATH environment variable by running the following command in Terminal, replacing /usr/local/Cellar/gdal/3.3.0_1/lib with the path where GDAL library was installed:

GDAL_LIBRARY_PATH=/usr/local/Cellar/gdal/3.3.0_1/lib

  1. Restart your Django server and verify that GDAL library is accessible by running the following command in Python shell:

django.contrib.gis import gdal

If there are no errors, then GDAL is successfully configured for Django on your Mac.