How to install RcppArmadillo on Apple Silicon (M1) Macs

Viewed 2004

I've been trying many different ways to install RcppArmadillo, but I don't get it to work

install.packages(c('Rcpp'))
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
install.packages(c('RcppArmadillo'),type = "source")

It gives me this error:

ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [RcppArmadillo.so] Error 1
ERROR: compilation failed for package ‘RcppArmadillo’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo’

Obviously, I don't have x86_64-apple-darwin18/8.2.0 since I installed gfortran for the apple silicon architecture.

Mac version:

macOS Big Sur 
11.2.3
Apple M1

R version:

version
               _                           
platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          0.4                         
year           2021                        
month          02                          
day            15                          
svn rev        80002                       
language       R                           
version.string R version 4.0.4 (2021-02-15)
nickname       Lost Library Book           
2 Answers

I think I found an issue. First, I follow this tutorial : R COMPILER TOOLS FOR RCPP ON MACOS
Once completed, it gaves me almost the same error as you mentioned in your post :

ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/.....'
ld: library not found for -lgfortran clang: error: linker command failed
with exit code 1 (use -v to see invocation)

It seems, R is looking for gfortran in /opt/R/arm64 folder.
In the turorial it indicates that gfortran is installed in the /usr/local/gfortran folder.
I created a symbolic link in the /opt/R/arm64 which refers to /usr/local/gfortran with the command
ln -s /usr/local/gfortran /opt/R/arm64

One can set FLIBS in ~/.R/Makevars to one of the following options

# homebrew gfortran
FLIBS=-L/opt/homebrew/opt/gfortran/lib

# gfortran included in R
FLIBS=-L/opt/R/arm64/gfortran/lib

In addition one might want to also define F77 and FC as

F77     = /opt/R/arm64/gfortran/bin/gfortran
FC      = /opt/R/arm64/gfortran/bin/gfortran

To verify, try installing the glmnet package which should now succeed.

Related