Using renv with Continuous Integration

When building, deploying, or testing with continuous integration (CI) systems (e.g. GitHub Actions, Travis CI, AppVeyor, and others), one often needs to download and install a set of R packages before the service can be run. Normally, one will have to download and re-install these packages on each build, which can often be slow – especially in environments where binary packages are not available from your R package repositories.

renv can often be helpful in these situations. The general idea is:

  1. Call renv::snapshot() on your local machine, to generate renv.lock;

  2. Call renv::restore() on your CI service, to restore the project library from renv.lock;

  3. Ensure that the project library, as well as the global renv cache, are cached by the CI service.

Normally, renv will use the R package repositories as encoded in renv.lock during restore, and this will override any repositories set in other locations (e.g. in .Rprofile or We’ll discuss some strategies for providing an alternate R package repository to use during restore below.

GitHub Actions

When using GitHub Actions, you typically need two steps:

  1. Cache any packages installed by renv,
  2. Use renv::restore() to restore packages.

As an example, these steps might look like:

  RENV_PATHS_ROOT: ~/.local/share/renv


- name: Cache packages
  uses: actions/cache@v1
    path: ${{ env.RENV_PATHS_ROOT }}
    key: ${{ runner.os }}-renv-${{ hashFiles('**/renv.lock') }}
    restore-keys: |
      ${{ runner.os }}-renv-

- name: Restore packages
  shell: Rscript {0}
  run: |
    if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv")

See also the example on GitHub actions.

Travis CI

On Travis CI, one can add the following entries to .travis.yml to accomplish the above:

  - $HOME/.local/share/renv
  - $TRAVIS_BUILD_DIR/renv/library

  - Rscript -e "if (!requireNamespace('renv', quietly = TRUE)) install.packages('renv')"
  - Rscript -e "renv::restore()"
  - Rscript -e '<your-build-action>'

Note that we provide both install and script steps, as we want to override the default behaviors provided by Travis for R (which might attempt to install different version of R packages than what is currently encoded in renv.lock). See for more details.

It’s also possible to override the package repository used during restore by setting the RENV_CONFIG_REPOS_OVERRIDE environment variable. For example:


replacing <cran> with your desired R package repository. This can also be accomplished in a similar way by setting:

options(renv.config.repos.override = <...>)

but it is generally more ergonomic to set the associated environment variable. (See ?config for more details.) This can be useful if you’d like to, for example, enforce the usage of a MRAN checkpoint during restore, or another similarly-equipped repository.

See for more details on how Travis manages caching.

GitLab CI

The following template can be used as a base when using renv with GitLab CI:


  key: ${CI_JOB_NAME}

  - < ... other pre-deploy steps ... >
  - Rscript -e "if (!requireNamespace('renv', quietly = TRUE)) install.packages('renv')"
  - Rscript -e "renv::restore()"