How to Install a Package in R: A Complete Guide

R's real power comes from its ecosystem of packages — thousands of community-built libraries that extend the base language for everything from data visualization to statistical modeling. Knowing how to install them correctly is one of the first practical skills any R user needs.

What Is an R Package?

A package in R is a collection of functions, datasets, and documentation bundled together to solve specific problems. Base R ships with a core set of capabilities, but packages let you do far more — think ggplot2 for graphics, dplyr for data manipulation, or caret for machine learning.

Packages are hosted in several places:

  • CRAN (Comprehensive R Archive Network) — the official, curated repository with thousands of vetted packages
  • Bioconductor — a specialized repository for bioinformatics tools
  • GitHub — where developers publish experimental or in-development packages before a CRAN release

Where a package lives determines how you install it.

Installing a Package from CRAN

CRAN is the default source, and installing from it is straightforward. In your R console or RStudio:

install.packages("package_name") 

Replace package_name with the actual package you want — for example:

install.packages("ggplot2") 

R will download the package and its dependencies (other packages it relies on) automatically. You only need to install a package once per R installation — not every time you open a new session.

Loading a Package After Installation

Installing and loading are two separate steps that trip up many beginners. After installation, you still need to load the package into your current session before you can use its functions:

library(ggplot2) 

Think of installation as putting a book on your shelf. library() is picking it up and opening it. You call library() every time you start a new R session.

Installing Multiple Packages at Once

You can pass a character vector to install several packages in a single command:

install.packages(c("dplyr", "tidyr", "ggplot2")) 

This is useful when setting up a new environment and you need a full stack of tools at once.

Installing Packages from GitHub 📦

Some packages aren't on CRAN yet — they exist only on GitHub. To install these, you first need the remotes package (or devtools, which is heavier but more common in older workflows):

install.packages("remotes") remotes::install_github("username/repository") 

For example, to install a hypothetical package from a GitHub repo:

remotes::install_github("tidyverse/dplyr") 

Important: GitHub packages haven't gone through CRAN's review process. They may be less stable or have undocumented breaking changes — worth keeping in mind depending on your use case.

Installing from Bioconductor

Bioconductor uses its own installer. You first install the BiocManager package from CRAN, then use it:

install.packages("BiocManager") BiocManager::install("DESeq2") 

Bioconductor packages are versioned to match specific R releases, so compatibility between your R version and the Bioconductor release matters here more than with CRAN packages.

Common Variables That Affect the Installation Process

FactorHow It Matters
R versionOlder R versions may not support newer package releases
Operating systemSome packages require OS-specific compilers (e.g., Rtools on Windows)
DependenciesA package may pull in many others; network speed and disk space matter
Repository sourceCRAN, GitHub, and Bioconductor each have different stability guarantees
RStudio vs. base RRStudio offers a GUI package manager as an alternative to the console

Troubleshooting Installation Errors

A few common issues come up regularly:

"Package not available for R version X" — The package may not have been updated for your R version yet. Updating R itself or installing from source can sometimes resolve this.

Compilation errors on Windows — Many packages with C or Fortran code require Rtools, a separate download from CRAN's website. Without it, installing from source will fail.

"There is no package called..." — This means the package isn't installed. Running install.packages() first resolves it. This error is often seen when sharing scripts with others who haven't installed the same packages.

Dependency conflicts — Occasionally two packages require incompatible versions of a shared dependency. Tools like renv help manage this by creating isolated, reproducible package environments per project. 🔧

Keeping Packages Updated

Installed packages don't update themselves. To update all installed packages:

update.packages(ask = FALSE) 

Or within RStudio, the Packages tab has an "Update" button that shows which packages have newer CRAN versions available.

Updating matters because package maintainers fix bugs, patch security issues, and improve performance over time — but updates can occasionally introduce breaking changes in your existing scripts, particularly with major version bumps.

Using renv for Reproducible Environments 🗂️

If you work in teams or on long-term projects, renv is worth knowing about. It locks your project to specific package versions, so the script that runs today still runs the same way six months from now — even if packages have been updated system-wide.

install.packages("renv") renv::init() 

This creates a project-specific library separate from your global R installation.

What Shapes Your Approach

How you install and manage packages in R shifts considerably depending on your situation. A solo analyst working in base R with stable CRAN packages has entirely different needs than a developer building production pipelines who needs version-locked environments. Someone in bioinformatics will rely heavily on Bioconductor, while someone doing web scraping might need cutting-edge GitHub packages before they hit CRAN.

Your operating system, R version, project scope, and tolerance for instability all factor into which installation methods and management tools make the most sense for how you actually work.