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

Amazon Linux 2023 EC2实例无EPEL装GDAL后Terra包安装失败

在Amazon Linux 2023上安装R包terra时找不到gdal-config的解决方法

问题背景

我创建了一台使用Amazon Linux 2023 AMI(al2023-ami-2023.1.20230705.0-kernel-6.1-x86_64)的EC2实例,想要安装GDAL以便在R中使用terra包。但现有指南均要求使用EPEL,而EPEL在Amazon Linux 2023中不可用。

我尝试用Spack作为替代方案,成功编译安装了GDAL、GEOS及约38个依赖,但在R中安装terra时仍报错,提示找不到gdal-config。我使用renv作为包管理器。

该问题曾被标记为重复,但并不适用:一是我使用的是Red Hat系Linux而非Debian;二是Amazon Linux 2023中没有gdal-devel包。执行dnf search gd的结果如下:

Last metadata expiration check: 11:17:55 ago on Tue Jul 18 02:29:44 2023.
========================================== Name Exactly Matched: gd ===========================================gd.x86_64 : A graphics library for quick creation of PNG or JPEG images
========================================= Name & Summary Matched: gd ==========================================gcc-gdb-plugin.x86_64 : GCC plugin for GDB
gd-devel.x86_64 : The development libraries and header files for gd
gd-progs.x86_64 : Utility programs that use libgd
gdb-doc.noarch : Documentation for GDB (the GNU source-level debugger)
gdb-gdbserver.x86_64 : A standalone server for GDB (the GNU source-level debugger)
gdbm-devel.x86_64 : Development libraries and header files for the gdbm library
gdbm-libs.x86_64 : Libraries files for gdbm
gdk-pixbuf2-devel.x86_64 : Development files for gdk-pixbuf2
gdk-pixbuf2-modules.x86_64 : Additional image modules for gdk-pixbuf2
gdk-pixbuf2-tests.x86_64 : Tests for the gdk-pixbuf2 package
google-noto-sans-old-sogdian-fonts.noarch : Noto Sans Old Sogdian font
google-noto-sans-sogdian-fonts.noarch : Noto Sans Sogdian font
graphviz-gd.x86_64 : Graphviz plugin for renderers based on gd
librevenge-gdb.x86_64 : gdb pretty printers for librevenge
perl-GD.x86_64 : Perl interface to the GD graphics library
perl-GD-Barcode.noarch : Create barcode image with GD
perl-GDBM_File.x86_64 : Perl5 access to the gdbm library
php8.1-gd.x86_64 : A module for PHP 8.1 applications for using the gd graphics library
php8.2-gd.x86_64 : A module for PHP 8.2 applications for using the gd graphics library
ruby3.2-rubygem-bigdecimal.x86_64 : BigDecimal provides arbitrary-precision floating point decimal arithmetic  
rust-gdb.noarch : GDB pretty printers for Rust
texlive-flagderiv-doc.noarch : Documentation for flagderiv
texlive-kdgdocs-doc.noarch : Documentation for kdgdocs
============================================== Name Matched: gd ===============================================gdb.x86_64 : A stub package for GNU source-level debugger
gdb-headless.x86_64 : A GNU source-level debugger for C, C++, Fortran, Go and other languages
gdb-minimal.x86_64 : A GNU source-level debugger for C, C++, Fortran, Go and other languages (minimal version) 
gdbm.x86_64 : A GNU set of database routines which use extensible hashing
gdisk.x86_64 : An fdisk-like partitioning tool for GPT disks
gdk-pixbuf2.x86_64 : An image loading library
glibc-langpack-gd.x86_64 : Locale data for Scottish Gaelic
swig-gdb.x86_64 : Commands for easier debugging of SWIG
texlive-ecgdraw.noarch : Draws electrocardiograms (ECG)
texlive-flagderiv.noarch : Flag style derivation package
texlive-kdgdocs.noarch : Document classes for Karel de Grote University College
texlive-longdivision.noarch : Long division arithmetic problems
============================================= Summary Matched: gd =============================================langpacks-core-en_GB.noarch : English (United Kingdom) langpacks core meta-package
langpacks-en_GB.noarch : English (United Kingdom) langpacks meta-package

解决方案

问题根源是Spack编译安装的GDAL不在系统默认的PATH路径中,导致R无法找到gdal-config工具。以下是几种解决方法:

1. 定位Spack安装的GDAL路径

首先找到GDAL的安装目录,在终端运行:

spack location -i gdal

输出路径类似/home/ec2-user/spack/opt/spack/linux-amzn2023-x86_64/gcc-12.2.0/gdal-3.7.2-abcdef,gdal-config工具位于该路径下的bin子目录中。

2. 临时设置环境变量(当前会话有效)

在终端中添加GDAL的bin目录到PATH,再启动R安装terra:

export PATH=$(spack location -i gdal)/bin:$PATH
R

在R中执行:

install.packages("terra")

3. 永久设置环境变量(全局生效)

编辑~/.bashrc或~/.bash_profile文件,添加以下内容:

export PATH=$(spack location -i gdal)/bin:$PATH
export LD_LIBRARY_PATH=$(spack location -i gdal)/lib:$LD_LIBRARY_PATH

保存后执行以下命令使更改生效:

source ~/.bashrc

之后启动R即可正常安装terra。

4. 针对renv环境的配置

如果使用renv管理R包,可以在项目根目录下创建或编辑.Renviron文件,添加:

PATH="$(spack location -i gdal)/bin:$PATH"
LD_LIBRARY_PATH="$(spack location -i gdal)/lib:$LD_LIBRARY_PATH"

重启R后,环境变量会自动加载,此时安装terra即可成功。


内容的提问来源于stack exchange,提问作者AaronDD

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 03:08:24