The basic way to install an R package
To install a package in R, open R or RStudio and run the install.packages() function with the package name in quotes. For example, to install the ggplot2 package, type install.packages("ggplot2") and press Enter. R will download the package from CRAN (the Comprehensive R Archive Network) and install it on your computer.
The first time you install a package, R may ask you to select a CRAN mirror — a server location to download from. Pick one geographically close to you for faster downloads. After installation finishes, you will see a message saying the package was unpacked successfully. You only need to install a package once per R version; after that, you load it with library() each time you start a new session.
Key Takeaways
- Use install.packages("package_name") to download and install a package from CRAN, the official R package repository.
- After installation, load the package into your session with library(package_name) — without quotes — every time you want to use it.
- If installation fails, check your internet connection, verify the exact package name on cran.r-project.org, and make sure you have write permissions in your R library folder.
- For packages not on CRAN, use devtools::install_github() to install directly from GitHub, or install.packages() with a local file path for offline installation.
Installing multiple packages at once
To install several packages in one command, pass a vector of package names to install.packages(). Type install.packages(c("ggplot2", "dplyr", "tidyr")) and R will install all three. This is faster than running the function three separate times because R downloads them all in one session.
If one package fails to install, the others will still complete. R will print an error message for the failed package, but you can install it separately afterward. This approach is useful when setting up a new R installation or sharing a script that requires multiple packages.
Loading a package after installation
Installing a package and loading it are two separate steps. Installation puts the package on your computer; loading it brings its functions into your current R session. After you install a package, load it with library(package_name) — note that the package name does not need quotes when using library().
For example, after running install.packages("ggplot2"), type library(ggplot2) to load it. You will see a message confirming the package loaded, sometimes with version information or startup messages. You must load the package every time you start a new R session if you want to use its functions. If you forget to load it, R will return an error saying the function is not found.
Installing packages from GitHub
Some packages are not on CRAN and exist only on GitHub. To install directly from GitHub, first install the devtools package with install.packages("devtools"), then load it with library(devtools). Next, run install_github("username/repository") where username and repository are the GitHub account and project name.
For example, to install a package from the GitHub user "hadley" in the repository "ggplot2", you would run install_github("hadley/ggplot2"). This method requires an internet connection and works only if the repository is public. GitHub packages are often development versions, so they may be less stable than CRAN releases.
Troubleshooting installation problems
If install.packages() returns an error, first check that you spelled the package name correctly and that it exists on CRAN. Visit cran.r-project.org and search for the package name to confirm. Common misspellings include forgetting hyphens or underscores — for example, the package is "data.table", not "datatable".
If the package name is correct but installation still fails, check your internet connection and try selecting a different CRAN mirror. Some mirrors are faster or more reliable than others. You can also check that you have write permissions in your R library folder — if R cannot write files there, installation will fail. On Windows, try running R as administrator. On Mac or Linux, check folder permissions with your system settings.
If you see a message about missing system dependencies or compiler tools, the package requires software outside of R to build. On Windows, install Rtools from cran.r-project.org/bin/windows/Rtools. On Mac, install Xcode Command Line Tools by opening Terminal and typing xcode-select --install. On Linux, install build-essential and other development packages through your package manager.
Installing packages from a local file
If you have a package file on your computer — usually a .zip file on Windows or a .tar.gz file on Mac and Linux — you can install it without downloading from CRAN. Use install.packages("path/to/file.zip", repos = NULL, type = "source"), replacing the path with the actual location of your file.
This method is useful when you are offline, installing a modified version of a package, or installing a package that someone sent you directly. Make sure the file is a valid R package file before attempting installation, as corrupted or incomplete files will cause errors.
Checking which packages are installed
To see all packages currently installed on your computer, run library() with no arguments. R will open a window showing every installed package with its version number. You can also run installed.packages() to get a detailed table of all packages, their versions, and their library locations.
To check if a specific package is installed before loading it, use require("package_name") instead of library(). The require() function returns TRUE if the package is installed and FALSE if it is not, which is useful in scripts that need to handle missing packages gracefully.
Frequently Asked Questions
Do I need to install a package every time I open R?
No. You install a package once, and it stays on your computer. Each time you open R, you load the package with library() if you want to use it. Installation and loading are separate — installation is permanent, loading is temporary per session.
What is the difference between install.packages() and library()?
install.packages() downloads and installs a package from CRAN to your computer. library() loads an already-installed package into your current R session so you can use its functions. You run install.packages() once; you run library() every session you need the package.
Can I install a package if I do not have internet?
Yes, if you have the package file. Download the .zip or .tar.gz file on a computer with internet, transfer it to your offline computer, and install it with install.packages("path/to/file", repos = NULL, type = "source"). You cannot use CRAN without internet.
Why does my package installation fail with a compiler error?
Some packages contain C or C++ code that must be compiled during installation. If you see compiler errors, you need build tools: Rtools on Windows, Xcode Command Line Tools on Mac, or build-essential on Linux. Install these first, then try installing the package again.
What does "package not found" mean?
R cannot find a package with that name on CRAN. Check the spelling — package names are case-sensitive and often contain dots or hyphens. Search cran.r-project.org to confirm the exact name. If the package truly does not exist on CRAN, it may be on GitHub and require devtools::install_github() instead.