Mac M1 Ventura安装R空间包时gdal-config无法定位求助
问题
在搭载Ventura系统的MacBook Air M1上,尝试安装sf、stars、rgdal等空间数据处理R包。已通过Homebrew安装gdal及依赖proj,但从源码安装sf时提示找不到gdal-config文件,重装gdal后仍未解决,如何为R指定该文件的路径?
环境信息
系统PATH配置
(base)@MacBook-Air ~ % echo $PATH /opt/homebrew/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
gdal-config路径
(base)@MacBook-Air ~ % whereis gdal-config gdal-config: /opt/homebrew/bin/gdal-config /opt/homebrew/share/man/man1/gdal-config.1
gdal-config可执行性验证
(base)@MacBook-Air ~ % gdal-config Usage: gdal-config [OPTIONS] Options: [--prefix[=DIR]] [--libs] [--dep-libs] [--cflags] [--datadir] [--version] [--ogr-enabled] [--gnm-enabled] [--formats]
R环境信息
> sessionInfo() R version 4.0.3 (2020-10-10) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS 13.1 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_4.0.3 tools_4.0.3
PROJ版本信息
(base)@MacBook-Air ~ % proj DeprecationWarning: PROJ_LIB environment variable is deprecated, and will be removed in a future release. You are encouraged to set PROJ_DATA instead. Rel. 9.1.1, December 1st, 2022 usage: proj [-bdeEfiIlmorsStTvVwW [args]] [+opt[=arg] ...] [file ...]
解决方案
方法1:安装时直接指定编译参数
安装sf包时,通过configure.args参数传入gdal-config的绝对路径:
install.packages("sf", configure.args = "--with-gdal-config=/opt/homebrew/bin/gdal-config")
若要同时解决proj相关的潜在依赖问题,可扩展参数:
install.packages("sf", configure.args = c( "--with-gdal-config=/opt/homebrew/bin/gdal-config", "--with-proj-include=/opt/homebrew/include", "--with-proj-lib=/opt/homebrew/lib", "--with-proj-data=/opt/homebrew/share/proj" ))
方法2:配置R的环境变量
临时生效(当前R会话)
在R控制台中设置环境变量,让R能识别gdal-config的位置:
Sys.setenv(PATH = paste("/opt/homebrew/bin", Sys.getenv("PATH"), sep = ":")) Sys.setenv(PROJ_DATA = "/opt/homebrew/share/proj")
设置完成后执行install.packages("sf")即可。
永久生效(所有R会话)
- 打开终端,创建或编辑R的环境配置文件:
touch ~/.Renviron open -e ~/.Renviron
- 在文件中添加以下内容:
PATH="/opt/homebrew/bin:${PATH}" PROJ_DATA="/opt/homebrew/share/proj"
- 保存文件后重启R,再尝试安装目标包。
方法3:解决架构不匹配问题
当前R为x86_64架构,而M1 Mac默认是arm64架构,Homebrew安装的gdal是arm64版本,可能存在架构冲突。可通过以下两种方式处理:
- 安装Apple Silicon版本的R:替换现有x86版本为适配arm64的R安装包。
- 用Rosetta运行终端和R:右键终端选择「使用Rosetta打开」,在该终端中启动R后再安装包。
内容的提问来源于stack exchange,提问作者Jouline
相关产品推荐
相关产品推荐

