Skip to content

R installs a package library per version, and keeps every version

Package libraries, renv caches and knitted output accumulate per project and per R release. What to prune and what to keep.

4 min read

R keeps its packages in a library folder tied to the minor version of R, so upgrading from one release to the next means installing everything again into a new folder while the old one remains. On a machine that has followed a few releases, that is several complete package libraries.

Find the libraries

du -sh ~/Library/R/*/library 2>/dev/null | sort -h
du -sh /Library/Frameworks/R.framework/Versions/* 2>/dev/null | sort -h
R -e '.libPaths()' 2>/dev/null | head

The first command shows the per version user libraries, which is where most installed packages go. Compare the list against the R version you actually run, which is the last entry from .libPaths().

Compiled packages are the large ones

Packages with compiled code, particularly the spatial, statistical modelling and plotting stacks, are tens of megabytes each with their source and shared libraries. A full data science library is a few gigabytes, per R version.

renv, and the cache that makes it worthwhile

A project using renv keeps its own library, which sounds worse and is actually better: renv links to a shared global cache instead of copying, so twenty projects using the same package version store it once.

du -sh ~/Library/Caches/org.R-project.R/R/renv 2>/dev/null
R -e 'renv::paths$cache()' 2>/dev/null

Because projects link into that cache, removing it breaks their libraries until restored, which is the same distinction between a cache and a store described in pnpm and yarn stores. Use renv's own clean rather than deleting the folder.

Knitted output and datasets

  • Rendered documents and their figure folders, regenerated by knitting again.
  • Cached chunks in a _cache folder beside the document, disposable.
  • Datasets downloaded into project folders, which are the part that may not be replaceable.

The last one matters more than all the packages: a dataset fetched from a source that has since changed is not recoverable, and it is worth separating from the rebuildable output before any cleanup.

Common questions

Where does R install packages on a Mac?

In a user library under ~/Library/R, one folder per minor R version, plus the framework libraries under /Library/Frameworks/R.framework. Running .libPaths() in R prints the paths in use.

Why do I have several R package libraries?

Because each minor R version gets its own library folder, and upgrading installs packages again rather than reusing the old folder. Previous versions stay until removed.

Is it safe to delete the renv cache?

Not directly. Project libraries link into it, so removing it breaks them until packages are restored. Use renv's own clean functions instead of deleting the folder.

What is the largest part of an R setup?

Compiled packages, particularly spatial and modelling stacks, at tens of megabytes each. A full data science library is a few gigabytes per R version installed.

Read next