Robert's Data Science Blog

Parallel Installation of R Packages

Installing R packages on Linux is time consuming if the packages include compiled code – which is the case for a lot of contemporary packages.

To speed things up we can use the argument Ncpus for install.packages:

install.packages("here", Ncpus = 4)

The optional arguments for devtools::install and remotes::install_* are passed on to install.packages, so Ncpus can be used here as well.

The command detectCores from the standard library package parallel can detect the number of available (logical) cores.

In my Docker images with R I now use Ncpus and experience a great speedup. The number used in Ncpus can be set with a build argument called NCORES.

If NCORES is specified it is available as an environment variable with a value during the build. If it is not specified the environment variable is an empty string.

I use this behavior to set a default number if NCORES is not specified:

Sys.getenv("NCORES", unset = parallel::detectCores())