Installing brms on OSX 13.5 M1

I’ve been working through all the excellent resources on this forum but still haven’t had any luck getting things working yet. I’ve checked just configuring the rstan headers and that doesn’t work either:

install.packages(c("StanHeaders", "rstan"), type = "source")

This gives me back:

trying URL 'https://cran.rstudio.com/src/contrib/StanHeaders_2.26.27.tar.gz'
Content type 'application/x-gzip' length 2307044 bytes (2.2 MB)
==================================================
downloaded 2.2 MB

trying URL 'https://cran.rstudio.com/src/contrib/rstan_2.21.8.tar.gz'
Content type 'application/x-gzip' length 1203455 bytes (1.1 MB)
==================================================
downloaded 1.1 MB

* installing *source* package ‘StanHeaders’ ...
** package ‘StanHeaders’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -DNO_FPRINTF_OUTPUT -I"../inst/include" -I"sundials" -include stan_sundials_printf_override.hpp -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -D_REENTRANT -DSTAN_THREADS -DSTRICT_R_HEADERS -DUSE_PTHREAD -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppEigen/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppParallel/include' -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c cvodes/cvodes_spils.c -o cvodes/cvodes_spils.o
In file included from cvodes/cvodes_spils.c:19:
In file included from ../inst/include/cvodes/cvodes_ls.h:21:
../inst/include/sundials/sundials_direct.h:22:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
make: *** [/Library/Frameworks/R.framework/Resources/etc/Makeconf:169: cvodes/cvodes_spils.o] Error 1
ERROR: compilation failed for package ‘StanHeaders’
* removing ‘/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/StanHeaders’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/StanHeaders’
Warning in install.packages :
  installation of package ‘StanHeaders’ had non-zero exit status
* installing *source* package ‘rstan’ ...
** package ‘rstan’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -D_REENTRANT -DSTAN_THREADS   -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppEigen/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/BH/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/StanHeaders/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppParallel/include' -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c init.cpp -o init.o
clang-14: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from init.cpp:25:
In file included from /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppEigen/include/RcppEigen.h:25:
/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppEigen/include/RcppEigenForward.h:25:10: fatal error: 'iterator' file not found
#include <iterator>
         ^~~~~~~~~~
1 error generated.
make: *** [/Library/Frameworks/R.framework/Resources/etc/Makeconf:176: init.o] Error 1
ERROR: compilation failed for package ‘rstan’
* removing ‘/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rstan’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rstan’
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

The downloaded source packages are in
	‘/private/var/folders/pk/v31bk0rx0mvfl7r242p2hcqh0000gn/T/Rtmp1mgMHK/downloaded_packages’

Seems like R can’t find headers but I have XCode and XCode commandline tools both installed as well.

clang -v
Apple clang version 15.0.0 (clang-1500.0.38.1)
Target: arm64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Operating System: OSX 13.5
Interface Version: RStudio 2023.06.1
Compiler/Toolkit: R version 4.2.3 (2023-03-15)

A few other calls I’ve seen other folks find helpful

Sys.getenv("PATH")

is

"/Users/joshuanoble/opt/anaconda3/bin:/Users/joshuanoble/opt/anaconda3/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/joshuanoble/Applications/quarto/bin:/Library/TeX/texbin:/usr/texbin:/Applications/RStudio.app/Contents/Resources/app/quarto/bin:/Applications/RStudio.app/Contents/Resources/app/bin/postback:/Applications/RStudio.app/Contents/Resources/app/bin/postback"
Sys.getenv("LDLIBS")
[1] ""

Neither ~/.Renviron nor ~/.R/Makevars nor ~/.R/Rprofile exist.

Happy to put what happens when I run example(stan_model, package = "rstan", run.dontrun = TRUE, verbose = TRUE) as well. Thanks so much for any tips

You might be able to install the binaries of rstan and StanHeaders from the Stan r-packages repository:

install.packages(c("StanHeaders", "rstan"), repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Does that work? If so can you compile a model with brms or do you run into an error at that step?

Alternatively, you could try installing cmdstanr instead of rstan. If that works then you can run brms with argument backend="cmdstanr" and avoid using rstan entirely.

Thanks for the quick reply. I’m not able to set up cmdstanr either:

install_cmdstan(cores = 2)
The C++ toolchain required for CmdStan is setup properly!
trying URL 'https://api.github.com/repos/stan-dev/cmdstan/releases/latest'
Content type 'application/json; charset=utf-8' length 14642 bytes (14 KB)
==================================================
downloaded 14 KB

* Latest CmdStan release is v2.32.2
* Installing CmdStan v2.32.2 in /Users/joshuanoble/.cmdstan/cmdstan-2.32.2
* Downloading cmdstan-2.32.2.tar.gz from GitHub...
trying URL 'https://github.com/stan-dev/cmdstan/releases/download/v2.32.2/cmdstan-2.32.2.tar.gz'
Content type 'application/octet-stream' length 50452148 bytes (48.1 MB)
==================================================
downloaded 48.1 MB

* Download complete
* Unpacking archive...
* Building CmdStan binaries...
cp bin/mac-stanc bin/stanc
clang++ -pipe   -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT  -O3 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DNO_FPRINTF_OUTPUT     -O3  -c -x c -include stan/lib/stan_math/lib/sundials_6.1.1/include/stan_sundials_printf_override.hpp stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.c -o stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.o
chmod +x bin/stanc
clang++ -pipe   -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT  -O3 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DNO_FPRINTF_OUTPUT     -O3  -c -x c -include stan/lib/stan_math/lib/sundials_6.1.1/include/stan_sundials_printf_override.hpp stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.c -o stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.o
stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.c:19:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.c:19:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
make: *** [stan/lib/stan_math/make/libraries:64: stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.o] Error 1
make: *** Waiting for unfinished jobs....
1 error generated.
make: *** [stan/lib/stan_math/make/libraries:64: stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.o] Error 1

Warning message:
There was a problem during installation. See the error message(s) above. 

Looks like R can’t find any headers, this seems more of a big path problem than a Stan problem.

Noticing the anaconda include in your PATH, did you install R or the toolchain through conda? Conda changes the include/library paths that compilers use, and very rarely works well when compiling code through R

2 Likes

That seems to have been the problem! I didn’t install R or the toolchain through Anaconda but do have it installed on my machine and use it for other projects. I had already removed that from my path in terminal, so interestingly, my path in a zsh terminal looked like this:

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

I didn’t notice that the RStudio reported path was different and holding onto the path to Ananconda. Removing it in RStudio fixes my problems (yay!) Restarting RStudio though re-adds the Anaconda path confusingly, so I’m going to look into that. As long as I know what to do I’m happy enough so deep thanks all for all the help.

1 Like