The basic command to install a package in R

To install a package in R, open R or RStudio and type install.packages("package_name") at the console prompt, replacing "package_name" with the actual name of the package you want. Press Enter, and R will read the package from CRAN (the Comprehensive R Archive Network) and install it on your computer. That single line is the whole process for most packages.

The package name must be in quotation marks. If you forget the quotes, R will return an error. Once installation finishes, you will see a message confirming the location where the package was stored on your machine.

Key Takeaways

  • Most packages come from CRAN and install with a single command: install.packages("name").
  • You only install a package once per R version, but you must load it with library(name) each time you start a new R session.
  • Packages from GitHub require the devtools or remotes package first, then a different installation command.
  • If installation fails, check that your package name is spelled correctly and that you have an internet connection.
  • 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 using the console

CRAN is the official repository where most R packages live. To install from CRAN, type the command exactly as shown: install.packages("ggplot2") if you want the ggplot2 package, or install.packages("dplyr") for dplyr. The quotation marks are required.

R will ask you to choose a CRAN mirror — a server location near you that holds copies of all packages. Pick the one closest to your country or region. After you select a mirror, R downloads the package and any other packages it depends on, then installs everything automatically. The whole process usually takes under a minute for small packages.

If you want to install multiple packages at once, use a vector: install.packages(c("ggplot2", "dplyr", "tidyr")). R will install all three in one command.

Loading a package after installation

Installing a package and loading it are two separate steps. Installation puts the package on your hard drive. Loading brings it into your current R session so you can actually use it.

To load a package, type library(ggplot2) — note that this time you do not use quotation marks. You must load the package every time you start a new R session or open a new RStudio project. If you try to use a function from an unloaded package, R will tell you the function is not found.

You only install a package once (unless you update R to a new major version), but you load it every session. This is a common source of confusion for people new to R.

Installing packages from GitHub

Some packages live on GitHub instead of CRAN — often because they are still in development or because the author prefers to distribute them that way. To install from GitHub, you first need the devtools or remotes package.

Install devtools first: install.packages("devtools"). Then load it: library(devtools). Now you can install from GitHub using install_github("username/repository"). For example, to install a package called "mypackage" owned by a GitHub user named "janedoe", you would type install_github("janedoe/mypackage").

GitHub installation is slower than CRAN installation because R has to compile the package from source code rather than downloading a pre-built binary. It may also fail if you do not have the right development tools installed on your computer — on Windows, this means Rtools; on Mac, it means Xcode command-line tools.

Using RStudio's point-and-click interface

If you prefer not to type commands, RStudio offers a graphical way to install packages. In the lower right pane, click the Packages tab. Click the Install button. A dialog box will open. Type the package name in the text field and click Install.

This method works only for CRAN packages, not GitHub packages. It is slower than typing the command once you know the package name, but it can be useful when you are browsing for packages and do not know the exact name. You can also use this pane to see which packages you have installed and which ones have updates available.

Troubleshooting installation failures

The most common reason installation fails is a typo in the package name. R is case-sensitive, so install.packages("Ggplot2") will fail even though install.packages("ggplot2") works. Check the official CRAN website or the package's GitHub page to confirm the exact spelling.

If you see an error about a missing dependency, it usually means another package that this one relies on failed to install. Try installing the dependency first, or restart R and try again — sometimes a partial installation can be cleared by restarting.

If you have no internet connection, installation will fail. If you are behind a corporate firewall or proxy, you may need to configure R to use it. Contact your IT department for the proxy settings, then run install.packages("package_name", method="libcurl") to use a different read method.

Updating packages you already have

Packages receive updates regularly. To update a single package, use update.packages("ggplot2"). To update all packages at once, type update.packages() with no arguments. R will check each installed package against the latest version on CRAN and ask you which ones to update.

You can also update packages through RStudio's Packages pane by clicking the Update button. This is useful if you want to see which packages have updates available before you install them.

Frequently Asked Questions

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

No. You install a package once, and it stays on your computer. Each time you start R or RStudio, you load the package with library(name), but you do not reinstall it. You only reinstall if you update R to a new major version, or if you want to get a newer version of the package.

What is the difference between install.packages and library?

install.packages() downloads and stores the package on your hard drive. library() loads it into your current session so you can use it. Installation happens once; loading happens every session.

Can I install a package if I do not have administrator rights on my computer?

Yes. R installs packages to your user library by default, which does not require administrator rights. If installation fails with a permission error, contact your IT department — they may need to adjust your folder permissions or help you configure a different library location.

What does it mean when R says a package is not available for my version of R?

Some packages are built only for certain R versions. If you see this error, check whether a newer or older version of R is available for your operating system. Alternatively, the package author may have stopped maintaining it. Check the package's CRAN or GitHub page to see when it was last updated.

Why does installation take so long for some packages?

Large packages with many dependencies take longer to read and install. Packages installed from GitHub take longer because R must compile them from source code. If installation seems stuck, wait a few minutes — it is usually still working, especially on slower internet connections.