23 September 2022

Compiling R packages on M1 Mac

  1. All kinds of errors installing packages on M1
  2. Installing gmp
  3. Installing gfortran
    1. Installing Cairo

All kinds of errors installing packages on M1

I personally found this entry useful in figuring out the build errors I was facing. They were namely related to R looking for my system libraries in the wrong places. I do not entirely understanding the underlying mechanisms at play here but I will shut up and show code.

Installing gmp

I ran into gmp.h not found errors after running renv::install("gmp"). I had to update my ~/.R/Makevars file to have the following flags.

CFLAGS=-I/opt/homebrew/include
CXXFLAGS=-I/opt/homebrew/include
LDFLAGS+=-L/opt/homebrew/opt/gmp/lib

From my understanding, CFLAGS would instruct R where to look for header files and LDFLAGS for linker files, whatever linker files are. CXXFLAGS were required since it seems that gmp in itself uses C++ and therefore, one must specify the C++ flag. I ran into the same gmp.h not found error without the CXX flag.

Installing gfortran

gfortran should be included with gcc, if you ran brew install gcc. I personally had to create a system link where R would expect gfortran to be located.

sudo ln -sf /usr/local/bin/gfortran /opt/R/arm64/bin/gfortran

If that doesn't work for you, you should do what was done in the aforementioned blog post.

FLIBS   =-L/opt/homebrew/opt/gfortran/lib
F77     = /opt/homebrew/bin/gfortran
FC      = /opt/homebrew/bin/gfortran

Installing Cairo

Aside from installing it from brew, your C and CXX flags in the Makevars file should be shown earlier in this blog entry. You will also need an X11 backend which on Mac else you will run into this error.

xlib-backend.c:34:10: fatal error: X11/Intrinsic.h: No such file or directory

The proposed solution was to install XQuartz. That alone did not work for me, I had to also install libxt.

brew install libxt
Tags: R