# Rstan install - need to compile from source in order to see parser error messages

**URL:** <https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907>\
**Category:** Interfaces\
**Created:** [January 5, 2018, 4:25pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907 "2018-01-05T16:25:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 4:25pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/1 "2018-01-05T16:25:41Z")

</div>

Operating System: MacOS High Sierra  
Interface Version: rstan 2.17.2  
Compiler/Toolkit: clang/xcode

the downloaded rstan package can’t get compiler error messages from stanc.

I just upgraded to High Sierra (what’s in a name? celebrating legal pot in CA?).  
In RStudio, I upgraded all installed packages - this gave me RStan 2.17.2.  
Next I tried to compile a model with a simple syntax error:

```
Error in stanc(file = file, model_code = model_code, model_name = model_name, : 
  c++ exception (unknown reason)

```

the fix that worked for me is:

```
library(devtools)

```

this required a restart of the R session, then:

```
install.packages("rstan", type = "source")

```

Posting this for future reference - next I’ll check the RStan install page (because I don’t read the docs, I just write them).

cheers,  
Mitzi

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 4:56pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/2 "2018-01-05T16:56:06Z")

</div>

didn’t read the docs, or see this message in Ben’s announcement:

> The main change to rstan is that it should work with a Mac that uses the upstream version of clang (which CRAN now uses) rather than Xcode’s clang. It is necessary that the upstream clang be installed in /usr/local/clang4/bin/clang++. After that, there should be no more “unknown exception” messages.

because I don’t have clang4 installed at the prescribed path, I triggered this error. Will install clang4 and retest.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 7:07pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/3 "2018-01-05T19:07:22Z")

</div>

installed clang4, using this nifty installer:

> **[rmacoslib/r-macos-clang](https://github.com/rmacoslib/r-macos-clang)**
>
> Setup script to create the binary installer for the released clang binaries used to compile R - rmacoslib/r-macos-clang

still not working for me.  
cheers,  
Mitzi

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 5, 2018, 9:05pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/4 "2018-01-05T21:05:59Z")

</div>

Can you tell which part of

> <https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/R/cxxfunplus.R#L145>

  
is not working?

```
grepl("^darwin", R.version$os)
grepl("clang4", rstan:::get_CXX())
Sys.which("install_name_tool")
file.exists("/usr/local/clang4/lib/libc++.1.dylib")
file.exists("/usr/lib/libc++.1.dylib")

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 10:25pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/5 "2018-01-05T22:25:40Z")

</div>

all of them seem to be working -

> grepl(“^darwin”, R.version$os)  
> [1] TRUE  
> grepl(“clang4”, rstan:::get\_CXX())  
> [1] TRUE  
> Sys.which(“install\_name\_tool”)  
> install\_name\_tool  
> “/usr/bin/install\_name\_tool”  
> file.exists(“/usr/local/clang4/lib/libc++.1.dylib”)  
> [1] TRUE  
> file.exists(“/usr/lib/libc++.1.dylib”)  
> [1] TRUE

and can run clang4 from command line, FWIW:

/usr/local/clang4/bin/clang++ -v  
clang version 4.0.0 (tags/RELEASE\_400/final)  
Target: x86\_64-apple-darwin17.3.0  
Thread model: posix  
InstalledDir: /usr/local/clang4/bin

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 5, 2018, 11:11pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/6 "2018-01-05T23:11:04Z")

</div>

Is it just parser errors that do this or do syntactically correct models that occasionally throw numerical exceptions (like funnel) do this as well?

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [January 5, 2018, 11:13pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/7 "2018-01-05T23:13:45Z")

</div>

I’ve seen this just for parser errors. With the same version, I’ve seen rejections with appropriate messages.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 5, 2018, 11:22pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/8 "2018-01-05T23:22:44Z")

</div>

What if you do (in a clean R session)

```
  cmd <- paste(
    "install_name_tool",
    "-change",
    "/usr/local/clang4/lib/libc++.1.dylib",
    "/usr/lib/libc++.1.dylib",
    system.file("libs", "rstan.so", package = "rstan")
  )
  system(cmd)
  library(rstan)
  stanc("program_with_syntax_error.stan")

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 11:28pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/9 "2018-01-05T23:28:50Z")

