The basic way to install an R package
To install a package in R, you use the install.packages() function with the package name in quotes. Open R or RStudio, type this command into the console, and press Enter:
install.packages("ggplot2")
R will download the package from CRAN (the Comprehensive R Archive Network, the official repository) and install it on your computer. The first time you run this command, R may ask you to choose a mirror — pick one geographically close to you for faster downloads. After installation finishes, you load the package into your current session with library():
library(ggplot2)
Once loaded, you can use all the functions that package provides. You only install a package once per R version, but you load it with library() every time you start a new R session and want to use it.
Key Takeaways
- Install packages once using install.packages("package_name"), then load them each session with library(package_name).
- CRAN is the default source, but you can install from GitHub using devtools::install_github("username/repository") if you have the devtools package.
- Check that a package installed correctly by running library(package_name) without errors.
- If installation fails, check your internet connection, make sure the package name is spelled correctly, and verify the package exists on CRAN.
Installing multiple packages at once
If you need several packages, pass them all to install.packages() as a vector using the c() function:
install.packages(c("dplyr", "tidyr", "ggplot2"))
R will download and install all three in one command. This is faster than running install.packages() three separate times. After installation, load each one individually with library() when you need it, or load them all at the start of your script.
Installing packages from GitHub
Some packages live on GitHub before they reach CRAN, or developers maintain development versions there. To install from GitHub, you first need the devtools package, which you install from CRAN the normal way:
install.packages("devtools")
Then load it and use the install_github() function with the repository owner and name:
library(devtools) install_github("hadley/ggplot2")
GitHub packages are useful for testing new features or using packages that haven't been released to CRAN yet. They may be less stable than CRAN versions, so use them when you have a specific reason to.
Checking your R version and package compatibility
Some packages require a minimum R version. If installation fails with a message about version compatibility, check your current R version by typing:
R.version
If your version is too old, you may need to update R itself. On Windows and Mac, download the latest version from the R Project website and run the installer. On Linux, use your package manager (apt, yum, or brew depending on your distribution). After updating R, you may need to reinstall your packages, since they are stored separately for each R version.
Troubleshooting installation failures
If install.packages() returns an error, start with these steps. First, confirm the package name is spelled exactly right — R is case-sensitive. Second, check that you have an active internet connection. Third, try switching to a different CRAN mirror by running chooseCRANmirror() and selecting a different server.
Some packages require system libraries that R cannot install on its own. On Windows, you may need to install Rtools. On Mac, you may need Xcode command-line tools. On Linux, you may need to install development headers for your system (usually a package like build-essential or gcc). The error message usually tells you what is missing. If you are stuck, copy the full error message into a search engine — someone has usually solved it before.
Updating packages you already have
Packages receive updates that fix bugs and add features. To update all your installed packages, run:
update.packages()
R will check each package against the latest version on CRAN and ask whether you want to update. You can also update a single package by running install.packages("package_name") again — R will replace the old version with the new one. Updating is optional but recommended, especially for security-related packages.
Understanding where packages are stored
When you install a package, R stores it in a library directory on your computer. You can see where this is by running:
.libPaths()
On Windows, this is usually in your Documents folder under R. On Mac and Linux, it is usually in your home directory under R. If you have multiple R versions installed, each has its own library, so packages from one version do not automatically appear in another. You can also create a project-specific library if you want to isolate packages for a particular analysis, though this is an advanced workflow.
Frequently Asked Questions
Do I need to install 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 R and want to use that package, you load it with library(package_name). Think of installation as buying a book and loading as taking it off the shelf.
What is the difference between CRAN and GitHub packages?
CRAN packages are reviewed and tested before release, so they are generally stable. GitHub packages are often development versions that may change or have bugs. Use CRAN packages for regular work and GitHub packages when you need a specific new feature or are helping test a package.
Why does installation fail with a message about dependencies?
Some packages depend on other packages to work. R should install dependencies automatically, but if it does not, you can install them manually by name. The error message usually lists which packages are missing. Install those first, then try installing your original package again.
Can I install a package offline?
Not easily. R packages are downloaded from online repositories. If you have no internet access, you can download a package file manually on another computer and install it locally using install.packages("path/to/file.tar.gz", repos=NULL), but this is uncommon and requires the file in the right format.
What happens if two packages have conflicting functions?
If two packages define a function with the same name, the one you loaded most recently takes priority. You can still access the other by using the package name with a double colon: package1::function_name(). This is rare but useful to know when you load many packages.