<p>Using Rocker images to install <code>rstan</code> is indeed a good idea, and I demontrated it via (essentially) a single command in <a href="http://dirk.eddelbuettel.com/blog/2017/12/22#014_finding_binary_deb_packages" rel="nofollow noreferrer">this older post</a> on my blog.</p>
<p>Your best bet, really, is to rely on Rocker containers already set up for the c2d4u.team repo.  And, as @Oliver noted in a comment to your question, those are <em>not</em> the r-ver images which (for their own valid reason) follow a different internal model (which makes them less ideal for the binaries we show in use here).</p>
<h4>First example: rocker/r-ubuntu:20.04</h4>
<p>This &quot;simply&quot; relies on the fact that <code>rstan</code> exists here as a binary package <code>r-cran-rstan</code> -- so we install it in one command after updating the <code>apt</code> index.</p>
<pre><code>edd@rob:~$ docker run --rm -ti rocker/r-ubuntu:20.04 bash
root@11e89aea64f6:/# apt update -qq
85 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@11e89aea64f6:/# apt install r-cran-rstan
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpng-tools libtbb2 pandoc pandoc-data r-cran-backports r-cran-bh r-cran-brio r-cran-callr
  r-cran-checkmate r-cran-cli r-cran-colorspace r-cran-cpp11 r-cran-crayon r-cran-data.table
  r-cran-desc r-cran-diffobj r-cran-digest r-cran-ellipsis r-cran-evaluate r-cran-fansi r-cran-farver
  r-cran-fastmatch r-cran-ggplot2 r-cran-glue r-cran-gridextra r-cran-gtable r-cran-inline
  r-cran-isoband r-cran-jsonlite r-cran-labeling r-cran-lifecycle r-cran-loo r-cran-magrittr
  r-cran-matrixstats r-cran-munsell r-cran-pillar r-cran-pkgbuild r-cran-pkgconfig r-cran-pkgload
  r-cran-praise r-cran-prettyunits r-cran-processx r-cran-ps r-cran-r6 r-cran-rcolorbrewer r-cran-rcpp
  r-cran-rcppeigen r-cran-rcppparallel r-cran-rematch2 r-cran-rlang r-cran-rprojroot r-cran-rstudioapi
  r-cran-scales r-cran-stanheaders r-cran-svglite r-cran-systemfonts r-cran-testthat r-cran-tibble
  r-cran-utf8 r-cran-vctrs r-cran-viridislite r-cran-waldo r-cran-withr
Suggested packages:
  texlive-latex-recommended texlive-xetex texlive-luatex pandoc-citeproc texlive-latex-extra context
  wkhtmltopdf librsvg2-bin groff ghc nodejs php python ruby libjs-mathjax node-katex r-cran-devtools
  r-cran-knitr r-cran-rmarkdown r-cran-tinytest r-cran-covr
The following NEW packages will be installed:
  libpng-tools libtbb2 pandoc pandoc-data r-cran-backports r-cran-bh r-cran-brio r-cran-callr
  r-cran-checkmate r-cran-cli r-cran-colorspace r-cran-cpp11 r-cran-crayon r-cran-data.table
  r-cran-desc r-cran-diffobj r-cran-digest r-cran-ellipsis r-cran-evaluate r-cran-fansi r-cran-farver
  r-cran-fastmatch r-cran-ggplot2 r-cran-glue r-cran-gridextra r-cran-gtable r-cran-inline
  r-cran-isoband r-cran-jsonlite r-cran-labeling r-cran-lifecycle r-cran-loo r-cran-magrittr
  r-cran-matrixstats r-cran-munsell r-cran-pillar r-cran-pkgbuild r-cran-pkgconfig r-cran-pkgload
  r-cran-praise r-cran-prettyunits r-cran-processx r-cran-ps r-cran-r6 r-cran-rcolorbrewer r-cran-rcpp
  r-cran-rcppeigen r-cran-rcppparallel r-cran-rematch2 r-cran-rlang r-cran-rprojroot r-cran-rstan
  r-cran-rstudioapi r-cran-scales r-cran-stanheaders r-cran-svglite r-cran-systemfonts r-cran-testthat
  r-cran-tibble r-cran-utf8 r-cran-vctrs r-cran-viridislite r-cran-waldo r-cran-withr
