Issue creating docker image which installs rstan

I have the following image:

FROM rocker/r-ver:4.1.2

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    apt-utils \
    ed \
    libnlopt-dev \
    && apt-get clean \
    && 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("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)'

After building the image with docker build -f Dockerfile -t docker_r_stan_test . (no errors output), then running: example(stan_model, package = "rstan", run.dontrun = TRUE) I'm expecting to see something similar to what's at: https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started#verify-installation

I'm instead getting the output:

Error in find.package(package, lib.loc, verbose = verbose) :
  there is no package called ‘rstan’

I have a similar error trying to use library(rstan):

> library("rstan")
Error in library("rstan") : there is no package called ‘rstan’

I don't understand why rstan isn't installed properly within this image, as it seems I've followed the steps closely


Using Rocker images to install rstan is indeed a good idea, and I demontrated it via (essentially) a single command in this older post on my blog.

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 not 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).

First example: rocker/r-ubuntu:20.04

This "simply" relies on the fact that rstan exists here as a binary package r-cran-rstan -- so we install it in one command after updating the apt index.

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] 

Second example: rocker/r-bspm:20.04

It gets even nicer thanks to bspm and its integration (and Inaki and I have an arXiv paper on this) because we can _use install.packages() (via a script) to fetch r-cran-rstan for us:

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 "install.packages" in package "utils"                                                   
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) -- "Bird Hippie"
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 "install.packages" in package "utils"
> 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)
> 

Note that we use install.r here -- a wrapper for install.packages() from littler -- to point at the R package yet we get the binary. And all its depends. In one command. I think I even posted an animated gif showing just this once ...

The key insight, though, is that none of this is limited to Docker. I am using the bspm approach on one small laptop where I don't want to compile from sources. It works flawlessly, and has for months.

Let us know here or on the r-sig-debian list if you have questions.

Edit: The animated gif I just created is too large to post it here (2mb limit) but fits for Twitter so here it is: https://twitter.com/eddelbuettel/status/1485318710818754567