The Easy Way to Install a Package in R (with 8 Code Examples)
R is a powerhouse programming language with many data science applications. But before we can put its packages to work, we need to install them. Here's how.
If you're learning R or working on a data project, you'll often need to install packages. R packages are add-ons that give you extra tools—whether you're cleaning data, creating charts, or building statistical models. In this guide, we'll walk you through the easiest ways to install R packages, step by step. You'll learn how to get packages from CRAN, GitHub, and other sources, plus how to load them in your R session.
Installing R Packages from CRAN
Most R packages are stored on the Comprehensive R Archive Network (CRAN), the official R package repository. To install a package from CRAN, use the install.packages()
function:
install.packages("readr")
This command installs the {readr} package, which helps you read CSV files and other flat file formats. Be sure to put the package name in quotation marks.
You can also install multiple packages at once by passing a character vector (c()
):
install.packages(c("readr", "ggplot2", "tidyr"))
For more details, see the install.packages() documentation.
If you’re not sure which package to use for a specific task, the CRAN Task Views page is a helpful resource. It organizes packages by topic and use case.
A Faster Way to Install R Packages
The {pak} package is a modern tool that makes installing packages quicker and more reliable. It automatically handles dependencies and works well on any operating system.
To use {pak}, first install it:
install.packages("pak")
Then, install any package with:
pak::pkg_install("readr")
You can install multiple packages at once, too:
pak::pkg_install(c("readr", "ggplot2", "tidyr"))
The {pak} package is a great option when you’re setting up a new R environment or managing multiple packages.
How to Install R Packages in RStudio Without Code
If you’re working in RStudio, you can install packages without typing any code:
- Go to
Tools
→Install Packages
- Select
Repository (CRAN)
from the dropdown - Enter one or more package names
- Make sure
Install dependencies
is checked - Click
Install
This is a good option if you’re new to RStudio and prefer using menus.
Installing R Packages from GitHub
Some R packages aren't on CRAN but are available on GitHub instead. These packages are often under development or offer features not yet available on CRAN.
To install a package from GitHub, use the {remotes} package:
install.packages("remotes")
remotes::install_github("rstudio/shiny")
In this example, you’re installing the {shiny} package from the RStudio GitHub repository.
If you need to install a package from a private GitHub repository, you’ll need a personal access token (PAT). You can create one in your GitHub account settings and use the auth_token
argument. See the remotes package documentation for details.
Installing R Packages from Other Repositories
You can also install packages from external repositories outside CRAN and GitHub. To do this, provide the repository URL when using install.packages()
:
install.packages("furrr", repos = "http://cran.us.r-project.org", dependencies = TRUE)
This method is helpful when using a CRAN mirror or a private repository.
Installing R Packages from a Local File
If you’ve downloaded a package file to your computer, you can install it like this:
install.packages("C:/Users/YourName/Downloads/abc_2.1.zip", repos = NULL, type = "source")
You can also install a package file using RStudio’s interface:
- Go to
Tools
→Install Packages
- Select
Package Archive File (.zip, .tar.gz)
- Choose the file from your computer
- Click
Install
Loading a Package After Installation
After installing a package, you need to load it before you can use it in your R session. Do this with the library()
function:
library(readr)
You only need to install a package once, but you’ll need to load it every time you start a new R session.
Summary
Here’s a quick recap of the ways you can install R packages:
- Use
install.packages()
to install from CRAN - Use the {pak} package for faster, dependency-aware installs
- Use RStudio’s interface to install packages without code
- Install packages from GitHub with {remotes}
- Add packages from external repositories or local files
- Load packages in your session with
library()
You can browse available R packages on the CRAN Packages Index or explore packages by topic in the CRAN Task Views.
Whether you’re working in RStudio, a Jupyter Notebook, or the R console, knowing how to install and load packages is an essential skill for any R user.