gfortran: No such file or directory

MacOS上安装R包的时候报错,如下

* installing *source* package ‘vegan’ ...
** 成功将‘vegan’程序包解包并MD5和检查
** using staged installation
** libs
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c data2hill.c -o data2hill.o
usr/local/opt/gcc/bin/gfortran -fno-optimize-sibling-calls  -fPIC  -Wall -g -O2  -c decorana.f -o decorana.o
make: usr/local/opt/gcc/bin/gfortran: No such file or directory
make: *** [decorana.o] Error 1
ERROR: compilation failed for package ‘vegan’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/vegan’
Warning in install.packages :
  installation of package ‘vegan’ had non-zero exit status

Try1:重装gcc

检查发现的确没有这个文件,上网搜说是gcc下的文件,重装gcc就可以,于是用homebrew安装gcc,出现了以下报错

在.zshrc里添加这一行export HOMEBREW_NO_INSTALL_CLEANUP=TRUE,重新安装gcc就没有报错了,gfortran -v也能执行成功,说明gfortran安装没问题了
然后回到Rstudio,安装R包,还是报错说找不到gfortran...

Try2:设置R的环境变量

我想,应该是R环境变量的问题,于是上网搜了搜,看到一个解答
https://blog.csdn.net/u012110870/article/details/105066621

Sys.getenv() 查看R目前的所有环境变量,PATH下的路径都没有gfortran

我的gfortran安装在/usr/local/bin下面,所以把/usr/local/bin添加到PATH

old_path = Sys.getenv("PATH")
new_path = paste(old_path, "/usr/local/bin", sep = ":")
Sys.setenv("PATH" = new_path)

然而安装R包还是报同样的错....

Try3:配置~/.R/Makevars

按照网上建议,配置~/.R/Makevars
https://www.cynkra.com/blog/2021-03-16-gfortran-macos/

cat >> ~/.R/Makevars
FC      = usr/local/opt/gcc/bin/gfortran
F77     = /usr/local/opt/gcc/bin/gfortran
FLIBS   = -L/usr/local/opt/gcc/lib

重启R,install.packages("picante"),同样的报错...

最后通过重新安装R解决的问题

posted @ 2022-04-24 09:56  Ryann'sBio  阅读(3005)  评论(1编辑  收藏  举报