Issues installing StanHeaders, rstan on a MacBook M1

Hi

I'm really hoping someone can help. I am having problems running models in brms, and realised that rstan isn't installed properly. I totally uninstalled r and rstudio, and reinstalled it

platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          3.1                         
year           2023                        
month          06                          
day            16                          
svn rev        84548                       
language       R                           
version.string R version 4.3.1 (2023-06-16)
nickname       Beagle Scouts
mode
[1] "desktop"

$version
[1] ‘2023.6.1.524’

$long_version
[1] "2023.06.1+524"

$release_name
[1] "Mountain Hydrangea"

I try to install rstan:


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

Warning in install.packages :
  unable to access index for repository https://mc-stan.org/r-packages/bin/macosx/big-sur-arm64/contrib/4.3:
  cannot open URL 'https://mc-stan.org/r-packages/bin/macosx/big-sur-arm64/contrib/4.3/PACKAGES'
trying URL 'http://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.3/rstan_2.21.8.tgz'
Content type 'application/x-gzip' length 21545569 bytes (20.5 MB)
==================================================
downloaded 20.5 MB


The downloaded binary packages are in
	/var/folders/vd/pb0tbmfn17j_spybl3qmrzrw0000gn/T//Rtmp9mxjNx/downloaded_packages

I have tried:

  1. I totally uninstalled r and rstudio, and reinstalled it . No change
    2 .uncheck the configuration option: Global option -> Packages->Use secure download method for HTTP.No change
  2. I tried example models
    example(stan_model, package = "rstan", run.dontrun = TRUE)
    which gives a popup: > R Install Build Tools, Building R package from source requires installation of additional build tools.

Do you want to install the additional tools now?
If I click 'yes' I get:


Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directoryusing SDK: ‘’/bin/sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directorymake: *** [file34bf4883bfe4.o] Error 127
>
  1. remove and reinstall packages
remove.packages(c("StanHeaders", "rstan"))

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

I get the same errors.

  1. Where is stanheaders stored? I run:
    system.file("include", "src", "stan", "model", "model_header.hpp", package = "StanHeaders")
    [1] "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp"

  2. What is the compilation argument (suggested on a different site)

example(stan_model, package = "rstan", run.dontrun = TRUE)

gives:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directoryusing SDK: ‘’/bin/sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directorymake: *** [file37de8f7c8f6.o] Error 127
  1. I uninstalled and reinstalled Xcode. I have version 14.3.1

8 Check g++ in the terminal:

$g++ --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I don't understand what is happening and would be so grateful for any help!

Your first try with (essentially)

install.packages("rstan")

seemed to work (never mind the warning). What's wrong with that? Does it not work properly?

install.packages("rstan")
library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.8, 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)

Then I try to run any brms model and I get the pop up

Building R package from source requires installation of additional build tools.

Do you want to install the additional tools now?

I click 'yes' and get:


Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
sh: /usr/local/Cellar/gcc/7.2.0/bin/gcc-7: No such file or directory
using SDK: ‘’
/usr/local/Cellar/gcc/7.2.0/bin/gcc-7 -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
/bin/sh: /usr/local/Cellar/gcc/7.2.0/bin/gcc-7: No such file or directory
make: *** [foo.o] Error 127
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' -f '/Users/gabriellakountourid/.R/Makevars' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='file4a37e1deff6.so' OBJECTS='file4a37e1deff6.o'

make would use
/usr/local/Cellar/gcc/7.2.0/bin/g++-7 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.3-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 file4a37e1deff6.cpp -o file4a37e1deff6.o
if test  "zfile4a37e1deff6.o" != "z"; then \
	  echo /usr/local/Cellar/gcc/7.2.0/bin/g++-7 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/Cellar/gcc/7.2.0/lib -o file4a37e1deff6.so file4a37e1deff6.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  /usr/local/Cellar/gcc/7.2.0/bin/g++-7 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/Cellar/gcc/7.2.0/lib -o file4a37e1deff6.so file4a37e1deff6.o  '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/lib/' -ltbb  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directoryusing SDK: ‘’/bin/sh: /usr/local/Cellar/gcc/7.2.0/bin/g++-7: No such file or directorymake: *** [file4a37e1deff6.o] Error 127

so something is not working

This means that you have gcc installed from homebrew, but seems like it is a broken installation. Run

brew list | grep gcc

to see which gcc homebrew package(s) you have installed. If you don't really need them (you probably don't, surely not for R), then you can remove them with

brew uninstall <package>

Also make sure that you have Xcode or the Xcode command line tools installed, that's the compiler that you want.

Thanks for your reply. I did as you suggested:

gabriellakountourid🐰 ~ $brew list | grep gcc

gcc

gabriellakountourid🐰 ~ $brew uninstall gcc

Error: Refusing to uninstall /opt/homebrew/Cellar/gcc/13.1.0

because it is required by lapack and openblas, which are currently installed.

You can override this and force removal with:

