Error when Configuring RStan C++ toolchain

Hello! I am a new user to RStan and I ran into an issue when configuring the C++ toolchain and subsequent loading of the RStan package.

I am using Windows 10, R x64 v.4.0.2 and RStudio v.1.3.1073. I followed these instructions to the letter in R and RStudio: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started. Below are the commands I used an abbreviated version of the output highlighting the error.

> install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
ERROR: compilation failed for package 'jsonlite'
* removing 'C:/Users/Allison/Documents/R/win-library/4.0/jsonlite'
Warning in install.packages :
  installation of package ā€˜jsonliteā€™ had non-zero exit status
The downloaded source packages are in
ā€˜C:\Users\Allison\AppData\Local\Temp\RtmpAnUOQm\downloaded_packagesā€™
> pkgbuild::has_build_tools(debug = TRUE)
>library("rstan") 
Loading required package: StanHeaders
Loading required package: ggplot2
Error: package or namespace load failed for ā€˜rstanā€™ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 there is no package called ā€˜jsonliteā€™

Any insights on would be helpful.
Thank you!
Allison

Can you try running:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

and restart the R ression and then try installing rstan again.

Still the same error.
Error: package or namespace load failed for ā€˜rstanā€™ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ā€˜jsonliteā€™

Can you run

install.packages(ā€œjsonliteā€)

This is what I get when I try installing just the jsonlite package.

ERROR: compilation failed for package ā€˜jsonliteā€™

  • removing ā€˜C:/Users/Allison/Documents/R/win-library/4.0/jsonliteā€™

The downloaded source packages are in
ā€˜C:\Users\Allison\AppData\Local\Temp\Rtmpu2NfgF\downloaded_packagesā€™
Warning message:
In install.packages(ā€œjsonliteā€) :
installation of package ā€˜jsonliteā€™ had non-zero exit status

Here is the complete output in case it helps.

install.packages(ā€œjsonliteā€)
Installing package into ā€˜C:/Users/Allison/Documents/R/win-library/4.0ā€™
(as ā€˜libā€™ is unspecified)
ā€” Please select a CRAN mirror for use in this session ā€”

There is a binary version available but the source version is later:
binary source needs_compilation
jsonlite 1.7.0 1.7.1 TRUE

installing the source package ā€˜jsonliteā€™

trying URL ā€˜https://cran.case.edu/src/contrib/jsonlite_1.7.1.tar.gzā€™
Content type ā€˜application/x-gzipā€™ length 1057786 bytes (1.0 MB)
downloaded 1.0 MB

  • installing source package ā€˜jsonliteā€™ ā€¦
    ** package ā€˜jsonliteā€™ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/Rtools/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
C:/Rtools/mingw_32/bin/gcc: not found
make: *** [base64.o] Error 127
ERROR: compilation failed for package ā€˜jsonliteā€™

  • removing ā€˜C:/Users/Allison/Documents/R/win-library/4.0/jsonliteā€™

The downloaded source packages are in
ā€˜C:\Users\Allison\AppData\Local\Temp\Rtmpu2NfgF\downloaded_packagesā€™
Warning message:
In install.packages(ā€œjsonliteā€) :
installation of package ā€˜jsonliteā€™ had non-zero exit status

What does Sys.getenv(ā€œRTOOLS40_HOMEā€) return?

Sys.getenv(ā€œRTOOLS40_HOMEā€)
[1] ā€œā€

Oh ok, in this case you do not have RTools 4.0 installed.

See: https://cran.r-project.org/bin/windows/Rtools/

I followed the instructions to the link you sent and I get a compilation error for the jsonlite package.

Sys.which(ā€œmakeā€)
make
ā€œC:\rtools40\usr\bin\make.exeā€
install.packages(ā€œjsonliteā€, type = ā€œsourceā€)
Installing package into ā€˜C:/Users/Allison/Documents/R/win-library/4.0ā€™
(as ā€˜libā€™ is unspecified)
trying URL ā€˜https://cran.rstudio.com/src/contrib/jsonlite_1.7.1.tar.gzā€™
Content type ā€˜application/x-gzipā€™ length 1057786 bytes (1.0 MB)
downloaded 1.0 MB

  • installing source package ā€˜jsonliteā€™ ā€¦
    ** package ā€˜jsonliteā€™ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/Rtools/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
sh: C:/Rtools/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/i386/Makeconf:222: base64.o] Error 127
ERROR: compilation failed for package ā€˜jsonliteā€™

  • removing ā€˜C:/Users/Allison/Documents/R/win-library/4.0/jsonliteā€™
    Warning in install.packages :
    installation of package ā€˜jsonliteā€™ had non-zero exit status

The downloaded source packages are in
ā€˜C:\Users\Allison\AppData\Local\Temp\RtmpyI6z2r\downloaded_packagesā€™

Hi Allison,

That means that R is still configured to look for the old RTools installation. To help figure out what needs to be changed, can you post the outputs from:

Sys.getenv("BINPREF")
Sys.getenv("PATH")
readLines("~/.Renviron")
readLines("~/.Rprofile")
readLines("~/.R/Makevars.win")

Sys.getenv(ā€œBINPREFā€)
[1] ā€œC:/Rtools/mingw_$(WIN)/bin/ā€

