Installation of cmdstanR MacOs Tahoe

Hello,

Short summary of the problem

I am trying to use the cmdstanR interface to CmdStan on a M1 mac (macOS Tahoe). I am able to use cmdstan from the terminal :

[edelaire1@home-imglab01:~/.cmdstan/cmdstan-2.38.0/examples/bernoulli]$ ./bernoulli sample\
>   data file=bernoulli.data.json 
method = sample (Default)
  sample
    num_samples = 1000 (Default)
    num_warmup = 1000 (Default)
    save_warmup = false (Default)
    thin = 1 (Default)
    adapt
      engaged = true (Default)
      gamma = 0.05 (Default)
      delta = 0.8 (Default)
      kappa = 0.75 (Default)
      t0 = 10 (Default)
      init_buffer = 75 (Default)
      term_buffer = 50 (Default)
      window = 25 (Default)
      save_metric = false (Default)
    algorithm = hmc (Default)
      hmc
        engine = nuts (Default)
          nuts
            max_depth = 10 (Default)
        metric = diag_e (Default)
        metric_file =  (Default)
        stepsize = 1 (Default)
        stepsize_jitter = 0 (Default)
    num_chains = 1 (Default)
id = 1 (Default)
data
  file = bernoulli.data.json
init = 2 (Default)
random
  seed = 593756500 (Default)
output
  file = output.csv (Default)
  diagnostic_file =  (Default)
  refresh = 100 (Default)
  sig_figs = 8 (Default)
  profile_file = profile.csv (Default)
  save_cmdstan_config = false (Default)
num_threads = 1 (Default)


