No compiler found on M1 MacOS Ventura when running simple stan code

Short summary of the problem
Updated my MacOS a few weeks ago, and now when I try to run the 8 schools example, I see that R cannot find a compiler for C (also have the same issue with C++ compiler when running the simple Rcpp example of https://blog.thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/index.html).

Issue related to https://discourse.mc-stan.org/t/cant-compile-simple-stan-code-on-m1-macos-ventura/32277, but different (I ran all the commands in that post and get the same output). I understood from reading that post that it might have to do with the fact that R was not looking in the right place for the compiler (computer was actually pointing to llvm installed with homebrew [what I was getting from running the clang -v command], but changed it in the .zshrc file to use the Command Line Tools compiler), but it seems it is not working, and at this point, I am at the end of my knowledge. So any help from the community would be much appreciated

Below is the output I get

library(rstan)
schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
fit <- stan(file = '~/Desktop/schools.stan', data = schools_dat)

Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘NA’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.c -o foo.o
make: *** [foo.o] Killed: 9
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' -f '/Users/jean-sebastien/.R/Makevars' CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB='file9be15a1bb46.so' OBJECTS='file9be15a1bb46.o'

make would use
clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2   -c file9be15a1bb46.cpp -o file9be15a1bb46.o
if test  "zfile9be15a1bb46.o" != "z"; then \
	  echo clang++ -arch arm64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -o file9be15a1bb46.so file9be15a1bb46.o  '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch arm64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -o file9be15a1bb46.so file9be15a1bb46.o  '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  using C++ compiler: ‘NA’
using C++17
using SDK: ‘MacOSX13.3.sdk’
make: *** [file9be15a1bb46.o] Killed: 9
Error in sink(type = "output") : invalid connection

Session info also below.

devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.2 (2024-10-31)
 os       macOS Ventura 13.7.2
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Ljubljana
 date     2025-02-24
 rstudio  2024.12.1+563 Kousa Dogwood (desktop)
 pandoc   2.18 @ /opt/homebrew/bin/pandoc
 quarto   1.5.57 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────────────────────
 package       * version  date (UTC) lib source
 base64enc       0.1-3    2015-07-28 [1] CRAN (R 4.4.1)
 cachem          1.1.0    2024-05-16 [1] CRAN (R 4.4.1)
 callr           3.7.6    2024-03-25 [1] CRAN (R 4.4.0)
 cli             3.6.4    2025-02-13 [1] CRAN (R 4.4.1)
 codetools       0.2-20   2024-03-31 [1] CRAN (R 4.4.2)
 colorspace      2.1-1    2024-07-26 [1] CRAN (R 4.4.1)
 devtools        2.4.5    2022-10-11 [1] CRAN (R 4.4.0)
 digest          0.6.37   2024-08-19 [1] CRAN (R 4.4.1)
 dplyr         * 1.1.4    2023-11-17 [1] CRAN (R 4.4.0)
 ellipsis        0.3.2    2021-04-29 [1] CRAN (R 4.4.1)
 evaluate        1.0.3    2025-01-10 [1] CRAN (R 4.4.1)
 fastmap         1.2.0    2024-05-15 [1] CRAN (R 4.4.1)
 forcats       * 1.0.0    2023-01-29 [1] CRAN (R 4.4.0)
 fs              1.6.5    2024-10-30 [1] CRAN (R 4.4.1)
 generics        0.1.3    2022-07-05 [1] CRAN (R 4.4.1)
 ggplot2       * 3.5.1    2024-04-23 [1] CRAN (R 4.4.0)
 glue            1.8.0    2024-09-30 [1] CRAN (R 4.4.1)
 gridExtra       2.3      2017-09-09 [1] CRAN (R 4.4.1)
 gtable          0.3.6    2024-10-25 [1] CRAN (R 4.4.1)
 hms             1.1.3    2023-03-21 [1] CRAN (R 4.4.0)
 htmltools       0.5.8.1  2024-04-04 [1] CRAN (R 4.4.1)
 htmlwidgets     1.6.4    2023-12-06 [1] CRAN (R 4.4.0)
 httpuv          1.6.15   2024-03-26 [1] CRAN (R 4.4.0)
 inline          0.3.21   2025-01-09 [1] CRAN (R 4.4.1)
 jsonlite        1.8.9    2024-09-20 [1] CRAN (R 4.4.1)
 knitr           1.49     2024-11-08 [1] CRAN (R 4.4.1)
 later           1.4.1    2024-11-27 [1] CRAN (R 4.4.1)
 lifecycle       1.0.4    2023-11-07 [1] CRAN (R 4.4.1)
 loo             2.8.0    2024-07-03 [1] CRAN (R 4.4.0)
 lubridate     * 1.9.4    2024-12-08 [1] CRAN (R 4.4.1)
 magrittr        2.0.3    2022-03-30 [1] CRAN (R 4.4.1)
 matrixStats     1.5.0    2025-01-07 [1] CRAN (R 4.4.1)
 memoise         2.0.1    2021-11-26 [1] CRAN (R 4.4.0)
 mime            0.12     2021-09-28 [1] CRAN (R 4.4.1)
 miniUI          0.1.1.1  2018-05-18 [1] CRAN (R 4.4.0)
 munsell         0.5.1    2024-04-01 [1] CRAN (R 4.4.1)
 pillar          1.10.1   2025-01-07 [1] CRAN (R 4.4.1)
 pkgbuild        1.4.6    2025-01-16 [1] CRAN (R 4.4.1)
 pkgconfig       2.0.3    2019-09-22 [1] CRAN (R 4.4.1)
 pkgload         1.4.0    2024-06-28 [1] CRAN (R 4.4.0)
 processx        3.8.5    2025-01-08 [1] CRAN (R 4.4.1)
 profvis         0.4.0    2024-09-20 [1] CRAN (R 4.4.1)
 promises        1.3.2    2024-11-28 [1] CRAN (R 4.4.1)
 ps              1.8.1    2024-10-28 [1] CRAN (R 4.4.1)
 purrr         * 1.0.4    2025-02-05 [1] CRAN (R 4.4.1)
 QuickJSR        1.5.1    2025-01-08 [1] CRAN (R 4.4.1)
 R6              2.6.1    2025-02-15 [1] CRAN (R 4.4.1)
 Rcpp            1.0.14   2025-01-12 [1] CRAN (R 4.4.1)
 RcppArmadillo   14.2.3-1 2025-02-06 [1] CRAN (R 4.4.1)
 RcppParallel    5.1.10   2025-01-24 [1] CRAN (R 4.4.1)
 readr         * 2.1.5    2024-01-10 [1] CRAN (R 4.4.0)
 remotes         2.5.0    2024-03-17 [1] CRAN (R 4.4.1)
 repr            1.1.7    2024-03-22 [1] CRAN (R 4.4.0)
 rlang           1.1.5    2025-01-17 [1] CRAN (R 4.4.1)
 rstan         * 2.32.6   2024-03-05 [1] CRAN (R 4.4.1)
 rstudioapi      0.17.1   2024-10-22 [1] CRAN (R 4.4.1)
 scales          1.3.0    2023-11-28 [1] CRAN (R 4.4.0)
 sessioninfo     1.2.3    2025-02-05 [1] CRAN (R 4.4.1)
 shiny           1.10.0   2024-12-14 [1] CRAN (R 4.4.1)
 skimr         * 2.1.5    2022-12-23 [1] CRAN (R 4.4.0)
 StanHeaders   * 2.32.10  2024-07-15 [1] CRAN (R 4.4.1)
 stringi         1.8.4    2024-05-06 [1] CRAN (R 4.4.1)
 stringr       * 1.5.1    2023-11-14 [1] CRAN (R 4.4.0)
 tibble        * 3.2.1    2023-03-20 [1] CRAN (R 4.4.0)
 tidyr         * 1.3.1    2024-01-24 [1] CRAN (R 4.4.1)
 tidyselect      1.2.1    2024-03-11 [1] CRAN (R 4.4.0)
 tidyverse     * 2.0.0    2023-02-22 [1] CRAN (R 4.4.0)
 timechange      0.3.0    2024-01-18 [1] CRAN (R 4.4.1)
 tzdb            0.4.0    2023-05-12 [1] CRAN (R 4.4.0)
 urlchecker      1.0.1    2021-11-30 [1] CRAN (R 4.4.1)
 usethis         3.1.0    2024-11-26 [1] CRAN (R 4.4.1)
 vctrs           0.6.5    2023-12-01 [1] CRAN (R 4.4.0)
 withr           3.0.2    2024-10-28 [1] CRAN (R 4.4.1)
 xfun            0.50     2025-01-07 [1] CRAN (R 4.4.1)
 xtable          1.8-4    2019-04-21 [1] CRAN (R 4.4.1)

This looks like a broader R/toolchain configuration issue, rather than anything Stan-specific.

Are you able to install any R packages from source? For example:

install.packages("jsonlite", type = "source")

And what output do you get from:

pkgbuild::check_build_tools(debug = TRUE)

It seems like I am not. Again, R cannot find the compiler. Below is the output from both commands

install.packages("jsonlite", type = "source")
trying URL 'https://cran.rstudio.com/src/contrib/jsonlite_1.9.0.tar.gz'
Content type 'application/x-gzip' length 1055532 bytes (1.0 MB)
==================================================
downloaded 1.0 MB

* installing *source* package ‘jsonlite’ ...
** package ‘jsonlite’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘NA’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Iyajl/api  -I/opt/R/arm64/include     -fPIC  -falign-functions=64 -Wall -g -O2  -c base64.c -o base64.o
make: *** [base64.o] Killed: 9
ERROR: compilation failed for package ‘jsonlite’
* removing ‘/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/jsonlite’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/jsonlite’
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status

The downloaded source packages are in
	‘/private/var/folders/n3/kq93_6sd4j54gxcvccnsrhzm0000gn/T/Rtmpmyo3gD/downloaded_packages’
pkgbuild::check_build_tools(debug = TRUE)
Trying to compile a simple C file
Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
using C compiler: ‘NA’
using SDK: ‘MacOSX13.3.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.c -o foo.o
make: *** [foo.o] Killed: 9
Error: Could not find tools necessary to compile a package
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.

OK… so I realized that the PATH from within RStudio was different than when I was using R from console. Changed that and now pkgbuild::check_build_tools(debug = TRUE) returns TRUE, but I get this error message when I try to run the 8schools example

make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' -f '/Users/jean-sebastien/.R/Makevars' CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB='file13f3919ff83b5.so' OBJECTS='file13f3919ff83b5.o'

make would use
clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2   -c file13f3919ff83b5.cpp -o file13f3919ff83b5.o
if test  "zfile13f3919ff83b5.o" != "z"; then \
	  echo clang++ -arch arm64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o file13f3919ff83b5.so file13f3919ff83b5.o  '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -arch arm64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L"/Library/Frameworks/R.framework/Resources/lib" -L/opt/R/arm64/lib -o file13f3919ff83b5.so file13f3919ff83b5.o  '/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib/' -ltbb -L/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/RcppParallel/lib -ltbb -ltbbmalloc   -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  /Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/stan/model/indexing.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/stan/model/indexing/rvalue_varmat.hpp:7:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/stan/model/indexing/rvalue.hpp:516:22: warning: unused variable 'rows' [-Wunused-variable]
  const Eigen::Index rows = row_idx.ns_.size();
                     ^
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/StanHeaders/include/src/stan/model/indexing/rvalue.hpp:517:22: warning: unused variable 'cols' [-Wunused-variable]
  const Eigen::Index cols = col_idx.ns_.size();
                     ^
11 warnings and 1 error generated.
make: *** [file13f3919ff83b5.o
Error in sink(type = "output") : invalid connection

Any idea of what’s broken? Sorry to ask, but this is really beyond my knowledge.