Sys.getenv(ā€œPATHā€)
[1] ā€œC:\rtools40\usr\bin;C:\Program Files\R\R-4.0.2\bin\x64;c:\Rtools\bin;c:\Rtools\mingw_64\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\mpirt;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\compiler;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64\;C:\MATLAB701\bin\win32;C:\Program Files\MATLAB\R2017b\runtime\win64;C:\Program Files\MATLAB\R2017b\bin;C:\Program Files (x86)\Kensington\TrackballWorks;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;ā€

readLines(ā€œ~/.Renvironā€)
[1] "PATH=${RTOOLS40_HOME}\usr\bin;${PATH}"

readLines(ā€œ~/.Rprofileā€)
[1] ā€œSys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")ā€

readLines(ā€œ~/.R/Makevars.winā€)
[1] ā€œā€
[2] ā€œCXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2ā€
[3] ā€œā€
[4] ā€œCXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2ā€
[5] ā€œā€
[6] ā€œCXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2ā€
[7] ā€œā€
[8] ā€œCXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2ā€

Great! Three steps to take here:

  • In your Documents folder there will a file called .Rprofile, delete that
  • In your Documents folder there will be a .R folder, in that will be a Makevars.win file. Open that in a text editor and delete all but one of the CXX14FLAGS lines
  • Restart R and try your model again
1 Like

Hi There!

I have a similar problem. I have R version 4.0.4 with Rstudio version 1.4.1106 (both updated today). I have been trying to get the installation from source to work, but I keep getting the same error messages. I have already done all the steps from this thread, including numerous steps from other threads to get jsonlite to install. I get the following error message:

install.packages(ā€œjsonliteā€, type = ā€œsourceā€)
Installing package into ā€˜C:/Users/laura/OneDrive/Documenten/R/win-library/4.0ā€™
(as ā€˜libā€™ is unspecified)
trying URL ā€˜http://cran.rstudio.com/src/contrib/jsonlite_1.7.2.tar.gzā€™
Content type ā€˜application/x-gzipā€™ length 421716 bytes (411 KB)
downloaded 411 KB

  • installing source package ā€˜jsonliteā€™ ā€¦
    ** package ā€˜jsonliteā€™ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs

*** arch - i386
C:/rtools40/usr/mingw_32/bin/gcc -I"C:/PROGRA~1/R/R-40~1.4/include" -DNDEBUG -Iyajl/api -D__USE_MINGW_ANSI_STDIO -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c base64.c -o base64.o
sh: C:/rtools40/usr/mingw_32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.4/etc/i386/Makeconf:222: base64.o] Error 127
ERROR: compilation failed for package ā€˜jsonliteā€™

  • removing ā€˜C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/jsonliteā€™
  • restoring previous ā€˜C:/Users/laura/OneDrive/Documenten/R/win-library/4.0/jsonliteā€™
    Warning in install.packages :
    installation of package ā€˜jsonliteā€™ had non-zero exit status

The downloaded source packages are in
ā€˜C:\Users\laura\AppData\Local\Temp\Rtmpctpahf\downloaded_packagesā€™

Btw, my output to the code metioned above is as following:

Sys.getenv(ā€œBINPREFā€)
[1] ā€œC:/rtools40/usr/mingw_$(WIN)/bin/ā€

Sys.getenv(ā€œPATHā€)
[1] ā€œC:\rtools40\usr\bin;C:\Program Files\R\R-4.0.4\bin\x64;C:\Python27\Lib\site-packages\PyQt4;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\MATLAB\R2019a\bin;C:\Python27;C:\Python27\DLLs;C:\Python27\Scripts;C:\Python27\gnuplot\binary;C:\Program Files (x86)\pythonxy\SciTE-3.5.1-4;C:\Program Files (x86)\pythonxy\console;C:\Users\laura\AppData\Roaming\TinyTeX\bin\win32;C:\Users\laura\AppData\Local\Microsoft\WindowsApps;C:\Users\laura\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64ā€
readLines(ā€œ~/.Renvironā€)
[1] ā€œPATH="{RTOOLS40_HOME}\\usr\\bin;{PATH}"ā€
readLines(ā€œ~/.Rprofileā€)
[1] ā€œSys.setenv(BINPREF = "C:/rtools40/usr/mingw_$(WIN)/bin/")ā€
readLines(ā€œ~/.R/Makevars.winā€)
[1] ā€œā€
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"

Hi Laura,

The issue is caused by your setting of BINPREF in your .Rprofile file. See the RStan Getting Started Instructions for information on how to fix this.

Hi there,

Thank you for your quick reply! I had already deleted my Rprofile file before, but there was a new one now. I again deleted it, as was in the instructions. The problem is still there though. Do you have any idea how I can fix it?

What do you get from:

Sys.getenv("BINPREF")
readLines("~/.Rprofile")

The first one gives:

Sys.getenv(ā€œBINPREFā€)
[1] ā€œā€

And the second one:

readLines("~/.Rprofile")
Error in file(con, ā€œrā€) : cannot open the connection
In addition: Warning message:
In file(con, ā€œrā€) :
cannot open file ā€˜C:/Users/laura/OneDrive/Documenten/.Rprofileā€™: No such file or directory

If you restart your R session and run the example model:

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

What error do you get? If youā€™re still getting an error now that the .Rprofile file has been deleted then there is a different issue to be fixed