Unable to Run Stan Model in R v. 4.3.1

I am attempting to run my Stan model via rstan::stan() in the latest version of R (4.3.1), but get the error

Error in dyn.load(libLFile) : 
unable to load shared object   '/var/folders/r4/xm5blbcd2tn06tjv00lm1c780000gn/T//RtmprSdSql/file95c3479bd57.so':
dlopen(/var/folders/r4/xm5blbcd2tn06tjv00lm1c780000gn/T//RtmprSdSql/file95c3479bd57.so, 0x0006): Library not loaded: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib
Referenced from: <00AA8820-6959-3AC7-8CEC-5A3DFEF89645> /private/var/folders/r4/xm5blbcd2tn06tjv00lm1c780000gn/T/RtmprSdSql/file95c3479bd57.so
Reason: tried: '/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib' (no such file), '/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libR.dylib' (no such file), '/usr/local/lib/libR.dylib' (no such file), '/usr/lib/libR.dylib' (no such file, not in dyld cache)

I’m running on macOS Ventura 13.2 (Apple M2 Pro chip), so selected the appropriate R binary from CRAN (R-4.3.1-arm64.pkg).

Has anyone experienced this issue and knows a solution?

Sorry you’re running into trouble.

It’s a bit weird because the R version referenced in those error messages is 4.2, e.g.

but you said you were running 4.3, right?

@andrjohns recently added binaries for R 4.3 for the development version of RStan at Repository for distributing (some) stan-dev R packages | r-packages. Maybe that would work:

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

If that doesn’t work then maybe @andrjohns has another idea (he’s better at debugging install issues than I am), or alternatively you could try CmdStanR instead of RStan to run Stan from R.

Hi @jphill01,

I had the same issue with rstan not working on my Mac (Intel) with R 4.3.1.

I got it working though by removing the rstan and StanHeaders packages as well as uninstalling R 4.3.1 from my system. A bit of an overkill but I also deleted the “R.framework” folder located through the path Library/Frameworks/.

I then installed R 4.2.3.pkg (for Intel Mac). Also, I made sure the following applications were installed: XQuartz and XCode.

Through RStudio, I installed the latest development version of rstan i.e., 2.26.22 (avoid 2.21.1!) with the following code chunks and tested it which worked!

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

# code to test if it installed successfully
example(stan_model, package = "rstan", run.dontrun = TRUE)

# you will see a pile of gibberish in rconsole window
# if  objects 'fit', 'fit2', 'mod' and 'stancode' appears in environments then rstan package installed successfully!

It’s a bit of a downgrade but perhaps you can try using R 4.2.3. that’s compatible with Mac (M1/2) for now and see if this resolves the problem. Hopefully, it does I’m quite curious to know.

1 Like

@jonah

Thanks.

Yes, I recently updated R and the issue occurred.

CmdStanR works as an alternative for now since it’s bare bones. In the meantime, I’ll give your solution a try.

1 Like