R keeps optional features in package libraries outside its base installation. CRAN provides released packages and their declared dependencies, while install.packages() handles repository lookup, download, and installation from an R session.
When the lib argument is omitted, R installs into the first directory returned by .libPaths(). Creating R's default personal library and putting it first for the current session avoids granting write access to the system library.
The withr example is a pure-R package that installs without a native-code compiler. Packages containing C, C++, or Fortran code can require platform build tools and system development libraries on Linux, while Windows and macOS often use a compatible CRAN binary when one is available.
Steps to install an R package from CRAN:
- Launch R without loading a saved workspace.
$ R --quiet --no-save >
- Store R's default personal-library path.
> package_lib <- Sys.getenv("R_LIBS_USER") - Create the default personal-library directory.
> dir.create(package_lib, recursive = TRUE, showWarnings = FALSE)
- Put the personal library first for this R session.
> .libPaths(c(package_lib, .libPaths()))
A later R session uses this directory automatically once it exists, provided the default R_LIBS_USER setting remains unchanged.
Related: Set a user package library in R - Install the withr package from the cloud CRAN mirror.
> install.packages("withr", repos = "https://cloud.r-project.org") Installing package into '/home/analyst/R/aarch64-unknown-linux-gnu-library/4.5' (as 'lib' is unspecified) trying URL 'https://cloud.r-project.org/src/contrib/withr_3.0.3.tar.gz' ##### snipped ##### * DONE (withr)The package name, archive type, and library path depend on the package, operating system, and R build. CRAN dependencies required for installation are fetched automatically.
Related: Fix an R package 00LOCK error
- Confirm the installed withr package path.
> find.package("withr") [1] "/home/analyst/R/aarch64-unknown-linux-gnu-library/4.5/withr" - Call a withr function to confirm the installed namespace is usable.
> withr::with_options(list(width = 40), getOption("width")) [1] 40
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.