R brms compilation error - DLL initialization

Hello! I suddenly began running into an issue with running models using brms. The code below worked fine last week, but after updating my R packages this afternoon (04/12/21), I began receiving the following error when attempting to run any model with brms.

> library(brms)
Loading required package: Rcpp
Loading 'brms' package (version 2.15.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').

Attaching package: ‘brms’

The following object is masked from ‘package:stats’:


> prior1 <- prior(normal(0,10), class = b) +
+   prior(cauchy(0,2), class = sd)
> fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
+             data = epilepsy, family = poisson(), prior = prior1)
Compiling Stan program...
Error in inDL(x, as.logical(local), as.logical(now), ...) : 
  unable to load shared object 'C:/Users/JASON_~1/AppData/Local/Temp/RtmpKE1VuQ/file1fc44c4653fb.dll':
  LoadLibrary failure:  A dynamic link library (DLL) initialization routine failed.
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found
Error in sink(type = "output") : invalid connection

I have searched far and wide in terms of potential solutions (e.g., reinstalling relevant packages, uninstalling and reinstalling R and RStudio, etc.), but the error persists. I am able to run models in rstanarm with no issue.

I would greatly appreciate any guidance towards a solution.

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] brms_2.15.0 Rcpp_1.0.6 

loaded via a namespace (and not attached):
  [1] nlme_3.1-152         matrixStats_0.58.0   xts_0.12.1           threejs_0.3.3       
  [5] rstan_2.21.2         backports_1.2.1      tools_4.0.5          utf8_1.2.1          
  [9] R6_2.5.0             DT_0.17              DBI_1.1.1            mgcv_1.8-34         
 [13] projpred_2.0.2       colorspace_2.0-0     withr_2.4.1          tidyselect_1.1.0    
 [17] gridExtra_2.3        prettyunits_1.1.1    processx_3.5.1       Brobdingnag_1.2-6   
 [21] emmeans_1.5.5-1      curl_4.3             compiler_4.0.5       cli_2.4.0           
 [25] shinyjs_2.0.0        sandwich_3.0-0       colourpicker_1.1.0   scales_1.1.1        
 [29] dygraphs_1.1.1.6     mvtnorm_1.1-1        ggridges_0.5.3       callr_3.6.0         
 [33] StanHeaders_2.21.0-7 stringr_1.4.0        digest_0.6.27        minqa_1.2.4         
 [37] base64enc_0.1-3      pkgconfig_2.0.3      htmltools_0.5.1.1    lme4_1.1-26         
 [41] fastmap_1.1.0        htmlwidgets_1.5.3    rlang_0.4.10         shiny_1.6.0         
 [45] generics_0.1.0       jsonlite_1.7.2       zoo_1.8-9            crosstalk_1.1.1     
 [49] gtools_3.8.2         dplyr_1.0.5          inline_0.3.17        magrittr_2.0.1      
 [53] loo_2.4.1            bayesplot_1.8.0      Matrix_1.3-2         munsell_0.5.0       
 [57] fansi_0.4.2          abind_1.4-5          lifecycle_1.0.0      multcomp_1.4-16     
 [61] stringi_1.5.3        MASS_7.3-53.1        pkgbuild_1.2.0       plyr_1.8.6          
 [65] grid_4.0.5           parallel_4.0.5       promises_1.2.0.1     crayon_1.4.1        
 [69] miniUI_0.1.1.1       lattice_0.20-41      splines_4.0.5        ps_1.6.0            
 [73] pillar_1.5.1         igraph_1.2.6         boot_1.3-27          estimability_1.3    
 [77] markdown_1.1         shinystan_2.5.0      codetools_0.2-18     reshape2_1.4.4      
 [81] stats4_4.0.5         rstantools_2.1.1     glue_1.4.2           V8_3.4.0            
 [85] RcppParallel_5.1.1   vctrs_0.3.7          nloptr_1.2.2.2       httpuv_1.5.5        
 [89] gtable_0.3.0         purrr_0.3.4          assertthat_0.2.1     ggplot2_3.3.3       
 [93] mime_0.10            xtable_1.8-4         coda_0.19-4          later_1.1.0.1       
 [97] survival_3.2-10      rsconnect_0.8.17     tibble_3.1.0         shinythemes_1.2.0   
[101] gamm4_0.2-6          statmod_1.4.35       TH.data_1.0-10       ellipsis_0.3.1      
[105] bridgesampling_1.0-0

So, looking at recent posts here, the following fixed my issue.

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

Thanks for your time! >.>


THANK YOU! I had the same issue and spent several hours searching for an answer. Your solution worked.

Just be aware that this installs the preview of the next version of RStan, so you may run into bugs or unexpected behaviour

1 Like

Ah, thank you for pointing that out! I hope that another solution will present itself but at least I can continue now :-).

If you want to work with the current release of RStan, try:

remove.packages(c("StanHeaders", "rstan")) 

# Compile packages using all cores 
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))


Thank you @andrjohns!

Amazing thanks so much for the help - this fixed my problem!

Had the same issue and this worked for me too, thanks so much!