If there are libraries needed on the container, it is recommended to write a Dockerfile and build the image.
See the Dockerfile reference and the Dockerfile Best Practices for to write Dockerfiles. Also, please refer to the reference page of each Rocker image for links to the respective Dockerfile of Rocker image.
This section covers several topics specific to the installation of R packages into Docker containers.
Users can also preserve changes to Docker images that they have modified interactively using the docker commit mechanism.
While this is sometimes convenient, we encourage users to consider writing Dockerfiles instead whenever possible, as this creates a more transparent and reproducible mechanism to accomplish the same thing.
1 Install R packages on Linux
1.1 Install source packages
When installing R packages from CRAN (the official package repository for R) on Linux, source installation is performed. This differs from Windows and macOS.
Source installation may require system libraries needed to build the package.
As an example, here is a Dockerfile that installs the curl package from source on r-base, which has already set up to install R packages from the 0-Cloud CRAN mirror (Automatic redirection to servers worldwide).
FROM r-base:latest
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libcurl4-openssl-dev \
&& rm -rf /var/lib/apt/lists/* \
&& R -q -e 'install.packages("curl")'Before installing the curl package, you must install libcurl4-openssl-dev (libcurl) by using apt-get. If you try to source install the curl package without libcurl, the installation will fail because it cannot be built.
In general, system requirements can be found on error messages on installation failures, the package’s reference page, CRAN, or METACRAN. The rstudio/r-system-requirements repository and The dashboard of system libraries linked by R packages on R-universe are also useful.
In R, the pak::pkg_system_requirements() function can be used to find system requirements for a package.
pak::pkg_system_requirements("curl", "ubuntu", "20.04")
#> [1] "apt-get install -y libcurl4-openssl-dev"
#> [2] "apt-get install -y libssl-dev"On supported systems, installing packages using the functions provided by pak may automatically install dependencies.
$ R -q -e 'pak::pak("curl")'
> pak::pak("curl")
✔ Loading metadata database ... done
ℹ No downloads are needed
ℹ Installing system requirements
ℹ Executing `sudo sh -c apt-get install -y libcurl4-openssl-dev`
ℹ Executing `sudo sh -c apt-get install -y libssl-dev`
✔ 1 pkg: kept 1 [11.8s]Check the pak package documentation for details.
1.2 Install binary packages
Source installations generally take longer than binary installations, so there are several ways to install binary R packages on Linux.
1.2.1 Posit Public Package Manager
Posit Package Manager (Formerly “RStudio Package Manager”, RSPM) provides binary R packages for specific Linux distributions1. Since RSPM provides all packages on CRAN as a CRAN mirror, users can install packages just as if they were installed from CRAN.
For example, Ubuntu based image rocker/r-ver:4 on amd64 platform, which has already set up the public version of RSPM (Posit Public Package Manager) as its default CRAN mirror, can install the curl package as follows.
FROM rocker/r-ver:4
RUN R -q -e 'install.packages("curl")'Some packages (e.g. sf) will fail to load if the system requirements are not met when the package is attempted to be loaded. In such cases, the system libraries must be installed as in the case of source installation.
Please check FAQ for Posit Public Package Manager.
Binary installation from RSPM only supports amd64 now. So, R package installation on rocker/r-ver on arm64 platform will be source installation.
Binary-installed packages from RSPM are not stripped and may be larger in size. (rocker-org/rocker-versioned2#340)
Therefore, it may be possible to reduce the image size by stripping immediately after R package installation as follows.
FROM rocker/r-ver:4
RUN R -q -e 'install.packages("curl")' \
&& strip /usr/local/lib/R/site-library/*/libs/*.so1.2.2 System package management system
Some Linux distributions allow installation of binary R packages with the system package management system.
Since r-base and rocker/r-ubuntu are already configured to enable them, you can install R packages with the apt-get install command like this.
FROM r-base:latest
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
r-cran-curl \
&& rm -rf /var/lib/apt/lists/*This method can only be used if R is installed with the System Package Manager.
Do not use this method for rocker/r-ver or its derivative images as they have installed R from source.
Not all packages on CRAN are necessarily available.
r2u, provided by Rocker Project member @eddelbuettel, offers faster package installation on Ubuntu (amd64). Also, more CRAN packages and BioConductor packages are being registered. Please check the r2u document for details.
1.2.3 bspm
bspm provides functions to manage R packages via the distribution’s package manager from R.
For example, rocker/r-bsmp:testing (based on r-base) can install r-cran-curl with the install.packages() R function.
FROM rocker/r-bspm:testing
RUN R -q -e 'install.packages("curl")' \
&& rm -rf /var/lib/apt/lists/*1.2.4 conda-forge
If you use the conda command (or the mamba command) to install R, you can install the many binary R package from conda-forge.
Currently, the Rocker project has no mamba (or conda) based Docker images. So, use jupyter/base-notebook maintained by the Project Jupyter for example.
FROM jupyter/base-notebook:latest
RUN mamba install -y r-curl \
&& mamba clean -yafPackage versions may differ due to CPU architecture (amd64 and arm64). Please check package pages on conda-forge for details.
2 Helper commands
Rocker images provide a few utility functions to streamline this process, including the littler scripts which provide a concise syntax for installing packages in Dockerfiles.
2.1 install2.r
The install2.r command can be used to concisely describe the installation of the R package.
For example, a Dockerfile that installs multiple R packages can be written as follows.
FROM r-base:latest
RUN install2.r pkg1 pgk2 pkg3If the same content were written using the install.packages() function, it would be more complicated, as shown below.
FROM r-base:latest
RUN R -q -e 'install.packages(c("pkg1", "pkg2", "pkg3"))'If you install R packages from CRAN using the install2.r command, the temporary files are stored in /tmp/downloaded_packages directory. Therefore, it is recommended to delete /tmp/downloaded_packages at the end if you use this command in Dockerfiles.
FROM r-base:latest
RUN install2.r pkg1 pgk2 pkg3 \
&& rm -rf /tmp/downloaded_packages2.1.1 Options
The install2.r command also has several useful options.
2.1.1.1 -e, --error
By setting the --error option, you can make the docker build command also fail if the package installation fails.
2.1.1.2 -s, --skipinstalled
--skipinstalled option to skip installing installed packages.
2.1.1.3 -n, --ncpus
You can set --ncpu -1 to maximize parallelism of the installation.
2.1.1.4 -r, --repos
You can install R packages from specific repositories with this option. A special value --repos getOption means using R’s getOption("repos") value.
2.1.2 Examples
Use with r-base; Install source R packages from CRAN.
FROM r-base:latest
RUN install2.r --error --skipinstalled --ncpus -1 \
pkg1 \
pgk2 \
pkg3 \
&& rm -rf /tmp/downloaded_packagesUse with rocker/r-ver; Install binary R packages from RSPM.
FROM rocker/r-ver:4
RUN install2.r --error --skipinstalled --ncpus -1 \
pkg1 \
pgk2 \
pkg3 \
&& rm -rf /tmp/downloaded_packages \
&& strip /usr/local/lib/R/site-library/*/libs/*.soUse with rocker/r-ver; Install binary R packages from RSPM and source R packages from the R-universe ropensci repository.
FROM rocker/r-ver:4
RUN install2.r --error --skipinstalled --ncpus -1 \
--repos https://ropensci.r-universe.dev --repos getOption \
pkg1 \
pgk2 \
pkg3 \
&& rm -rf /tmp/downloaded_packages \
&& strip /usr/local/lib/R/site-library/*/libs/*.soUse with rocker/r-bspm; Install binary R packages from system package repository.
FROM rocker/r-bspm:testing
RUN install2.r --error --skipinstalled \
pkg1 \
pgk2 \
pkg3 \
&& rm -rf /var/lib/apt/lists/*If bspm is already set, some options have no effect because the system package manager is used to install the R packages.