0 upgraded, 64 newly installed, 0 to remove and 85 not upgraded.
Need to get 60.7 MB of archives.
After this operation, 345 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
</code></pre>
<h4>Second example: rocker/r-bspm:20.04</h4>
<p>It gets even nicer thanks to <a href="https://cloud.r-project.org/package=bspm" rel="nofollow noreferrer">bspm</a>  and its integration (and Inaki and I have an <a href="https://arxiv.org/abs/2103.08069" rel="nofollow noreferrer">arXiv paper on this</a>) because we can _use <code>install.packages()</code> (via a script) to fetch <code>r-cran-rstan</code> for us:</p>
<pre><code>edd@rob:~$ docker run --rm -ti rocker/r-bspm:20.04 bash                                                  
root@ef07add5e9e7:/# apt update -qq
28 packages can be upgraded. Run 'apt list --upgradable' to see them.                                    
root@ef07add5e9e7:/# install.r rstan
(loaded the methods namespace)                                                                           
Loading required package: utils                                                                          Tracing function &quot;install.packages&quot; in package &quot;utils&quot;                                                   
Install system packages as root...
Reading package lists... Done
Building dependency tree         
Reading state information... Done
Hit http://archive.ubuntu.com/ubuntu focal InRelease                                                     
Hit http://security.ubuntu.com/ubuntu focal-security InRelease                                          
Hit http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal InRelease                                
Hit http://archive.ubuntu.com/ubuntu focal-updates InRelease                                            
Hit http://archive.ubuntu.com/ubuntu focal-backports InRelease                                          
Hit http://ppa.launchpad.net/edd/r-4.0/ubuntu focal InRelease                                           
Hit http://ppa.launchpad.net/marutter/rrutter4.0/ubuntu focal InRelease                                 
Fetched 0 B in 0s (0 B/s)                                                                                
Reading package lists... Done    
Building dependency tree           
Reading state information... Done
Get:1 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal/main amd64 r-cran-backports amd64 1.4.1-
1cran1.2004.0 [94.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libpng-tools amd64 1.6.37-2 [26.1 kB]           
Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 pandoc-data all 2.5-3build2 [76.0 kB]       
Get:4 http://archive.ubuntu.com/ubuntu focal/universe amd64 pandoc amd64 2.5-3build2 [15.4 MB] 
[... stuff omitted here for brevity ...]
Setting up r-cran-stanheaders (2.21.0-7-1cran1.2004.0) ...
Setting up r-cran-callr (3.7.0-1cran1.2004.0) ...
Setting up r-cran-tibble (3.1.6-1cran1.2004.0) ...
Setting up r-cran-pkgbuild (1.3.1-1cran1.2004.0) ...
Setting up r-cran-ggplot2 (3.3.5-1cran1.2004.0) ...
Setting up r-cran-rstan (2.21.3-1cran1.2004.0) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
root@d3045995c945:/# R

R version 4.1.2 (2021-11-01) -- &quot;Bird Hippie&quot;
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Loading required package: utils
Tracing function &quot;install.packages&quot; in package &quot;utils&quot;
&gt; library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
&gt; 
</code></pre>
<p>Note that we use <code>install.r</code> here -- a wrapper for <code>install.packages()</code> from <a href="https://cloud.r-project.org/package=littler" rel="nofollow noreferrer">littler</a> -- to point at the R package yet we get the binary. And <em>all</em>  its depends.  In one command. I think I even posted an animated gif showing just this once ...</p>
<p>The key insight, though, is that none of this is limited to Docker. I am using the <code>bspm</code> approach on one small laptop where I don't want to compile from sources.  It works flawlessly, and has for months.</p>
<p>Let us know here or on the <code>r-sig-debian</code> list if you have questions.</p>
<p><em>Edit:</em> The animated gif I just created is too large to post it here (2mb limit) but fits for Twitter so here it is:  <a href="https://twitter.com/eddelbuettel/status/1485318710818754567" rel="nofollow noreferrer">https://twitter.com/eddelbuettel/status/1485318710818754567</a></p>
Issue creating docker image which installs rstan
<p>I have the following image:</p>
<pre><code>FROM rocker/r-ver:4.1.2

RUN apt-get update \
    &amp;&amp; apt-get install -y --no-install-recommends \
    apt-utils \
    ed \
    libnlopt-dev \
    &amp;&amp; apt-get clean \
    &amp;&amp; rm -rf /var/lib/apt/lists/


RUN apt-get update 
RUN apt install software-properties-common  -y

# https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux
RUN add-apt-repository -y ppa:marutter/rrutter4.0
RUN add-apt-repository -y ppa:c2d4u.team/c2d4u4.0+
RUN apt-get update
RUN apt install -y r-cran-rstan


# https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started#installation-of-rstan
RUN Rscript -e 'Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1); install.packages(&quot;rstan&quot;, repos = &quot;https://cloud.r-project.org/&quot;, dependencies = TRUE)'
</code></pre>
<p>After building the image with <code>docker build -f Dockerfile -t docker_r_stan_test .</code> (no errors output), then running: <code>example(stan_model, package = &quot;rstan&quot;, run.dontrun = TRUE)</code> I'm expecting to see something similar to what's at: <a href="https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started#verify-installation" rel="nofollow noreferrer">https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started#verify-installation</a></p>
<p>I'm instead getting the output:</p>
<pre><code>Error in find.package(package, lib.loc, verbose = verbose) :
  there is no package called ‘rstan’
</code></pre>
<p>I have a similar error trying to use <code>library(rstan)</code>:</p>
<pre><code>&gt; library(&quot;rstan&quot;)
Error in library(&quot;rstan&quot;) : there is no package called ‘rstan’
</code></pre>
<p>I don't understand why <code>rstan</code> isn't installed properly within this image, as it seems I've followed the steps closely</p>