Compilation error after installing rstan on new arm Mac with macOS Monterey


I recently installed rstan on a new arm Mac and then tried running a brms model - however the model doesn’t run and I get a compilation error:

Error in compileCode(f, code, language = language, verbose = verbose) :
In file included from :1:In file included from /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:In file included from /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:In file included from /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppEigen/include/Eigen/Core:82:In file included from /usr/local/opt/llvm/bin/…/include/c++/v1/new:90:In file included from /usr/local/opt/llvm/bin/…/include/c++/v1/exception:81:In file included from /usr/local/opt/llvm/bin/…/include/c++/v1/cstdlib:85:/usr/local/opt/llvm/bin/…/include/c++/v1/stdlib.h:97:15: fatal error: ‘stdlib.h’ file not found#include_next <stdlib.h>

I have virtually tried every suggestion I found online after googling this issue, have uninstalled and reinstalled xcode, have removed and when this didn’t work (as apparently it did for some people) re-added command line tools, etc. Unfortunately none of the previous suggestions worked for me.

Any help on this would be much appreciated!

In case it helps, below is my session info:

R version 4.1.3 (2022-03-10)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.3

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] ggpubr_0.4.0 summarytools_1.0.0 ltm_1.2-0 polycor_0.8-1 msm_1.6.9
[6] MASS_7.3-55 sjmisc_2.8.9 viridis_0.6.2 viridisLite_0.4.0 ggridges_0.5.3
[11] ggthemes_4.2.4 ggmcmc_1.5.1.1 RColorBrewer_1.1-2 devtools_2.4.3 usethis_2.1.5
[16] HDInterval_0.2.2 brms_2.16.3 Rcpp_1.0.8 forcats_0.5.1 stringr_1.4.0
[21] purrr_0.3.4 tidyr_1.2.0 tidyverse_1.3.1 afex_1.0-1 lme4_1.1-28
[26] Matrix_1.4-0 zip_2.2.0 dplyr_1.0.8 readr_2.1.2 broom_0.7.12
[31] Select_1.4 tibble_3.1.6 rstan_2.21.3 ggplot2_3.3.5 StanHeaders_2.21.0-7

I don’t have a copy of Monterey to play with but I remember something about there not being a /usr/include directory? That might be part of the issue? This will at least bump the post and someone else might see it.

Thanks a lot for your reply. The main issue does seem to be coming from the fact that apple’s new arm chips are built based on a completely new architecture. I’ve somehow managed to solve the issue but still have not completely gotten behind what exactly got it to work yet.

I think a major part was me following this guide Transitioning from x86 to arm64 on macOS - experiences of an R user to a tee. However, the compilation error persisted for a while after I followed these steps and then magically went away. Yet, still now I sometimes get the message ‘recompiling to avoid crashing R session’.

1 Like