An introduction to renv

Managing package dependencies in R projects

November 2026
Nicolas Casajus ORCID
Senior data scientist
FRB-CESAB

Introduction

Context

These practices aim to make your code and data more reproducible.

  But what about the computational environment? More specifically:

Are my package dependencies reproducible?

What makes package management challenging?

  • Packages evolve over time, with frequent version updates
  • Code may break when upgrading to a new major version
  • Packages come from different repositories (CRAN, GitHub, R-universe, etc.)
  • Only one version of a given package can be installed in a standard R library
  • Packages often depend on other packages, resulting in complex dependency trees

Key questions

Portability
Will my project run on another computer?

Long-term reproducibility
Will my project still run in the future?

Isolation
How can I manage different package versions across projects?

The renv package

# Install 'renv' package ----
pak::pak("renv")

Function Description
init() Initialize renv in a project
status() Check lockfile–project library consistency
snapshot() Record current state of project library in lockfile
restore() Restore project library from a lockfile
install() Install packages in the project library
remove() Remove packages from the project library
deactivate() Temporary deactivate renv for the project
activate() (Re)activate renv in the project

For more information

Package vs. Library

Package
A structured collection of functions,
documentation, data, and tests

Library
A directory where R packages
are installed and stored

  A package is what you install. A library is where you install it

# Available libraries on Linux ----
.libPaths()

# [1] "/usr/lib64/R/library"
# [2] "~/.local/bin/R/x86_64-redhat-linux-gnu-library/4.6"
  • /usr/lib64/R/library: the System library for base packages
  • ~/.local/bin/R/x86_64-redhat-linux-gnu-library/4.6: the User library for the package the user install

The R Library System

The library system

The R Library System with renv

The library system

The renv library system

The R Library System with renv (global cache)

The library system

The renv library system
(global cache)

Using renv

Initializing renv in a project

  1. Initialize renv in an existing project1 by running:
renv::init()

  1. Restart: after initializing renv, you should see the following message:
renv activated -- Please restart the R session.

  1. Verify: after restarting your R session, you should see something similar to:
R version 4.6.2 (2026-10-29) -- "Diversified Portfolio"

...

- Project '~/git/course-renv' loaded. [renv 1.3.1]

What did renv create?

Additional files have been created by renv

.
├── .Rprofile
│
├── renv/
│   ├── .gitignore
│   ├── activate.R
│   ├── library/
│   └── settings.dcf
│
└── renv.lock

  • .Rprofile: automatically activate renv when starting the session
  • renv/: contains project-specific R package library and renv settings
  • renv.lock: record of package versions and sources - a.k.a the lockfile

The Lockfile

The renv.lock is a JSON (JavaScript Object Notation) file that records package metadata

Including:

  • Package name
  • Package version
  • Package source (e.g. repository)
{
  "R": {
    "Version": "4.6.2",
    "Repositories": [
      {
        "Name": "CRAN",
        "URL": "https://cloud.r-project.org"
      }
    ]
  },
  "Packages": {
    "renv": {
      "Package": "renv",
      "Version": "1.3.1",
      "Source": "Repository",
      ...
      "Imports": [
        "utils"
      ],
      ...
      "Repository": "CRAN"
    },
    ...
  }
}

Keeping the R project in sync

renv ensures consistency between the project code, the installed packages, and the lockfile

1 All packages used in the code should be installed

2 All installed packages should be used in the code

3 All packages used in the code should be recorded

4 All recorded packages should be used in the code

5 All installed packages should be recorded

6 All recorded packages should be installed

renv::status() is your friend

Use the renv::status() function at any time to check inconsistencies between lockfile, library and code

renv::status()

# The following package(s) are used in this project,
# but are not installed:
#
# - fs

Package used but not installed in the project library

renv::status()

# The following package(s) are in an inconsistent state:
# 
# package   installed   recorded   used
# fs        y           n          y 

Package used and installed but not recorded in the lockfile

renv::status()

# The following package(s) are in an inconsistent state:
# 
# package   installed   recorded   used
# fs        y           y          n 

Package not used but recorded in the lockfile

renv::status()

# No issues found -- the project is in a consistent
# state. 
#
#

Nothing to do

Installing packages

Use the renv::install() function to install packages in your project library

You can install:

  • the latest version of a package
  • a specific version of a package
  • a package from GitHub
# Install the latest version from CRAN ----
renv::install("fs")

# Install a specific version ----
renv::install("fs@1.6.0")

# Install the development version from GitHub ----
renv::install("r-lib/fs")

You can also install all required packages (automatically detected by renv) in one command:

# Install (latest versions of) all required packages ----
renv::install()

Updating the lockfile

Use the renv::snapshot() function to update the lockfile (package metadata).

# Update the lockfile ----
renv::snapshot()

# The following package(s) will be updated in the lockfile:
# 
# # CRAN ----------------------------------------------------
# - fs    [2.1.0 -> *]
# - cli   [* -> 3.6.6]

  • Remove the fs package metadata from the lockfile
  • Add the cli package metadata to the lockfile

Collaborating with renv

Collaborating with renv

Important

  • Identify one user who will explicitly run renv::init() to initialize renv in the project
  • Check that the renv/.gitignore file contains the library/ entry

  • The user who initialized renv shares the project sources with the collaborators
  • When a collaborator first opens the project, renv should automatically bootstrap itself
  • The collaborator then runs renv::restore() to restore the project library locally

Important

After running git pull each collaborator must run renv::restore() to restore the project library locally if the lockfile has changed.

For more information

Collaborative workflow

Conflicts on the lockfile

Git conflicts are likely to occur when two or more collaborators independently update the lockfile using renv::snapshot()

  One solution: use renv at the end of the project to freeze the package environment

Resources

  • renv homepage
  • renv articles
  • Collaborating with renv