</div>

in a fresh R session, trying to run a syntactically correct model - the funnel w/ centered parameterization and no data - `stan` command fails.

> niter=100;  
> nthin=1;  
> nchains=4;  
> fit\_cp\_0 = stan(file=“funnel\_cp.stan”, data=d\_0, iter=niter, thin=nthin, chains=nchains, save\_warmup=FALSE);

data exists, model compiles in cmdstan 2.17 - doesn’t run in R:

> In file included from file16b41811ba3.cpp:8:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model\_header.hpp:4:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hp p:4:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:4:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4:  
> In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/memory/stack\_alloc.hpp:8:  
> In file included from /usr/local/clang4/bin/…/include/c++/v1/cstdlib:86:  
> /usr/local/clang4/bin/…/include/c++/v1/stdlib.h:94:15: fatal error: ‘stdlib.h’ file not found  
> #include\_next \<stdlib.h\>

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 11:38pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/10 "2018-01-05T23:38:32Z")

</div>

> [@bgoodri](#):
>
> system(cmd)  
> library(rstan)

tried this - it didn’t work.  
corrected syntactically incorrect file and tried again w/ command `stan_model` - this fails to link the generated .cpp file - same set of error messages as below for the funnel model.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 5, 2018, 11:45pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/11 "2018-01-05T23:45:57Z")

</div>

checking what the installer did - it overwrites any existing ~/.R/Makevars file -

> cat .R/Makevars

> CC=/usr/local/clang4/bin/clang  
> CXX=/usr/local/clang4/bin/clang++  
> CXX1X=/usr/local/clang4/bin/clang++  
> CXX98=/usr/local/clang4/bin/clang++  
> CXX11=/usr/local/clang4/bin/clang++  
> CXX14=/usr/local/clang4/bin/clang++  
> CXX17=/usr/local/clang4/bin/clang++  
> LDFLAGS=-L/usr/local/clang4/lib

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 5, 2018, 11:50pm UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/12 "2018-01-05T23:50:39Z")

</div>

> [@mitzimorris](#):
>
> /usr/local/clang4/bin/…/include/c++/v1/stdlib.h

is trying to `#include_next <stdlib.h>`? WTF?

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 6, 2018, 12:18am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/13 "2018-01-06T00:18:56Z")

</div>

still having fun - trying to use xcode clang++ compiler.

blew away my `~/.R/Makevars` file, which was overwritten by the clang4 installer, and created the following Makevars file, per instructions on the RStan wiki for installs on Mac - current file is:

> cat .R/Makevars

> CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  
> CC=clang  
> CXX=clang++ -arch x86\_64 -ftemplate-depth-256

in a fresh RStudio session, try `stan_model` command for syntactially correct Stan program:

```no-highlight
> stan_model("hello.stan")
In file included from file196e5bbee5d6.cpp:8:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config/compiler/clang.hpp:200:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
# define BOOST_NO_CXX11_RVALUE_REFERENCES
          ^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
        ^
1 warning generated.

```

this works:

```no-highlight
> fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
+ ' )
> fx( 2L, 5 ) 
[1] 10

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 6, 2018, 12:51am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/14 "2018-01-06T00:51:55Z")

</div>

OK, this works:

file `~/.R/Makevars`

```no-highlight
CXXFLAGS=-O3 -mtune=native -march=native
CXXFLAGS= -Wno-macro-redefined -Wno-unused-variable -Wno-unused-function
CC=clang
CXX=clang++ -arch x86_64 -ftemplate-depth-256

