Reproducible R Environments in RStudio with renv: Lockfile Setup and Restore
Set up renv in an RStudio project to lock package versions in renv.lock and restore the environment with renv::restore() on any machine.
08 Jul 2026, 07:18 UTC

What you are solving
A teammate clones your RStudio project and the code fails because package versions differ. The useful takeaway is a lockfile: renv records exact package versions and sources in renv.lock, and renv::restore() rebuilds the environment automatically. RStudio integrates renv via the Project pane and Packages tab.
Prerequisites
Use RStudio with an R session where renv is installed. Run in the Console:
install.packages('renv')Check version:
packageVersion('renv')Require 0.9.0 or newer. renv must be installed before initializing a project, otherwise renv::init() fails. Work inside an RStudio Project, File → New Project → New Directory, so the library can be project-local and avoid cross-contamination between projects.
Initialize and snapshot
Open the project. In the Console:
renv::init()renv scans library() and require() calls, creates a project-local library, writes renv.lock to the project root, creates renv/ and a .Rprofile that activates the project library on startup.
Install packages as normal, for example:
renv::install('ggplot2')Record the environment:
renv::snapshot()renv::snapshot() updates renv.lock with exact versions and sources. Commit renv.lock to Git with your code. If you change versions later, install the new version and run renv::snapshot() again, otherwise reproducibility breaks.
Restore on a fresh machine
On a new machine, open the project in RStudio and run:
renv::restore()renv reads renv.lock and installs the recorded packages into the project library.
Expected checks:
- renv::status() reports the library is in sync with the lockfile.
- packageVersion('ggplot2') matches the version listed in renv.lock.
- renv.lock exists in the project root and is plain JSON listing each package, version and source.
Verification test: create a new RStudio project, run renv::init(), install a package, close and reopen the project, run renv::restore(), confirm the package loads without errors.
Limitations and recovery
renv records R packages only. It does not pin the R version itself or system libraries such as compilers or GDAL. If restore fails to compile, install required system dependencies.
Recovery options:
- Library mismatch: run renv::restore() to align with lockfile, or renv::snapshot() to update lockfile intentionally.
- Leave renv: renv::deactivate() and remove .Rprofile activation.
- Start over: delete renv/ and renv.lock, then renv::init() again.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.