Trouble installing CmdStanR on Mac

I’m trying to install CmdStanR on my Mac and am running into trouble. Since I haven’t been able to solve this problem via Googling, I thought I’d try and get some help here because someone may have run into the same issue.

I started out by installing the CmdStanR packages:

> install.packages("cmdstanr", repos = c("", getOption("repos")))
Warning in install.packages :
  unable to access index for repository
  cannot open URL ''
installing the source package ‘cmdstanr’

trying URL ''
Content type 'application/gzip' length 831895 bytes (812 KB)
downloaded 812 KB

* installing *source* package ‘cmdstanr’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (cmdstanr)

The downloaded source packages are in

So far, so good I think. Next I loaded the CmdStanR libary:

> library(cmdstanr)
This is cmdstanr version 0.1.3
- Online documentation and vignettes at
- CmdStan path set to: /Users/andrew/.cmdstanr/cmdstan-2.24.1
- Use set_cmdstan_path() to change the path

I next ran install_cmdstan() as done in this vignette:

> install_cmdstan(cores = 2, overwrite = TRUE)
* Latest CmdStan release is v2.24.1
* Installing CmdStan v2.24.1 in /Users/andrew/.cmdstanr/cmdstan-2.24.1
* Downloading cmdstan-2.24.1.tar.gz from GitHub...
* Removing the existing installation of CmdStan...
* Download complete
* Unpacking archive...
* Building CmdStan binaries...
cp bin/mac-stanc bin/stanc
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.7 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_5.2.0/include    -DBOOST_DISABLE_ASSERTS      -c -fvisibility=hidden -o bin/cmdstan/stansummary.o src/cmdstan/stansummary.cpp
chmod +x bin/stanc
cd stan/lib/stan_math/lib/boost_1.72.0; ./
Building Boost.Build engine with toolset clang... In file included from src/cmdstan/stansummary.cpp:1:
In file included from src/cmdstan/stansummary_helper.hpp:4:
In file included from stan/src/stan/mcmc/chains.hpp:4:
In file included from stan/src/stan/io/stan_csv_reader.hpp:4:
In file included from stan/lib/stan_math/lib/boost_1.72.0/boost/algorithm/string.hpp:18:
In file included from stan/lib/stan_math/lib/boost_1.72.0/boost/algorithm/string/std_containers_traits.hpp:18:
In file included from stan/lib/stan_math/lib/boost_1.72.0/boost/config.hpp:57:
In file included from stan/lib/stan_math/lib/boost_1.72.0/boost/config/platform/macos.hpp:28:
stan/lib/stan_math/lib/boost_1.72.0/boost/config/detail/posix_features.hpp:18:15: fatal error: 'unistd.h' file not found
#     include <unistd.h>
1 error generated.
make: *** [bin/cmdstan/stansummary.o] Error 1
make: *** Waiting for unfinished jobs....

Failed to build Boost.Build build engine
Consult 'bootstrap.log' for more details
make: *** [stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_program_options.a] Error 1

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

As you can see, this is where I started running into trouble. I’m not sure what is causing this error - I couldn’t find a similar issue when I was Googling.

I know that CmdStanR isn’t working right because I can’t run simple examples from the vignette:

> file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
> mod <- cmdstan_model(file)
Compiling Stan program...
stan/lib/stan_math/lib/sundials_5.2.0/src/nvector/serial/nvector_serial.c:19:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
1 error generated.
Error: An error occured during compilation! See the message above for more information.

I’d appreciate any help that can be provided. Here’s my session info in case it’s helpful:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] cmdstanr_0.1.3

loaded via a namespace (and not attached):
 [1] processx_3.4.3   compiler_3.6.1   backports_1.1.8  R6_2.4.1         rsconnect_0.8.15 tools_3.6.1      rstudioapi_0.11  crayon_1.3.4     checkmate_2.0.0 
