GCP Ubuntu环境Docker部署Django项目遇gdal-config缺失错误求助
gdal-config找不到问题 针对你在GCP Ubuntu 20.04.5 LTS环境下,Docker部署Django 3.2 REST API时遇到的gdal_config_error: [Errno 2] No such file or directory: 'gdal-config'问题,给出以下几个可行的解决方向:
1. 确保容器内libgdal-dev完整安装且环境变量配置正确
Ubuntu 20.04中gdal-config默认路径为/usr/bin/gdal-config,首先要确认Dockerfile中libgdal-dev安装过程无异常:
- 安装前必须更新apt源,避免因源缓存问题导致安装失败:
RUN apt-get update && apt-get install -y libgdal-dev gcc g++ && rm -rf /var/lib/apt/lists/* - 强制验证
gdal-config是否存在,可在Dockerfile中加入:RUN which gdal-config # 执行后应输出/usr/bin/gdal-config - 确保环境变量覆盖GDAL的头文件路径并包含
gdal-config所在目录:ENV CPLUS_INCLUDE_PATH=/usr/include/gdal ENV C_INCLUDE_PATH=/usr/include/gdal ENV PATH="/usr/bin:$PATH"
2. 为GDAL安装指定编译参数
pip安装GDAL时需要依赖系统级的GDAL库文件和头文件,直接指定编译参数可以解决路径匹配问题:
将requirements.txt中的GDAL==3.3.2替换为:
GDAL==3.3.2 --global-option=build_ext --global-option="-I/usr/include/gdal" --global-option="-L/usr/lib/x86_64-linux-gnu"
或者在Dockerfile中单独执行GDAL的安装命令(优先于安装整个requirements.txt):
RUN pip install GDAL==3.3.2 --global-option=build_ext --global-option="-I/usr/include/gdal" --global-option="-L/usr/lib/x86_64-linux-gnu" RUN pip install --no-cache-dir -r requirements.txt # 此时requirements.txt中可移除GDAL条目
3. 对齐系统libgdal-dev与pip安装的GDAL版本
Ubuntu 20.04默认源中的libgdal-dev版本为3.0.4,若你本地环境是3.3.2,说明本地添加了第三方PPA,容器内也需要同步这个源来获取对应版本的依赖:
RUN apt-get update && apt-get install -y software-properties-common RUN add-apt-repository ppa:ubuntugis/ppa && apt-get update RUN apt-get install -y libgdal-dev=3.3.2+dfsg-2~focal0 gcc g++
安装完对应版本的libgdal-dev后,再安装GDAL==3.3.2即可避免版本不兼容导致的路径问题。
4. 排查容器内的实际环境
若以上方法仍无效,可以临时修改Dockerfile的CMD为bash,启动容器后手动调试:
CMD ["bash"]
启动容器后执行以下命令定位问题:
gdal-config --version # 确认系统GDAL版本 echo $PATH # 检查gdal-config所在目录是否在PATH中 pip install GDAL==3.3.2 -v # 查看详细安装日志,定位具体错误点
附你提供的相关文件参考
错误日志
ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-xxxx/GDAL/setup.py'"'"'; file='"'"'/tmp/pip-install-xxxx/GDAL/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-xxxx
cwd: /tmp/pip-install-xxxx/GDAL/
Complete output (15 lines):
running egg_info
creating /tmp/pip-pip-egg-info-xxxx/GDAL.egg-info
writing /tmp/pip-pip-egg-info-xxxx/GDAL.egg-info/PKG-INFO
writing dependency_links to /tmp/pip-pip-egg-info-xxxx/GDAL.egg-info/dependency_links.txt
writing top-level names to /tmp/pip-pip-egg-info-xxxx/GDAL.egg-info/top_level.txt
writing manifest file '/tmp/pip-pip-egg-info-xxxx/GDAL.egg-info/SOURCES.txt'
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-xxxx/GDAL/setup.py", line 323, in
setup(**setup_kwargs)
File "/usr/local/lib/python3.8/site-packages/setuptools/init.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/usr/local/lib/python3.8/site-packages/setuptools/_distutils/core.py", line 148, in setup
_setup_distribution = dist = klass(attrs)
File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 467, in init
_Distribution.init(self, {
File "/usr/local/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 292, in init
self.finalize_options()
File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 829, in finalize_options
for ep in sorted(loaded, key=by_order):
File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 829, in
for ep in sorted(loaded, key=by_order):
File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 828, in
loaded = (ep.load() for ep in eps)
File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 475, in fetch_build_egg
return cmd.easy_install(req)
File "/usr/local/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 679, in easy_install
return self.install_item(spec, dist.location, tmpdir, deps)
File "/usr/local/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 705, in install_item
dists = self.install_eggs(spec, download, tmpdir)
File "/usr/local/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 890, in install_eggs
return self.build_and_install(setup_script, setup_base)
File "/usr/local/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1158, in build_and_install
self.run_setup(setup_script, setup_base, args)
File "/usr/local/lib/python3.8/site-packages/setuptools/command/easy_install.py", line 1144, in run_setup
exec(compile(code, file, 'exec'), locals(), globals())
File "/tmp/pip-install-xxxx/GDAL/setup.py", line 201, in
from GDALSetup import *
File "/tmp/pip-install-xxxx/GDAL/GDALSetup.py", line 51, in
gdal_config = GDALConfig('gdal-config')
File "/tmp/pip-install-xxxx/GDAL/GDALSetup.py", line 31, in init
self.version = self.run_and_parse('--version')
File "/tmp/pip-install-xxxx/GDAL/GDALSetup.py", line 41, in run_and_parse
stdout = subprocess.check_output([self.path, args], stderr=subprocess.STDOUT)
File "/usr/local/lib/python3.8/subprocess.py", line 415, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/local/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/lib/python3.8/subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'gdal-config'ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
Dockerfile
FROM python:3.8-slim WORKDIR /app RUN apt-get update && apt-get install -y \ libgdal-dev \ gcc \ && rm -rf /var/lib/apt/lists/* ENV CPLUS_INCLUDE_PATH=/usr/include/gdal ENV C_INCLUDE_PATH=/usr/include/gdal COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]
requirements.txt
Django==3.2.16 djangorestframework==3.13.1 GDAL==3.3.2 psycopg2-binary==2.9.3 celery==5.2.7 redis==4.3.4
内容的提问来源于stack exchange,提问作者Ananth

