Ubuntu/Dokku环境下Django/GeoDjango无法找到GDAL库的问题排查求助
Let's break down what's going wrong and how to fix it step by step:
1. First, locate the actual GDAL library path on Ubuntu
You mentioned /usr/local/lib doesn't have the GDAL files—this is normal because when you install via apt, the libraries go to system-wide paths instead. Run these commands to find where GDAL is installed:
Check the installed GDAL version:
gdal-config --versionSearch for the library files:
sudo find / -name "libgdal.so*"On Ubuntu 20.04, this will likely return something like
/usr/lib/x86_64-linux-gnu/libgdal.so.26(or a similar version suffix).Alternatively, use
ldconfigto list registered libraries:ldconfig -p | grep gdal
Once you have the correct path, update your Django settings.py with:
GDAL_LIBRARY_PATH = '/usr/lib/x86_64-linux-gnu/libgdal.so.26' # Use the path you found
2. The big issue: Dokku's containerized environment
Installing GDAL on your Ubuntu host doesn't help—Dokku runs your app in an isolated container, so the host's libraries aren't accessible. You need to make sure GDAL is installed inside the container during deployment.
Here are two reliable methods for this:
Method A: Use a custom Dockerfile
Create a Dockerfile in your project root with these contents (adjust Python version to match your app):
FROM python:3.9-slim # Install system dependencies for GDAL and PostgreSQL RUN apt-get update && apt-get install -y --no-install-recommends \ libpq-dev \ gdal-bin \ libgdal-dev \ && rm -rf /var/lib/apt/lists/* # Set environment variables for GDAL headers (required for installing the Python package) ENV CPLUS_INCLUDE_PATH=/usr/include/gdal ENV C_INCLUDE_PATH=/usr/include/gdal # Set work directory and copy requirements WORKDIR /app COPY requirements.txt . # Install Python dependencies (match GDAL version to system's installed version) RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the app COPY . . # Run migrations and start the server (adjust command as needed) CMD ["sh", "-c", "python manage.py migrate && gunicorn your_project.wsgi:application --bind 0.0.0.0:5000"]
Method B: Use Dokku with Apt Buildpack
This approach uses Heroku-style buildpacks to install system dependencies before Python packages:
- Create a
.buildpacksfile in your project root:https://github.com/heroku/heroku-buildpack-apt.git https://github.com/heroku/heroku-buildpack-python.git - Create an
Aptfilelisting the system packages to install:libpq-dev gdal-bin libgdal-dev - In your
requirements.txt, specify the exact GDAL version that matches the system's installed version (fromgdal-config --versionearlier). For Ubuntu 20.04, this is typicallyGDAL==3.0.4:# Other dependencies... GDAL==3.0.4
3. Common mistakes you might be making
- Version mismatch: If your Python GDAL package version doesn't match the system GDAL library version, Django will fail to find the library. Always align these versions.
- Forgetting container isolation: Installing GDAL on the host Ubuntu machine doesn't affect the Dokku container—you need to include the installation steps in your deployment workflow.
- Incorrect path: Double-check that
GDAL_LIBRARY_PATHpoints to the actual.sofile inside the container (not the host).
Give these steps a try, and you should be able to get GeoDjango running on Dokku without the GDAL error.
内容的提问来源于stack exchange,提问作者Chris Dixon

