MacOS Ventura(M1芯片)下Conda安装后gfortran无法运行的求助
我在搭载M1芯片的MacOS Ventura系统中,尝试让gfortran正常运行已有一段时间。问题在于:通过Homebrew安装的gfortran(12.2.0版本)原本可正常工作,但安装Anaconda/Conda后便无法运行,而我需要同时使用Conda和gfortran。
使用Homebrew安装gcc时,gfortran运行正常,但安装Conda后,编译Fortran程序(如Hello World)时出现错误:
gfortran hello.f90 -o trash ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/lib/libSystem.tbd' for architecture arm64 collect2: error: ld returned 1 exit status
为解决该问题,我卸载了Homebrew的gcc,转而通过Conda安装gfortran 11.2.0版本(执行命令conda install gfortran=11.2.0)。此时执行gfortran --version返回:
gfortran: warning: could not understand version 13.02.00 GNU Fortran (GCC) 11.2.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
执行which gfortran返回路径:/Users/Odilon/opt/anaconda3/bin/gfortran。但编译程序时仍出现错误:
gfortran: warning: could not understand version 13.02.00 ld: -rpath can only be used when targeting Mac OS X 10.5 or later collect2: error: ld returned 1 exit status
我的Xcode为最新14.2版本,已安装Command Line Tools。请问如何解决这些错误,让gfortran在Conda安装的情况下正常运行?
解决方法
方法一:指定编译目标MacOS版本
Conda安装的gfortran 11.2.0对Ventura(MacOS 13)的SDK兼容性不足,手动指定编译目标版本即可规避错误:
- 单次编译时添加参数:
gfortran hello.f90 -o trash -mmacosx-version-min=10.15
- 永久设置环境变量(避免每次输入):
如果用zsh,编辑~/.zshrc;用bash则编辑~/.bash_profile,添加以下内容:
export FFLAGS="-mmacosx-version-min=10.15" export LDFLAGS="-mmacosx-version-min=10.15"
保存后重启终端生效。
方法二:升级Conda中的gfortran版本
较新的Conda gfortran版本已修复M1 Ventura的兼容性问题,直接安装适配arm64的最新版:
- 卸载现有gfortran:
conda uninstall gfortran
- 从conda-forge安装最新版:
conda install -c conda-forge gfortran
- 验证版本:
gfortran --version
完成后重新编译程序即可正常运行。
方法三:保留Homebrew的gfortran,调整环境变量优先级
如果想继续使用Homebrew的gfortran,只需调整PATH让其优先级高于Conda:
- 编辑shell配置文件(
~/.zshrc或~/.bash_profile),添加:
export PATH="/opt/homebrew/bin:$PATH"
- 重启终端后,执行
which gfortran确认路径为/opt/homebrew/bin/gfortran - Conda环境仍可正常使用,其PATH会自动添加,只需保证Homebrew路径在最前面即可。
内容的提问来源于stack exchange,提问作者Odilon27

