You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu/Dokku环境下Django/GeoDjango无法找到GDAL库的问题排查求助

Fixing "Could not find the GDAL library" for GeoDjango on Ubuntu 20.04 with Dokku

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 --version
    
  • Search 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 ldconfig to 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:

  1. Create a .buildpacks file in your project root:
    https://github.com/heroku/heroku-buildpack-apt.git
    https://github.com/heroku/heroku-buildpack-python.git
    
  2. Create an Aptfile listing the system packages to install:
    libpq-dev
    gdal-bin
    libgdal-dev
    
  3. In your requirements.txt, specify the exact GDAL version that matches the system's installed version (from gdal-config --version earlier). For Ubuntu 20.04, this is typically GDAL==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_PATH points to the actual .so file 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:48:12