```

as before, installed RStan from source:

> install.packages(“rstan”, type = “source”)

now getting parser error messages for syntax errors,  
and syntactically correct files compile and run, with warning messages.

One final fix required in RStudio session from command line:

> Sys.setenv(TZ=“America/New\_York”)

to get rid of stupid error messages:

> Warning messages:  
> 1: 1: In as.POSIXlt.POSIXct(x, tz) :In as.POSIXlt.POSIXct(x, tz) :  
> unknown timezone ‘default/America/New\_York’

The permafix for the TZ problem is to edit either your personal or site installation’s `Renviron` file to add whatever you want your default TZ to be.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 9, 2018, 2:28am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/15 "2018-01-09T02:28:49Z")

</div>

giving the clang4 install another go, but still seeing problems trying to run a simple valid model through `stan_model` - linker fails with message:

> /usr/local/clang4/bin/…/include/c++/v1/stdlib.h:94:15: fatal error: ‘stdlib.h’ file not found

similar to what was discussed in this thread:

> [@Error in rstan\_2.16.2 after upgrading to R 3.4.1 with Clang 4.0.0](http://discourse.mc-stan.org/t/error-in-rstan-2-16-2-after-upgrading-to-r-3-4-1-with-clang-4-0-0/1893/19):
>
> Or even just call Rcpp::sourceCpp() on a C++ file like this #include \<Rcpp.h\> using namespace Rcpp; // [[Rcpp::export]] int throw\_exception() { std::stringstream errmsg; errmsg \<\< "this is an exception"; throw std::domain\_error(errmsg.str()); return 0; }

I suspect the Makevars file created by that installer is missing stuff needed by rstan et al, but reading the R docs and that other thread I can’t figure out what those should be.

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [January 9, 2018, 3:33am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/16 "2018-01-09T03:33:08Z")

</div>

For what it may be worth, last week I was also having problems with RStan 2.17 swallowing parser warnings but I was able to get everything working using the vanilla Xcode toolkit (OS X 10.12.6, Xcode 9.2, Apple LLVM version 9.0.0 (clang-900.0.39.2)) by installing StanHeaders from source and then RStanArm 2.17.2 from source.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 9, 2018, 4:06am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/17 "2018-01-09T04:06:34Z")

</div>

interesting. so don’t have to install RStan from source, just the other two?  
do you need RStanArm from source if you’re only using RStan?

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 9, 2018, 4:18am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/18 "2018-01-09T04:18:53Z")

</div>

the problem remains that RStan users won’t see parser error messages without doing some kind of install from source and previously this wasn’t necessary, so existing users who upgrade will hit this error as well as any new users.

I’ve just done a pass as editing the RStan Wiki page: “Installing RStan on Mac or Linux”, but I didn’t just flat out say “you’re going to have to install from source” because maybe that’s too strong?

I did, however, update the section on crafting your Makevars file to the set of flags that seem to be necessary for make RStan compile without throwing lots of warnings.

Eyeballs would be most welcome! [https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Mac-or-Linux](https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Mac-or-Linux)

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [January 9, 2018, 4:25am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/19 "2018-01-09T04:25:36Z")

</div>

Sorry, autocomplete typo – I meant StanHeaders and then RStan. No RStanArm. One issue I noticed is that if you install RStan from source then there’s a clash with StanHeaders which is precompiled presumably using a different version of clang. Compiling both from source ensures that everything is compiled with the same toolkit.

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [October 2, 2018, 8:05am UTC](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907/20 "2018-10-02T08:05:20Z")

</div>

Hi,

I stumbled into this the other day (maybe my OS X Mojave installation did something?) After Mojave installation I downloaded newest XCode, and ran Coatless’ installer. I changed my Makevars from the Coatless’ installer default to this:

> [@mitzimorris](#):
>
> CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  
> CC=clang  
> CXX=clang++ -arch x86\_64 -ftemplate-depth-256

So I guess the above uses OS X’s clang only?

Then I ran:

```
install.packages("StanHeaders", repos = "https://cloud.r-project.org/", dependencies=TRUE, type="source")
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies=TRUE, type="source")

```

and it all seems to work :)

[Next page](https://discourse.mc-stan.org/t/rstan-install-need-to-compile-from-source-in-order-to-see-parser-error-messages/2907.md?page=2)
