How to Install a Package in R: Methods, Options, and What to Know First
Installing packages in R is one of the first practical skills any R user needs — whether you're doing statistical analysis, building visualizations, or working with data pipelines. R's package ecosystem is one of its greatest strengths, with thousands of packages available across multiple repositories. But the right installation method depends on where the package lives, your R environment, and your technical setup.
What Is an R Package?
An R package is a bundled collection of functions, datasets, and documentation that extends R's base capabilities. Packages are how the R community shares reusable code. When you hear about tools like ggplot2, dplyr, or caret, those are packages — not part of base R, but installable additions that dramatically expand what R can do.
Packages are hosted in several places:
- CRAN (Comprehensive R Archive Network) — the official, curated repository
- Bioconductor — a specialized repository for bioinformatics packages
- GitHub — for development versions or packages not yet on CRAN
- Local files — for packages distributed as
.tar.gzor.ziparchives
The Standard Method: Installing from CRAN 📦
For most users, most of the time, packages come from CRAN. The syntax is straightforward:
install.packages("package_name") For example, to install ggplot2:
install.packages("ggplot2") R connects to CRAN, downloads the package, and installs it into your library path — the directory on your system where R stores installed packages. You only need to install a package once per R installation (unless you update R or reinstall).
After installation, you load the package in any session with:
library(ggplot2) The distinction between installing and loading trips up beginners. Installing places the package on your machine. Loading makes it available in your current session. You do the former once; you do the latter every time you start a new R session and need that package.
Installing Multiple Packages at Once
You can pass a character vector to install several packages in one call:
install.packages(c("dplyr", "tidyr", "readr")) This is useful when setting up a new environment or replicating someone else's workflow.
Installing from GitHub with remotes or devtools
When a package isn't on CRAN — perhaps it's in active development or maintained independently — GitHub is the next most common source. You'll need either the remotes or devtools package installed first:
install.packages("remotes") remotes::install_github("username/repository") The :: syntax lets you call a function from a package without loading the entire package with library() — a useful habit for one-off installations.
Key consideration: GitHub packages are not subjected to the same review process as CRAN packages. They may be less stable, lack full documentation, or change frequently.
Installing from Bioconductor
Bioconductor uses its own installer. You first install the BiocManager package from CRAN, then use it:
install.packages("BiocManager") BiocManager::install("GenomicRanges") Bioconductor is version-sensitive — specific Bioconductor releases align with specific R versions. Installing Bioconductor packages with a mismatched R version can cause dependency conflicts.
Installing from a Local File
If you have a package as a .tar.gz (source) or .zip (Windows binary) file, you can install it directly:
install.packages("path/to/package.tar.gz", repos = NULL, type = "source") The repos = NULL argument tells R not to look for the package online. This method is common in air-gapped environments, corporate networks, or when testing a package you've built yourself.
Key Variables That Affect Installation 🔧
Package installation isn't always seamless. Several factors shape your experience:
| Factor | Why It Matters |
|---|---|
| R version | Some packages require a minimum R version; outdated R can block installs |
| Operating system | Windows, macOS, and Linux handle binary vs. source packages differently |
| System dependencies | Some packages require external libraries (e.g., libcurl, libxml2) installed at the OS level |
| Network/proxy settings | Corporate or institutional networks may block CRAN connections |
| Library write permissions | Installing to a system library may require admin rights |
| Package dependencies | R installs dependencies automatically from CRAN, but not always from GitHub |
On Linux, R typically compiles packages from source, which means you may need development tools (gcc, make) and system libraries installed before certain packages will build. On Windows and macOS, pre-compiled binaries are usually available, making installation faster and simpler.
Managing Package Libraries
By default, R installs packages into a single library path. In more complex setups — shared servers, reproducible research projects, or production pipelines — per-project libraries become important. The renv package handles this by creating isolated, reproducible package environments per project, similar in concept to Python's virtual environments.
install.packages("renv") renv::init() This captures your project's package dependencies in a lockfile, so the same environment can be recreated elsewhere.
When Installation Fails
Common failure points include:
- Dependency not available for your R version
- Missing system libraries (usually flagged with a compile error)
- Timeout or mirror issues — try changing the CRAN mirror via
chooseCRANmirror() - Permission denied — install to a user library instead of the system library
How you resolve these depends heavily on your OS, your R setup, and whether you're working in a local environment, an RStudio Server, a Docker container, or a cloud-based notebook like Posit Cloud or Google Colab.
The mechanics of installing a package in R are consistent across contexts — but which method works best, which dependencies you'll need to handle, and how to manage your library structure all depend on the specifics of your environment and what you're building.