brew uninstall --ignore-dependencies gcc

I'm not sure what the next step is? Should I ignore dependencies? I"m not familiar with using the terminal

I ignored dependencies, uninstalled and still no luck with my code in r.

I think I have Xcode installed. How can I check this is the compiler being used? Thanks!

Type in

gcc --version

or

$(R CMD config CC) --version
$(R CMD config CXX) --version

into a terminal window, and see what you get.

But you can also just try using rstan again, to see if it works now.

gcc --version

Apple clang version 14.0.3 (clang-1403.0.22.14.1)

Target: arm64-apple-darwin22.5.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

my brms model does not work. Same error

This looks good.

Have you restarted R? What do you get if you run

Sys.which("gcc")

in R?

What do you get if you run

system("$(R CMD config CC) --version")

from R?

Yes, each time I do something I restart r.

I ran both your suggestions in R (not the terminal, I assume you'll say if its in the terminal I should try :slight_smile: )

> Sys.which("gcc")
           gcc 
"/usr/bin/gcc"

system("$(R CMD config CC) --version")
sh: /usr/local/opt/llvm/bin/clang: No such file or directory
Warning message:
In system("$(R CMD config CC) --version") : error in running command

Unfortunately your compiler setup seems broken. What is

ls -l /usr/bin/gcc

from a terminal?

What is

which -a gcc

from a terminal?


$ls -l /usr/bin/gcc

-rwxr-xr-x 76 root wheel 167136 15 Jun 11:08 **/usr/bin/gcc**
$which -a gcc
/usr/bin/gcc

Any ideas how to fix my complier set up? :smiling_face_with_tear:

That looks good so far! Do you have anything in ~/.R/Makevars? Run

cat ~/.R/Makevars

from a terminal.


gabriellakountourid🐰 ~ $cat ~/.R/Makevars

CC=/usr/local/opt/llvm/bin/clang

CPP=/usr/local/opt/llvm/bin/clang

CXX=/usr/local/opt/llvm/bin/clang++

CXX11=/usr/local/opt/llvm/bin/clang++

CXX14=/usr/local/opt/llvm/bin/clang++

LDFLAGS=-L/usr/local/opt/icu4c/lib -L/usr/local/opt/libxml2/lib -L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/libpng/lib/ -L/usr/local/opt/gettext/lib -L/usr/local/opt/llvm/lib -L/usr/local/opt/openblas/lib -L/usr/local/opt/lapack/lib -L/usr/local/opt/tcl-tk/lib -L/usr/local/opt/udunits/lib/ -L/usr/local/opt/expat/lib -L/usr/local/opt/krb5/lib -L/usr/local/opt/libpq/lib -L/usr/local/opt/qt@5/lib

CPPFLAGS=-I/usr/local/opt/icu4c/include -I/usr/local/opt/libxml2/include -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/libpng/include/ -I/usr/local/opt/gettext/include -I/usr/local/opt/llvm/include -I/usr/local/opt/openblas/include -I/usr/local/opt/lapack/include -I/usr/local/opt/tcl-tk/include -I/usr/local/opt/udunits/include/ -I/usr/local/opt/expat/include -I/usr/local/opt/krb5/include -I/usr/local/opt/libpq/include -I/usr/local/opt/qt@5/include

# STD libraries

CXX1XSTD=-std=c++0x

CXX11STD=-std=c++11

CXX14STD=-std=c++14

CXX17STD=-std=c++17

#https://gist.github.com/btskinner/98f0501ceb21e1c8e7ff9dbaa5bf7b96#file-makevars

# Flags for OpenMP support that should allow packages that want to use

# OpenMP to do so (data.table), and other packages that bork with

# -fopenmp flag (stringi) to be left alone

SHLIB_OPENMP_CFLAGS=-fopenmp

SHLIB_OPENMP_CXXFLAGS=-fopenmp

SHLIB_OPENMP_CXX98FLAGS=-fopenmp

SHLIB_OPENMP_CXX11FLAGS=-fopenmp

SHLIB_OPENMP_CXX14FLAGS=-fopenmp

SHLIB_OPENMP_CXX17FLAGS=-fopenmp

SHLIB_OPENMP_FCFLAGS=-fopenmp

SHLIB_OPENMP_FFLAGS=-fopenmp

That seems like a compiler setup for an older version of R.

You can set it aside with

mv ~/.R/Makevars ~/.R/Makevars.bak

and then chances are that rstan will be working. Or you'll at least get a different error.

IT IS WORKING!!!! THANK YOU SO MUCH!!!!!!!
If you have time, I would love to understand what you asked me to do, and how it fixed it, so if this happens again I will be better at trouble shooting.

You installed a custom compiler at some point, but its setup (in ~/.R/Makevars) was broken for some reason that is impossible to tell now.

By removing that file now you are back to the default compilers in Xcode.

3 Likes

This also solved a similar issue I have had for weeks. Many thanks Gabor!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.