Problem installing cmdstanr on Mac

I’m trying to install cmdrstanr onto a Mac but keep getting the error ‘sstream’ file not found. I’ve tried some of the fixes suggested elsewhere, like trying to install the GitHub version instead, but keep getting the same issue.

cmdstanr::install_cmdstan(overwrite = TRUE, cores=2)

I've also just run

install_cmdstan(overwrite = TRUE, cores=2)

I’m very new to R so apologies in advance. I think that my error messages are as follows:

In file included from src/cmdstan/main.cpp:1:
In file included from src/cmdstan/command.hpp:4:
In file included from src/cmdstan/arguments/arg_data.hpp:4:
In file included from src/cmdstan/arguments/categorical_argument.hpp:4:
In file included from src/cmdstan/arguments/argument.hpp:4:
In file included from stan/src/stan/callbacks/writer.hpp:4:
In file included from stan/lib/stan_math/stan/math/prim/fun/Eigen.hpp:22:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Dense:1:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Core:19:
stan/lib/stan_math/lib/eigen_3.4.0/Eigen/src/Core/util/Macros.h:679:10: fatal error: ‘cmath’ file not found
679 | #include
| ^~~~~~~

AND

In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/src/stan/model/model_base.hpp:7:
stan/src/stan/io/var_context.hpp:4:10: fatal error: ‘sstream’ file not found
4 | #include
| ^~~~~~~~~
1 error generated.
make: *** [stan/src/stan/model/model_header.hpp.gch/model_header_16_0.hpp.gch] Error 1
make: *** Waiting for unfinished jobs…
1 error generated.
make: *** [src/cmdstan/main.o] Error 1

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

I’m on a MacBook Air with the latest version of R.

This generally occurs when there are multiple C++ toolchains installed and the wrong toolchain header files are being detected. Have you installed GCC/Clang through Homebrew/Macports at some point?

No I’ve never installed GCC/Clang, at least as far as I’m aware.

When I run check_cmdstan_toolchain() it seems to think that the C++ toolchain required for CmdStan is setup properly, although I get that it wouldn’t know if it was detecting the wrong files.

Checked in terminal and I definitely don’t have GCC installed. These were the details for the version of clang:

Apple clang version 16.0.0 (clang-1600.0.26.6)

Target: arm64-apple-darwin24.1.0

Thread model: posix

InstalledDir: /Library/Developer/CommandLineTools/usr/bin

What output do you get from:

list.files("/usr/local/include")

character(0)

Ah well that rules one source of the errors. The next is a possible mismatch between MacOS version and XCode versions.

What version of MacOS are you using? What output do you get from:

list.files("/Library/Developer/CommandLineTools/SDKs/")

[1] “MacOSX.sdk” “MacOSX13.1.sdk” “MacOSX14.5.sdk” “MacOSX14.sdk” “MacOSX15.2.sdk”
[6] “MacOSX15.sdk”

I’m using MacOS Sequoia 15.1.1

Yep so you’ve got the 15.2 SDK installed but your MacOS version is only 15.1, which could be the issue. Are you able to upgrade to 15.2?

Apologies, that’s a rather silly mistake to have made! I’ve upgraded to 15.2, but I’m still getting an error message when trying to install cmdstan:

install_cmdstan(cores = 2, overwrite=TRUE)

In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/src/stan/model/model_base.hpp:7:
stan/src/stan/io/var_context.hpp:4:10: fatal error: ‘sstream’ file not found
4 | #include
| ^~~~~~~~~
1 error generated.
make: *** [stan/src/stan/model/model_header.hpp.gch/model_header_16_0.hpp.gch] Error 1
make: *** Waiting for unfinished jobs…
1 error generated.
make: *** [src/cmdstan/main.o] Error 1

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

