Install R packages using docker file Install R packages using docker file r r

Install R packages using docker file


As suggested by @Cameron Kerr's comment, Rscript does not give you a build failure.As of now, the recommended way is to do as the question suggests.

RUN R -e "install.packages('methods',dependencies=TRUE, repos='http://cran.rstudio.com/')"RUN R -e "install.packages('jsonlite',dependencies=TRUE, repos='http://cran.rstudio.com/')"RUN R -e "install.packages('tseries',dependencies=TRUE, repos='http://cran.rstudio.com/')" 

If you're fairly certain of no package failures then use this one-liner -

RUN R -e "install.packages(c('methods', 'jsonlite', 'tseries'),                           dependencies=TRUE,                            repos='http://cran.rstudio.com/')"

EDIT: If you're don't use the Base-R image, you can use rocker-org's r-ver or r-studio or tidyverse images. Here's the repo. Here's an example Dockerfile -

FROM rocker/tidyverse:latest# Install R packagesRUN install2.r --error \    methods \    jsonlite \    tseries

The --error flag is optional, it makes install.packages() throw an error if the package installation fails (which will cause the docker build command to fail). By default, install.packages() only throws a warning, which means that a Dockerfile can build successfully even if it has failed to install the package.

All rocker-org's basically installs the littler package for the install2.R functionality


Yes, your solution should work. I came across the same problem and found the solution here https://github.com/glamp/r-docker/blob/master/Dockerfile.

In short, use: RUN Rscript -e "install.packages('PACKAGENAME')". I have tried it and it works.

As others have mentioned in the comments, this solution will not raise an error if the package could not be installed.


This is ugly but it works - see below for real world example of why it's worth doing.

# install packages and check installation success, install.packages itself does not report failsRUN R -e "install.packages('RMySQL');     if (!library(RMySQL, logical.return=T)) quit(status=10)" \ && R -e "install.packages('devtools');   if (!library(devtools, logical.return=T)) quit(status=10)" \ && R -e "install.packages('data.table'); if (!library(data.table, logical.return=T)) quit(status=10)" \ && R -e "install.packages('purrr');      if (!library(purrr, logical.return=T)) quit(status=10)" \ && R -e "install.packages('tidyr');      if (!library(tidyr, logical.return=T)) quit(status=10)"

Real world example: devtools install starts failing because it suddenly needs libgit2-dev. install.packages() prints informative info. about the failure, but without a non-zero exit code, that just scrolls away as docker build continues.