Can't get rstan to install on Mac OS Mojave

I have been having a terrible time getting rstan installed and setup.

The short of it is that if I install from binary, I get errors, and I can’t seem to compile from the source code.

System Version Numbers

I’m working on a Mac OSX system with Mojave 10.14.6.
R version 4.0.2 (2020-06-22)

$ clang++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

> pkgbuild::has_build_tools(debug=TRUE)
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC
 -Wall -g -O2  -c foo.c -o foo.o
clang -mmacosx-version-min=10.13 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multipl
y_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o foo.so foo.o -F/Library/Frameworks/R.fra
mework/.. -framework R -Wl,-framework -Wl,CoreFoundation

[1] TRUE

Makevars Configurations I’ve Tried

I’ve tried having different things in my Makevars file, including empty, and then the following different options:

After blank, I tried this:

CFLAGS+=-O3 -march=native -mtune=native
CCFLAGS+=-O3 -march=native -mtune=native
CXXFLAGS+=-O3 -march=native -mtune=native

CXX14FLAGS+=-O3 -march=native -mtune=native

CXX14FLAGS += -DSTAN_THREADS
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256
CXX14FLAGS += -DBOOST_MATH_PROMOTE_DOUBLE_POLICY=false

Then I tried this:

CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include

SHLIB_CXXLDFLAGS+=-Wl,-rpath,${R_HOME}/lib ${R_HOME}/lib/libc++abi.1.dylib
SHLIB_CXX14LDFLAGS+=-Wl,-rpath,${R_HOME}/lib ${R_HOME}/lib/libc++abi.1.dylib

Then I tried this:

CXX14FLAGS=-O3 -march=native -mtune=native
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256

And finally I tried:

CPPFLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"

Currently I’ve reverted it to blank, since nothing seems to be working, but I’m happy to try any suggestions that might help me get rstan installed and working properly.

Session Info

Since I was over the character limit when I tried to submit this, I’ve restricted the session output to those packages that seem relevant, but please let me know if there are other packages you’d like to see, or if it’s useful for me to add the full output in a comment.

 Rcpp           1.0.4.6   2020-04-09 [1] CRAN (R 4.0.0)
 RcppEigen      0.3.3.7.0 2019-11-16 [1] CRAN (R 4.0.0)
 RcppParallel   5.0.2     2020-06-24 [1] CRAN (R 4.0.2)
 rstan          2.21.1    2020-07-08 [1] CRAN (R 4.0.2)
 StanHeaders    2.21.0-6  2020-08-16 [1] CRAN (R 4.0.2)

What Happens When I Try Installing

Without Unsetting C++ Flags

If I just install rstan 2.21.1 from binary, here’s what happens:

> library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.1, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
> J <- 8
> y <- c(28, 8, -3, 7, -1, 1, 18, 12)
> sigma <- c(15, 10, 16, 11, 9, 11, 10, 18)
> data <- list(J=J, y=y, sigma=sigma)
> modelcode <-'
+ data {
+   int<lower=0> N;
+   vector[N] y;
+   vector[N] sigma_y;
+ }
+ parameters {
  vector[N] eta;
  real mu_theta;
+   vector[N] eta;
+   real mu_theta;
+   real<lower=0,upper=100> sigma_eta;
+   real xi;
+ }
+ transformed parameters {
  real<lower=0> sigma_theta;
  vector[N] theta;
  theta = mu_theta + xi * eta;
  sigma_theta = fabs(xi) / sigma_eta;
+   real<lower=0> sigma_theta;
+   vector[N] theta;
+   theta = mu_theta + xi * eta;
+   sigma_theta = fabs(xi) / sigma_eta;
+ }
+ model {
+   mu_theta ~ normal(0, 100);
+   sigma_eta ~ inv_gamma(1, 1); //prior distribution can be changed to uniform
+   eta ~ normal(0, sigma_eta);
+   xi ~ normal(0, 5);
+   y ~ normal(theta,sigma_y);
+ }
+ '
> fit8 <- stan(model_code=modelcode, data=data)
sh: clang++ -mmacosx-version-min=10.13: command not found
The NEXT version of Stan will not be able to pre-process your Stan program.
Please open an issue at
 https://github.com/stan-dev/stanc3/issues
if you can share or at least describe your Stan program. This will help ensure that Stan
continues to work on your Stan programs in the future. Thank you!
This message can be avoided by wrapping your function call inside suppressMessages().
Error in compileCode(f, code, language = language, verbose = verbose) :
  Compilation ERROR, function(s)/method(s) not created! In file included from <built-in>:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun
/Eigen.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:436:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/MatrixBas
e.h:130:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/eigen_plugins.h:22:30: er
ror: no template named 'conditional_t' in namespace 'std'; did you mean 'conditional'?
using double_return_t = std::conditional_t<std::is_const<std::remove_reference_t<T>>::value,
                        ~~~~~^~~~~~~~~~~~~
                             condit
In addition: Warning messages:
1: In system2(CXX, args = ARGS) : error in running command
2: In file.remove(c(unprocessed, processed)) :
  cannot remove file '/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpQIuwrV/file100bf28e8af38.stan', reason 'No such fil
e or directory'
3: In system(cmd, intern = !verbose) :
  running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file100bf38f1317a.cpp 2> file100bf38f1317a.cpp.err
.txt' had status 1
Error in sink(type = "output") : invalid connection

If I try Sys.unsetenv("PKG_CXXFLAGS")

After running Sys.unsetenv("PKG_CXXFLAGS") and then running the same commands as above, this is what I get:

sh: clang++ -mmacosx-version-min=10.13: command not found
The NEXT version of Stan will not be able to pre-process your Stan program.
Please open an issue at
 https://github.com/stan-dev/stanc3/issues
if you can share or at least describe your Stan program. This will help ensure that Stan
continues to work on your Stan programs in the future. Thank you!
This message can be avoided by wrapping your function call inside suppressMessages().
Error in mod$fit_ptr() :
  Exception: variable does not exist; processing stage=data initialization; variable name=N; base type=int  (in 'model100bf43be
ab80_58d9cece79667a13f56c3248ffff0dc2' at line 3)

In addition: Warning messages:
1: In system2(CXX, args = ARGS) : error in running command
2: In file.remove(c(unprocessed, processed)) :
  cannot remove file '/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpQIuwrV/file100bf5f43aae8.stan', reason 'No such fil
e or directory'
failed to create the sampler; sampling not done

Installing from source

If I remove rstan with remove.packages("rstan") and
confirming that system.file("libs", package = "rstan") returns "", I run into compilation issues:

> remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan")
Downloading GitHub repo stan-dev/rstan@develop
'/usr/local/bin/git' clone --depth 1 --no-hardlinks --recurse-submodules --branch develop https://github.com/stan-dev/stan.git
/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-ae36e6f/rstan/rstan/../../Stan
Headers/inst/include/upstream
Cloning into '/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-ae36e6f/rstan/rs
tan/../../StanHeaders/inst/include/upstream'...
remote: Enumerating objects: 2405, done.
remote: Counting objects: 100% (2405/2405), done.
remote: Compressing objects: 100% (1487/1487), done.
remote: Total 2405 (delta 924), reused 1577 (delta 761), pack-reused 0
Receiving objects: 100% (2405/2405), 5.70 MiB | 8.72 MiB/s, done.
Resolving deltas: 100% (924/924), done.
Submodule 'lib/stan_math' (https://github.com/stan-dev/math.git) registered for path 'lib/stan_math'
Cloning into '/private/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T/RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-ae36e6f/S
tanHeaders/inst/include/upstream/lib/stan_math'...
remote: Enumerating objects: 96, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 441509 (delta 53), reused 71 (delta 45), pack-reused 441413
Receiving objects: 100% (441509/441509), 438.15 MiB | 34.14 MiB/s, done.
Resolving deltas: 100% (316320/316320), done.
'/usr/local/bin/git' clone --depth 1 --no-hardlinks --recurse-submodules --branch develop https://github.com/stan-dev/math.git
/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-ae36e6f/rstan/rstan/../../Stan
Headers/inst/include/mathlib
Cloning into '/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-ae36e6f/rstan/rs
tan/../../StanHeaders/inst/include/mathlib'...
remote: Enumerating objects: 28433, done.
remote: Counting objects: 100% (28433/28433), done.
remote: Compressing objects: 100% (22430/22430), done.
remote: Total 28433 (delta 7057), reused 16825 (delta 5490), pack-reused 0
Receiving objects: 100% (28433/28433), 35.90 MiB | 9.69 MiB/s, done.
Resolving deltas: 100% (7057/7057), done.
Updating files: 100% (26369/26369), done.
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

 1: All
 2: CRAN packages only
 3: None
 4: backports   (1.1.6   -> 1.1.10) [CRAN]
 5: glue        (1.4.0   -> 1.4.2 ) [CRAN]
 6: ps          (1.3.2   -> 1.4.0 ) [CRAN]
 7: processx    (3.4.2   -> 3.4.4 ) [CRAN]
 8: vctrs       (0.3.2   -> 0.3.4 ) [CRAN]
 9: pillar      (1.4.3   -> 1.4.6 ) [CRAN]
10: ellipsis    (0.3.0   -> 0.3.1 ) [CRAN]
11: callr       (3.4.3   -> 3.4.4 ) [CRAN]
12: pkgbuild    (1.0.7   -> 1.1.0 ) [CRAN]
13: withr       (2.2.0   -> 2.3.0 ) [CRAN]
14: pkgload     (1.0.2   -> 1.1.0 ) [CRAN]
15: Rcpp        (1.0.4.6 -> 1.0.5 ) [CRAN]
16: jsonlite    (1.6.1   -> 1.7.1 ) [CRAN]
17: matrixStats (0.56.0  -> 0.57.0) [CRAN]
18: tibble      (3.0.1   -> 3.0.3 ) [CRAN]
19: scales      (1.1.0   -> 1.1.1 ) [CRAN]
20: isoband     (0.2.1   -> 0.2.2 ) [CRAN]
21: V8          (3.0.2   -> 3.2.0 ) [CRAN]
22: inline      (0.3.15  -> 0.3.16) [CRAN]

Enter one or more numbers, or an empty line to skip updates:3
   checking for file ‘/private/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T/RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-a
✔  checking for file ‘/private/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T/RtmpD3uvk0/remotes104f2280167d2/stan-dev-rstan-a
e36e6f/rstan/rstan/DESCRIPTION’ (340ms)
─  preparing ‘rstan’: (781ms)
✔  checking DESCRIPTION meta-information ...
─  cleaning src
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories (472ms)
─  looking to see if a ‘data/datalist’ file should be added
─  building ‘rstan_2.21.3.tar.gz’

* installing *source* package ‘rstan’ ...
** using staged installation
** libs
clang++ -mmacosx-version-min=10.13 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/inclu
de" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PT
R -D_REENTRANT -DSTAN_THREADS   -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I'/Library/Fra
meworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources
/library/BH/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include' -I'/Library/Framewo
rks/R.framework/Versions/4.0/Resources/library/RcppParallel/include' -I/usr/local/include  -std=c++11 -fPIC  -Wall -g -O2  -c M
odule.cpp -o Module.o
In file included from Module.cpp:2:
In file included from ./stan/model/model_base.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/rev/core.hpp
:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/rev/core/aut
odiffstackstorage.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/memory/stack
_alloc.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/meta.hp
p:5:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/arr/met
a/index_type.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/arr/met
a/is_vector.hpp:4:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/scal/meta/is_vector.hpp:23:26
: error:
      no template named 'decay_t' in namespace 'std'; did you mean 'decay'?
    : bool_constant<std::decay_t<T>::RowsAtCompileTime == 1> {};
                    ~~~~~^~~~~~~
                         decay
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:1349:29: note: 'decay' declared here
struct _LIBCPP_TEMPLATE_VIS decay
                            ^
#################################################
#  I'm omitting some lengthy error codes here   #
#  due to the post character count restriction, #
#  but I think what I've kept is representative #
#################################################

In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/meta.hp
p:5:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/arr/met
a/index_type.hpp:6:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/scal/meta/require_generics.hp
p:64:12: error:
      no template named 'enable_if_t' in namespace 'std'; did you mean 'enable_if'?
    = std::enable_if_t<!math::disjunction<Checks...>::value>;
      ~~~~~^~~~~~~~~~~
           enable_if
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:435:63: note: 'enable_if' declared here
template <bool, class _Tp = void> struct _LIBCPP_TEMPLATE_VIS enable_if {};
                                                              ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [Module.o] Error 1
ERROR: compilation failed for package ‘rstan’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan’
Error: Failed to install 'rstan' from GitHub:
  (converted from warning) installation of package ‘/var/folders/0n/5q18drsd4wndt5w9htm00j5c0000gn/T//RtmpD3uvk0/file104f263306
337/rstan_2.21.3.tar.gz’ had non-zero exit status

If I try install.packages("rstan", type="source") I get the exact same errors as above.

I would appreciate any assistance getting this sorted out. Thank you in advance.

Hi Christian,

Sorry that you’ve had these issues! It looks like the compiler isn’t enabling by default all of the functions/metaprograms that Stan uses, which is causing the compilation to fail.

Did you setup your compiler/toolchain using r-macos-rtools (link here)?

Hi Andrew, thanks for the suggestion – I know that once upon a time I had installed r-macos-rtools v3.2.2 but I see that it’s now on v4.0.0 so I just tried installing v4.0.0 from the link you shared. That seemed to install, but then when I install.packages("rstan", type="source") I get the same errors as I documented above in my installing from source section.

I thought that would have fixed it, but alas! Can you try adding the following to your Makevars:

CXXFLAGS +=  -std=c++1y
CXX14FLAGS +=  -std=c++1y

Then restart R and try the install again?

1 Like

Excellent! This worked for me.

2 Likes