In file included from src/cmdstan/main.cpp:1:
In file included from src/cmdstan/command.hpp:4:
In file included from src/cmdstan/arguments/arg_data.hpp:4:
In file included from src/cmdstan/arguments/categorical_argument.hpp:4:
In file included from src/cmdstan/arguments/argument.hpp:4:
In file included from stan/src/stan/callbacks/writer.hpp:4:
In file included from stan/lib/stan_math/stan/math/prim/fun/Eigen.hpp:22:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Dense:1:
In file included from stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Core:19:
stan/lib/stan_math/lib/eigen_3.4.0/Eigen/src/Core/util/Macros.h:679:10: fatal error: ‘cmath’ file not found
679 | #include
| ^~~~~~~

I’m having a nearly identical issue as @manateesloth documents above. Is there some suggested work-around to get cmdstan and cmdstanr to install? I have installed cmdstan a few years back, but have since updated my macOS.

Here is some output about my system:

list.files("/usr/local/include")
#> [1] "JAGS"
list.files("/Library/Developer/CommandLineTools/SDKs/")
#> [1] "MacOSX.sdk"     "MacOSX12.1.sdk" "MacOSX13.3.sdk" "MacOSX13.sdk"  
#> [5] "MacOSX14.4.sdk" "MacOSX14.5.sdk" "MacOSX14.sdk"   "MacOSX15.2.sdk"
#> [9] "MacOSX15.sdk"

system("clang++ --version", intern=TRUE)
#> [1] "Apple clang version 16.0.0 (clang-1600.0.26.6)"           
#> [2] "Target: arm64-apple-darwin24.2.0"                         
#> [3] "Thread model: posix"                                      
#> [4] "InstalledDir: /Library/Developer/CommandLineTools/usr/bin"
system("make --version", intern=TRUE)
#> [1] "GNU Make 3.81"                                                      
#> [2] "Copyright (C) 2006  Free Software Foundation, Inc."                 
#> [3] "This is free software; see the source for copying conditions."      
#> [4] "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A"
#> [5] "PARTICULAR PURPOSE."                                                
#> [6] ""                                                                   
#> [7] "This program built for i386-apple-darwin11.3.0"

Created on 2025-01-20 with reprex v2.1.1

Let’s check what include paths the compiler looks through, what output do you get from:

system("echo | clang++ -E -Wp,-v -")

Actually that’s only checking the C include paths, for the C++ include paths can you run:

system("echo | clang++ -E -x c++ -Wp,-v -", intern = TRUE)

Thanks for the continued trouble-shooting. Here’s what I get:

system("echo | clang++ -E -x c++ -Wp,-v -", intern = TRUE)
#> [1] "# 1 \"<stdin>\""          "# 1 \"<built-in>\" 1"    
#> [3] "# 1 \"<built-in>\" 3"     "# 439 \"<built-in>\" 3"  
#> [5] "# 1 \"<command line>\" 1" "# 1 \"<built-in>\" 2"    
#> [7] "# 1 \"<stdin>\" 2"        ""

Created on 2025-01-20 with reprex v2.1.1

I’m not sure how to interpret this or what it might mean for possible solutions.

Can you run it just in R/RStudio, not through reprex? It prints things that aren’t captured by reprex

Good point. Here you go:

> system("echo | clang++ -E -x c++ -Wp,-v -", intern = TRUE)
clang -cc1 version 16.0.0 (clang-1600.0.26.6) default target arm64-apple-darwin24.2.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/16/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
[1] "# 1 \"<stdin>\""          "# 1 \"<built-in>\" 1"    
[3] "# 1 \"<built-in>\" 3"     "# 439 \"<built-in>\" 3"  
[5] "# 1 \"<command line>\" 1" "# 1 \"<built-in>\" 2"    
[7] "# 1 \"<stdin>\" 2"        ""      

That’s interesting, the first search path is in /Library/Developer/CommandLineTools/usr/bin/ which isn’t right (the c++ headers are located in the SDK folder). That would explain why the headers aren’t being found - the toolchain is configured to look in the wrong directory

I think it might be a hold-over from the older SDKs on your system (12.1, etc). Can you try removing and reinstalling the commandline tools?

To do this, open up a terminal and delete the existing installation:

sudo rm -rf /Library/Developer/CommandLineTools

Then request the installation of the tools again:

xcode-select --install
1 Like

The uninstall and reinstall of the CommandLineTools seems to have worked! Thanks for the trouble-shooting.

1 Like