[10] jsonlite_1.7.1   ps_1.3.4  ```


  1. You do have Xcode tools installed and you use that clang++ compiler (it looks that way but it doesn’t hurt to ask).
  2. If you open R and not RStudio, what happens if you run the same thing in the terminal then?

Thanks for the reply. Here are the answers:

I do have Xcode tools installed, though I’m having trouble verifying that I’m using the clang++ compiler (though if you have an easy R command I can use to check, I’ll happily take it). I should mention that I’ve run models using rstanarm and brms in the past with no problem (in case that clarifies the compiler issue).

I get the same exact errors and warnings.

@andrew_d Sorry you’re running into this error. @torkar thanks for trying to help!

Yeah if you can currently use brms successfully then your compiler should be fine (rstanarm is precompiled, so you could use it even without a functioning compiler).

@mitzimorris @rok_cesnovar Any ideas here?

Thanks for the clarification on the compiler @jonah. Also, thanks for pinging @mitzimorris and @rok_cesnovar about this issue. I’m still trying to solve it.

this looks like C++ compiler is using wrong set of libraries when trying to compile things.

do you have an RStan installation? do you have a file make/local somewhere? I don’t know much about the R/RStan config - hoping that another dev / CmdStanR user can help out.

@andrew_d Do you have anything in the .Renviron file or the .R/Makevars file? You can check manually but more easily via the usethis package:


I didn’t think these files would matter for CmdStanR (they do for RStan) but just in case it would be good to know.

@mitzimorris Any idea if this could have something to do with using the older macOS Mojave? If I remember correctly CmdStan should work fine with Mojave, but maybe I’m forgetting something.

I never upgraded to Mojave - my old laptop is still running High Sierra

When I run either of these commands I get the following error:

Error: [EACCES] Failed to open '/Users/andrew/.R/Makevars': permission denied

I can upgrade to Catalina quite easily. Perhaps this would help? If so, I can give it a try…

Hmm, do you have restricted permissions on the computer you’re using?

I have it running successfully with R version 4.0.2 and Catalina, but I thought it should work ok with R 3.6.1 and Mojave like you have. It wouldn’t hurt anything to upgrade (at least it wouldn’t hurt anything Stan or R related), but it probably won’t help to upgrade if the problem is related to permissions.

I think that you should try re-installing the Xcode command line tools -

I realize the SO post is about Catalina, but worth a try on Mojave before upgrading.

xcode-select --install
1 Like

R 3.6.2 & Mojave check in, it works on my machine.


Just to close the loop on this issue, the only thing that worked was just reinstalling R, R Studio, and re-doing the RStan setup.


@andrew_d sorry for the hassle but glad that it’s working now!

Perhaps helpful for the community: I had the same problem as andrew_d and when I updated RStudio to the latest version (1.4.1106), it did the trick. No need to reinstall R and re-doing the Rstan setup - at least in my case.

I just had this exact same issue (RStudio 1.4.1106, session info below). I tried all the above to no avail. Header files were not visible when compiling models.

The one thing that worked for me was r-mac-tools by @coatless. It ran without an issue and CmdStan has worked ever since.

R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] cmdstanr_0.3.0 rethinking_1.95 dagitty_0.2-2 rstan_2.21.3
[5] ggplot2_3.3.2 StanHeaders_2.21.0-7

loaded via a namespace (and not attached):
[1] shape_1.4.4 xfun_0.22 tidyselect_1.1.0 purrr_0.3.3
[5] lattice_0.20-38 V8_3.2.0 colorspace_1.4-1 vctrs_0.3.4
[9] generics_0.0.2 stats4_3.6.2 loo_2.3.1 rlang_0.4.7
[13] pkgbuild_1.1.0 pillar_1.4.6 glue_1.4.2 withr_2.3.0
[17] matrixStats_0.57.0 lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0
[21] mvtnorm_1.0-12 codetools_0.2-16 coda_0.19-3 knitr_1.31
[25] inline_0.3.16 callr_3.4.4 ps_1.3.4 curl_4.3
[29] fansi_0.4.1 Rcpp_1.0.5 backports_1.1.10 checkmate_2.0.0
[33] scales_1.1.1 RcppParallel_5.0.2 jsonlite_1.7.1 gridExtra_2.3
[37] processx_3.5.0 dplyr_1.0.2 grid_3.6.2 cli_2.0.2
[41] tools_3.6.2 magrittr_1.5 tibble_3.0.3 crayon_1.3.4
[45] pkgconfig_2.0.3 ellipsis_0.3.1 MASS_7.3-51.5 prettyunits_1.1.1
[49] assertthat_0.2.1 rstudioapi_0.11 R6_2.4.1 boot_1.3-24
[53] compiler_3.6.2