The three ways to get packages into R
R packages are collections of functions and data that extend what R can do. To use a package, you first download it from a repository — usually CRAN (the Comprehensive R Archive Network) — then install it on your computer, then load it into your current R session. Most people use the built-in install.packages() function, which handles all three steps at once. You can also download packages from Bioconductor if you work with biological data, or install directly from GitHub if you need a development version.
The method you choose depends on where the package lives and what you need it for. CRAN packages are the most common and the easiest to install. Bioconductor packages require a different installer. GitHub packages let you get the latest version before it reaches CRAN, but they may be less stable.
Key Takeaways
- The install.packages() function downloads and installs packages from CRAN in a single command: install.packages("package_name")
- After installing a package once, you load it into each new R session with library(package_name) — you do not reinstall it every time.
- Bioconductor packages require the BiocManager package first, then BiocManager::install("package_name") instead of install.packages().
- GitHub packages need the devtools or remotes package, then devtools::install_github("username/repository") to pull the latest development version.
- If install.packages() fails, check that you have an internet connection, that the package name is spelled correctly, and that the package actually exists on CRAN.
Installing packages from CRAN with install.packages()
CRAN is the default repository for R packages. To install a package from CRAN, open R or RStudio and type the command at the console prompt. Replace "package_name" with the actual name of the package you want — for example, install.packages("ggplot2") to install the ggplot2 graphics package.
R will download the package and any other packages it depends on, then install them all. This usually takes a few seconds to a minute depending on the package size and your internet speed. You will see text scroll past in the console. When it finishes, you are done installing — you do not need to do anything else.
If you want to install multiple packages at once, put them in a vector: install.packages(c("ggplot2", "dplyr", "tidyr")). R will install all three in one command.
Loading a package with library() each time you need it
Installing a package puts it on your computer. Loading it puts it into your current R session so you can actually use it. These are two separate steps. You install a package once, but you load it every time you start a new R session and want to use that package.
To load a package, type library(package_name) at the console — note that you do not use quotes around the package name here, unlike in install.packages(). For example, library(ggplot2) loads the ggplot2 package. If the package loads successfully, you will see the console prompt return with no error message. If you see an error saying the package does not exist, it means you have not installed it yet.
You can load multiple packages in the same session by calling library() multiple times, once for each package. The order usually does not matter, though occasionally one package will conflict with another if they define functions with the same name.
Installing Bioconductor packages for biological data
Bioconductor is a separate repository focused on packages for analyzing biological and genomic data. Packages like DESeq2, Seurat, and GenomicRanges live on Bioconductor, not CRAN. To install from Bioconductor, you first need to install the BiocManager package from CRAN, then use BiocManager to install the Bioconductor package you want.
Run these two commands in order. First, install BiocManager: install.packages("BiocManager"). Then, install your Bioconductor package: BiocManager::install("DESeq2") — replace "DESeq2" with the package name you actually need. BiocManager will handle the download and installation, including any dependencies.
You load Bioconductor packages the same way as CRAN packages: library(DESeq2). The difference is only in how you install them.
Installing development versions from GitHub
Developers often push new versions of packages to GitHub before those versions reach CRAN. If you need the latest features or bug fixes, you can install directly from GitHub. This requires the devtools or remotes package first.
Install devtools from CRAN: install.packages("devtools"). Then load it: library(devtools). Now you can install from GitHub using the syntax install_github("username/repository"). For example, install_github("tidyverse/ggplot2") installs the development version of ggplot2 from the tidyverse GitHub account.
GitHub packages may change frequently and are not tested as thoroughly as CRAN releases, so they can be less stable. Use them when you specifically need a feature that is not yet in the CRAN version, not as your default.
Checking what packages you have installed
To see all packages installed on your computer, type library() with no arguments. R will open a window showing every package you have installed, organized by category. This is useful if you forget the exact name of a package or want to see what you have available.
To check whether a specific package is installed, you can use require() instead of library(). The require() function returns TRUE if the package is installed and loads it, or FALSE if it is not. This is mainly useful in scripts where you want to check before proceeding.
Troubleshooting failed installations
If install.packages() fails, the most common causes are a typo in the package name, no internet connection, or the package does not exist on CRAN. Check the spelling first — R is case-sensitive, and most package names are lowercase. Verify you can reach the internet. Then search the CRAN website or Google to confirm the package exists and that you have the right name.
On Windows, you may see a message asking whether to install from source or binary. Choose binary — it is faster and usually works. On macOS, you may need to install Xcode Command Line Tools if you are installing from source. On Linux, you may need to install development libraries for your system.
If a package fails to install because of a missing dependency, install the dependency first, then try again. The error message usually tells you which package is missing. If you are still stuck, copy the full error message and search for it — someone else has likely hit the same problem and posted a solution online.
Frequently Asked Questions
Do I have to reinstall a package every time I open R?
No. You install a package once with install.packages(), and it stays on your computer. Every time you open a new R session, you load it with library() if you want to use it. Installing and loading are different steps.
What is the difference between install.packages() and library()?
install.packages() downloads and installs a package from the internet to your computer. library() loads an already-installed package into your current R session so you can use its functions. You install once, load every session.
Can I install a package if I do not have administrator rights on my computer?
Yes. R installs packages to a user library by default, which does not require administrator rights. If you get a permission error, it usually means R is trying to install to the system library instead. Reinstall with the default settings and it should work.
What does it mean when a package says it requires a newer version of R?
Some packages need features that only exist in recent versions of R. If you see this error, you need to update R itself, not just the package. Go to the R website, download the latest version for your operating system, and install it. Then try installing the package again.
Is it safe to install packages from GitHub?
GitHub packages are written by the same people who write CRAN packages, but they have not gone through CRAN's testing process. They are generally safe, but they may have bugs or change without warning. Use them when you need a specific feature, not as your default source.