Gradient evaluation took 1e-06 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.01 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 2000 [  0%]  (Warmup)
Iteration:  100 / 2000 [  5%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  300 / 2000 [ 15%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  500 / 2000 [ 25%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  700 / 2000 [ 35%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  900 / 2000 [ 45%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1100 / 2000 [ 55%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1300 / 2000 [ 65%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1500 / 2000 [ 75%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1700 / 2000 [ 85%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1900 / 2000 [ 95%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 0.003 seconds (Warm-up)
               0.005 seconds (Sampling)
               0.008 seconds (Total)

But it fails when called from RStudio (Version 2026.01.1+403, R version 4.5.3):

Chain 1 dyld[19146]: Library not loaded: @rpath/libtbb.12.dylib
Chain 1   Referenced from: <FB21BB72-06DB-3B16-913C-A4A70C56E763> /Users/edelaire1/.cmdstan/cmdstan-2.38.0/examples/bernoulli/bernoulli
Chain 1   Reason: tried: '/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/Library/Frameworks/R.framework/Resources/lib/libtbb.12.dylib' (no such file), '/Library/Java/JavaVirtualMachines/jdk-11.0.18+10/Contents/Home/lib/server/libtbb.12.dylib' (no such file), '/var/folders/rj/qpt02tfx4zlgjdyxvr878g1w0000gp/T/rstudio-fallback-library-path-660936210/libtbb.12.dylib' (no such file)

Please provide this additional information in addition to your question:

  • Operating System: macOS Tahoe
  • CmdStan Version: 2.38.0
  • Compiler/Toolkit: Not sure

I installed cmdstanr using:

install.packages("remotes")
remotes::install_github("stan-dev/cmdstanr")


install_cmdstan(cores = 8)

Thanks a lot

Edouard

Hmm, that’s strange. You said you can run cmdstan from the terminal but not cmdstanr from RStudio. How about cmdstanr from the terminal? Can you try opening R in the terminal instead of RStudio and running the cmdstanr model you tried via cmdstanr (not just cmdstan)? Does that give you the same error? It would be helpful to know if this is isolated to RStudio or not.

(@WardBrian seems like another TBB issue, although this time on Mac)

You said you can run cmdstan from the terminal but not cmdstanr from RStudio.

Yes, that’s right.

How about cmdstanr from the terminal? Can you try opening R in the terminal instead of RStudio and running the cmdstanr model you tried via cmdstanr (not just cmdstan)?

I have the same error. (see image: Left is via R and cmdstan R, right is via terminal and cmdstan)

So i tried to delete my .bash_profile file and then i get the same error in the terminal as in R, or R studio:

home-imglab01:bernoulli edelaire1$ ./bernoulli sample data file=bernoulli.data.json 
dyld[43658]: Symbol not found: __ZN3tbb10interface78internal15task_arena_base18internal_terminateEv
  Referenced from: <FB21BB72-06DB-3B16-913C-A4A70C56E763> /Users/edelaire1/.cmdstan/cmdstan-2.38.0/examples/bernoulli/bernoulli
  Expected in:     <3B0492D7-487F-34B2-8406-7C6F5834B66C> /opt/homebrew/Cellar/tbb/2022.2.0/lib/libtbb.12.16.dylib
Abort trap: 6
home-imglab01:bernoulli edelaire1$ 

I guess the main line related to the issue in the .bash_profile is the following:

export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/opt/homebrew/opt/tbb/lib

I put the entire file here: gist:c6125b1bf4e555dd795a22ed34d17a72 · GitHub

Edit: I confirm that just the inclusion/removal of

export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/opt/homebrew/opt/tbb/lib

Either remove or cause the error mentioned before. But I guess that this line is only 'executed’ when running directly from the terminal for some reason.

Edit 2: If I run the following code in R, then it’s working:

 Sys.setenv( DYLD_LIBRARY_PATH  = "/Users/edelaire1/VulkanSDK/1.3.275.0/macOS/lib/:/Applications/freesurfer/8.0.0/lib/gcc/lib:/Applications/MATLAB/MATLAB_Runtime/R2023b/runtime/maca64:/Applications/MATLAB/MATLAB_Runtime/R2023b/bin/maca64:/Applications/MATLAB/MATLAB_Runtime/R2023b/sys/osmaca64:/Applications/MATLAB/MATLAB_Runtime/R2023b/extern/bin/maca64:/opt/homebrew/opt/tbb/lib:/opt/X11/lib/flat_namespace:/opt/X11/lib/flat_namespace")

Is there a way to have this run automatically when opening rstudio ?

2 Likes

Yeah, you should be able to use the .Rprofile file for this.

Given that our CI is passing on macOs-26 (Tahoe) and you’re now seeing the same issue from the terminal outside of RStudio, I would say that this isn’t a general cmdstanr/Tahoe bug. From what you posted I think because you’re setting a large DYLD_LIBRARY_PATH that includes Homebrew’s TBB that must be affecting which TBB libraries CmdStan picks up at runtime. (@WardBrian would know better than I do if that assessment is correct, but that seems likely to me.)

I’m not sure if this would work, but you could also try to reinstall cmdstan from a clean R session with DYLD_LIBRARY_PATH unset, e.g.

Sys.unsetenv("DYLD_LIBRARY_PATH")
cmdstanr::install_cmdstan(version = "2.38.0", overwrite = TRUE)

That might fix the underlying problem so that you don’t need to add anything to your .Rprofile file. But it’s also possible that if your .bash_profile stays the same you might still pick up the wrong TBB, I don’t know.

As a fallback you can use the workaround that you found and add it to .Rprofile.

1 Like

I agree with @jonah’s assessment. Having another copy of TBB in your environment is fine on macOS, but you either need to have it every time (meaning when you install, and when you build the model, and when you sample the model) or you should avoid having it at all these points in time

If you did want cmdstan to use the home brew version, there is some make/local logic you can set that would avoid needing to fiddle with DL_LIBRARY_PATH

2 Likes

hi.

or you should avoid having it at all these points in time

This seems fair. I think it was set-up by some application i no longuer used. I cleanup my bash_profile to avoid all those inclusuon to DYLD_LIBRARY_PATH:

$ echo $DYLD_LIBRARY_PATH
/Applications/freesurfer/8.0.0/lib/gcc/lib

$ ls $DYLD_LIBRARY_PATH
libgcc_s.1.dylib	libgomp.1.dylib		libstdc++.6.dylib
libgfortran.3.dylib	libquadmath.0.dylib

So now, i can’t use cmdstan from both terminal or R. (note, i recompiled cmdstan from R as suggested by @jonah :

Sys.unsetenv(“DYLD_LIBRARY_PATH”)
cmdstanr::install_cmdstan(version = “2.38.0”, overwrite = TRUE)

[edelaire1@home-imglab01:~/.cmdstan/cmdstan-2.38.0/examples/bernoulli]$ ./bernoulli sample data file=bernoulli.data.json 
dyld[32505]: Library not loaded: @rpath/libtbb.12.dylib
  Referenced from: <3C351C14-BB2C-3AF8-9B38-3C67822792FE> /Users/edelaire1/.cmdstan/cmdstan-2.38.0/examples/bernoulli/bernoulli
  Reason: tried: '/Applications/freesurfer/8.0.0/lib/gcc/lib/libtbb.12.dylib' (no such file), '/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/edelaire1/.cmdstan/cmdstan-2.38.0/stan/lib/stan_math/lib/tbb/libtbb.12.dylib' (no such file)

I guess, I still need to let cmdstan know where is the tbb version included in cmdstan ?

Besides re-installing cmdstan, did you re- compile bernoulli?

yes. I recompiled it.

ChatGpt suggested that i run:

install_name_tool -change @rpath/libtbb.12.dylib \
/opt/homebrew/opt/tbb/lib/libtbb.12.dylib \
examples/bernoulli/bernoulli  

or

install_name_tool -add_rpath /opt/homebrew/opt/tbb/lib ~/.cmdstan/cmdstan-2.38.0/examples/bernoulli/bernoulli

After that, I could sample from the model in R and from the terminal. But i guess that’s not really the solution as it’s need to be executed after compiling every model.

Before that, it asked me to run : otool -L examples/bernoulli/bernoulli which return :

home-imglab01:cmdstan-2.38.0 edelaire1$ otool -L examples/bernoulli/bernoulli
examples/bernoulli/bernoulli:
	@rpath/libtbb.12.dylib (compatibility version 12.0.0, current version 12.12.0)
	@rpath/libtbb.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libtbbmalloc.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libtbbmalloc_proxy.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 2000.67.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1356.0.0)

Note: to make it less confusing, i removed everything i had in .bash_profile and .bashrc so we know there is no variable set.

Finally, ChatGPT told me to add this to the local makefile:

LDLIBS_TBB += -Wl,-rpath,/opt/homebrew/opt/tbb/lib

Is it ok? I guess this is the solution you mentioned using the homebrew version of tbb. Is it ok ? Or do I risk having some issue later?

R is fussy so I just use cmdstan and then upload the samples to R, and then use R stan. it’s too cumbersome. But I don’t mind working with command line, I prefer it. I’m using WSL and it’s like a VM for ubuntu on windows, but may switch back to Ubuntu. But managing R packages, apparently, is always tricky. I haven’t done any R packages. Stan is cool, but you get a lot of stuff developed by a grad student that doesn’t work. For linear modeling it’s cool, but for the niche stuff, meh. It’s also a slow programming language.

That’s probably fins, the rpath should be being set by our makefiles but something in your environment must still be overloading it.

The “official” way to use a custom TBB is to set TBB_LIB to the desired location, which should be more or less equivalent to what you have done. If it works, I would probably just leave it

2 Likes