The simplest way to install a package in R

To install a package in R, open R or RStudio and run a single command in the console: install.packages("package_name"). Replace "package_name" with the actual name of the package you want — for example, install.packages("ggplot2") installs the ggplot2 package for data visualization. R downloads the package from CRAN (the Comprehensive R Archive Network, the official repository) and places it on your computer.

After installation finishes, you load the package into your current session with library(package_name) — for example, library(ggplot2). You only install once, but you load the package every time you start a new R session and want to use it. This two-step process (install, then load) confuses many people because they think installation means the package is ready to use when ready.

Key Takeaways

  • Install a package once with install.packages("name"), then load it in each session with library(name).
  • CRAN is the default source, but you can install from GitHub or Bioconductor if a package is not on CRAN.
  • If installation fails, check your internet connection, verify the package name is spelled correctly, and confirm the package exists on your chosen repository.
  • RStudio's Packages pane offers a point-and-click alternative to typing commands, though the command line is faster once you know the package name.

Installing from CRAN in RStudio

RStudio provides a graphical interface that avoids typing commands. In the lower right pane, click the Packages tab, then click the Install button. A dialog box opens. Type the package name in the text field — RStudio will suggest matching names as you type. Click Install and RStudio runs the command for you.

This method works well if you are installing one or two packages and want to see the name suggestions. However, if you install packages regularly or need to install many at once, typing the command is faster. You can also install multiple packages in one command by separating names with commas: install.packages(c("ggplot2", "dplyr", "tidyr")).

Installing from GitHub when CRAN does not have the package

Some packages exist only on GitHub, not on CRAN. These are often development versions or packages that have not yet been submitted to CRAN. To install from GitHub, you first need the devtools package, which you install normally: install.packages("devtools").

Then load devtools and run: library(devtools) followed by install_github("username/repository"). Replace "username" and "repository" with the actual GitHub user and repository name — for example, install_github("hadley/ggplot2") installs Hadley Wickham's development version of ggplot2. The package documentation or GitHub page will tell you the correct username and repository name.

Installing from Bioconductor for biology and genetics packages

Bioconductor is a separate repository for packages related to biology, genomics, and bioinformatics. CRAN packages and Bioconductor packages do not overlap much, so if you are working with genetic data or biological analysis, the package you need may only exist on Bioconductor.

To install from Bioconductor, first install the BiocManager package from CRAN: install.packages("BiocManager"). Then load it and run: library(BiocManager) followed by install("package_name") — note that Bioconductor uses install() instead of install.packages(). For example, BiocManager::install("DESeq2") installs a popular package for analyzing RNA sequencing data.

What to do when installation fails

The most common reason installation fails is a typo in the package name. R is case-sensitive, so install.packages("Ggplot2") will not work — it must be lowercase: install.packages("ggplot2"). Check the official documentation or the repository page to confirm the exact spelling.

If the name is correct but R says the package does not exist, verify you are looking at the right repository. A package might exist on GitHub or Bioconductor but not on CRAN. Check your internet connection — installation requires downloading files from the repository. If your connection is unstable, the read may fail partway through. Try again, and R will usually resume or restart the read.

On rare occasions, a package requires a newer version of R than you have installed. The error message will mention this. You can check your R version by running R.version in the console. If you need to update R, visit the official R website and read the latest version for your operating system.

Updating packages you already have

Packages receive updates regularly. To update a single package, run install.packages("package_name") again — R will install the newer version over the old one. To update all packages at once, run update.packages() with no arguments. R will check each installed package against the repository and ask which ones you want to update.

In RStudio, click the Packages tab and then Update to see which packages have newer versions available. You can select individual packages or click Select All to update everything at once. Updates usually fix bugs or add features, so updating regularly is a good habit.

Understanding where packages are stored

When you install a package, R stores it in a folder called a library. On Windows, this is usually C:\Users\YourUsername\Documents\R\win-library\[R version]. On Mac, it is usually ~/Library/R/[R version]/library. On Linux, it varies by system. You do not need to navigate to these folders manually — R handles everything.

If you ever need to know where your packages are stored, run .libPaths() in the console. This shows all the folders R searches when you load a package. Most users have one library, but advanced users sometimes maintain multiple libraries for different projects. For most purposes, you can ignore where packages are stored and let R manage them automatically.

Frequently Asked Questions

Do I have to install a package every time I use R?

No. You install a package once, and it stays on your computer. Every time you start a new R session, you load it with library(package_name). Think of installation as buying a book and loading as taking it off the shelf to read.

What is the difference between install.packages() and library()?

install.packages() downloads and stores the package on your computer. library() loads it into your current session so you can use its functions. You must install before you can load, but you only install once.

Can I install a package if I do not have internet?

No, installation requires downloading files from a repository. However, if you have the package files already (for example, a .tar.gz file), you can install from a local file using install.packages("path/to/file.tar.gz", repos = NULL).

What does the error "package not found" mean?

R cannot find the package name you typed in the repository you are using. Check the spelling (R is case-sensitive), verify the package exists on CRAN or your chosen repository, and make sure you are connected to the internet.

How do I uninstall a package?

Run remove.packages("package_name") to uninstall. In RStudio, click the Packages tab, find the package, and click the X button next to its name. Uninstalling frees up disk space but does not affect other packages.