Issue of installing R package on arm64 Mac M1

使用2021年新款M1芯片Mac时,安装需要source的R包时,常常会出现问题。原因主要有以下几点:

  1. homebrew 安装包的路径发送变化:

    • old x86_64 path in /usr/local

    • new arm64 path in /opt/homebrew

所以我们需要告诉R,在新的路径里面找 brew install package。修改好的结果如下:

Sys.getenv("PATH")
"/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/RStudio.app/Contents/MacOS/postback"
  1. 需要把新路径添加到R里,让R可以找对相应的包的文件夹

例如:安装CLVTools包时发生的错误

install.packages("CLVTools")
Installing package into/Users/userName/Library/R/arm64/4.1/library(aslibis unspecified)

  There is a binary version available but the source version is later:
         binary source needs_compilation
CLVTools  0.8.1  0.9.0              TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) y
installing the source packageCLVTools
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/CLVTools_0.9.0.tar.gz'
Content type 'application/octet-stream' length 770884 bytes (752 KB)
==================================================
downloaded 752 KB

* installing *source* packageCLVTools...
** packageCLVToolssuccessfully unpacked and MD5 sums checked
** using staged installation
** libs
clang++ -arch arm64 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/Rcpp/include' -I'/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppArmadillo/include' -I'/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppGSL/include' -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c RcppExports.cpp -o RcppExports.o
In file included from RcppExports.cpp:5:
In file included from /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppGSL/include/RcppGSL.h:25:
/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppGSL/include/RcppGSLForward.h:25:10: fatal error: 'gsl/gsl_vector.h' file not found
#include <gsl/gsl_vector.h> 
         ^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for packageCLVTools* removing/Users/chenxing/Library/R/arm64/4.1/library/CLVToolsWarning in install.packages :
  installation of packageCLVToolshad non-zero exit status

The downloaded source packages are in
/private/var/folders/tc/k2t27jnx50zc0qmc_v0q8mxc0000gn/T/RtmpwWJuHq/downloaded_packages

原因就是因为R没有找对gsl包的路径。

这个解决的方案是:将如下代码加入到~/.R/Makevars文件里

CFLAGS=-I/opt/homebrew/include
CPPFLAGS=-I/opt/homebrew/include
CXXFLAGS=-I/opt/homebrew/include

详细的解决办法请参考这个链接:

Transitioning from x86 to arm64 on macOS - experiences of an R user

Chen Xing
Chen Xing
Founder & Data Scientist

Enjoy Life & Enjoy Work!

Related