Installing a library in R means downloading a package of code and making it available on your computer

R comes with built-in functions, but most of the work you'll do relies on libraries — collections of code that other people have written and shared. Installing a library is a two-step process: first you download it (usually from CRAN, the Comprehensive R Archive Network), then you load it into your current R session. The most common way to install is with the install.packages() function, which takes the library name in quotes.

Once installed, a library stays on your computer. You only install it once. But every time you start a new R session and want to use that library, you have to load it with the library() function — no quotes this time. This distinction trips up beginners: install is a one-time setup, load is something you do every session.

Key Takeaways

  • Use install.packages("library_name") to download a library from CRAN the first time; you only do this once per computer.
  • Use library(library_name) at the start of each R session to load a library you've already installed; note the absence of quotes.
  • If a library fails to install, check that you have the right name (case-sensitive), that your internet connection works, and that you have write permission to your R library folder.
  • RStudio's Packages pane offers a point-and-click alternative to typing commands, but the command-line method works the same way.

The install.packages() function and where libraries come from

When you run install.packages("ggplot2"), R connects to CRAN and downloads the ggplot2 library to a folder on your computer called your library directory. CRAN is a network of servers that host R packages; R picks one automatically, but you can specify a different mirror if downloads are slow. The library stays there permanently until you manually remove it.

The first time you install a package, R may ask you to choose a CRAN mirror — just pick one geographically close to you. After that, R remembers your choice. If you're installing multiple packages at once, you can pass them as a vector: install.packages(c("ggplot2", "dplyr", "tidyr")). R will download all three in one command.

Some libraries depend on other libraries to work. When you install a package, R automatically installs its dependencies too, so you don't have to track them down yourself. This happens silently in the background.

Loading a library with library() so you can actually use it

Installing a library downloads it, but it doesn't make its functions available to you yet. To use the functions inside, you load it with library(ggplot2) — note that this time you don't use quotes around the name. When you run this line, R reads the library from disk and makes all its functions available in your current session.

You only load a library once per session. If you close R and open it again, you need to load it again. This is why people usually put all their library() calls at the top of their script — so anyone reading the code knows immediately what packages it needs, and so the functions are available for the rest of the script.

If you try to use a function from a library you haven't loaded, R will say the function doesn't exist. This is the most common beginner mistake: the library is installed on your computer, but you forgot to load it in this session.

Installing from places other than CRAN

Most libraries live on CRAN, but some are only on GitHub or Bioconductor. To install from GitHub, you first need the devtools library installed and loaded, then use devtools::install_github("username/repository"). The double colon tells R to use the install_github function from devtools without loading the whole library.

Bioconductor is a separate repository for biology and bioinformatics packages. To install from there, you first run install.packages("BiocManager"), then BiocManager::install("package_name"). This is less common unless you're working with genomic data or biological analysis.

For most everyday work, CRAN is where you'll install from. GitHub and Bioconductor are useful when you need a newer version of a package or one that's specialized for a particular field.

Troubleshooting when installation fails

If install.packages() returns an error, check these things in order. First, verify the package name is spelled correctly and in the right case — R is case-sensitive, so install.packages("Ggplot2") will fail even though the correct name is ggplot2. Second, make sure you're connected to the internet and that the connection is stable. Third, check that you have write permission to your library directory — on some shared computers or network drives, you may not be able to install packages.

If the error message mentions missing dependencies or compilation, your computer may be missing tools needed to build the package from source code. On Windows, you may need Rtools; on Mac, you may need Xcode command-line tools. These are free but take time to download. For most packages, R can install a pre-compiled binary instead, which is faster — if you see an option to do so, take it.

If you're still stuck, copy the full error message and search for it online. Package installation errors are usually well-documented, and someone has likely solved your exact problem before.

Using RStudio's Packages pane as an alternative

If you prefer not to type commands, RStudio offers a graphical interface. In the lower right pane, click the Packages tab, then click Install. A dialog box opens where you can type the package name and click Install. RStudio runs the install.packages() command for you behind the scenes.

To load a package in RStudio, you can click the checkbox next to its name in the Packages pane. This runs the library() command. However, this approach has a drawback: if you close your script and reopen it later, anyone reading your code won't see which packages you used, because the loads happened through the GUI, not in your script. For this reason, most people type library() calls at the top of their scripts even when using RStudio.

Updating libraries and managing versions

Over time, package authors release new versions with bug fixes and new features. To update all your installed packages, run update.packages(). R will check CRAN for newer versions and ask which ones you want to update. You can also update a single package by running install.packages("package_name") again — if a newer version exists, R will download it.

Sometimes a new version of a package breaks your old code because the authors changed how functions work. If this happens, you can install an older version using install.packages("package_name", version = "1.2.3"), though you need to know the exact version number. For serious work where reproducibility matters, tools like renv let you lock your project to specific package versions so your code works the same way months or years later.

For most everyday work, keeping packages up to date is fine. Updates usually add features and fix bugs without breaking existing code.

Frequently Asked Questions

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

install.packages() downloads a library from the internet to your computer — you do this once. library() loads an already-installed library into your current R session so you can use its functions — you do this every time you start a new session. Think of install as "buy the book" and library as "take the book off the shelf."

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

No. You install once, and the library stays on your computer. Every time you start R, you load it with library(), but you don't reinstall it. If you uninstall R or delete your library folder, you'll need to reinstall packages, but that's rare.

Why does my library fail to install with a "package not found" error?

Check the spelling and case of the package name — R is case-sensitive. Verify your internet connection works. If those are fine, the package name you're using might not exist on CRAN. Search the package name on cran.r-project.org to confirm it exists and see the correct spelling.

Can I install a library without internet?

No, install.packages() requires an internet connection to download from CRAN. However, once a library is installed, you can load it and use it offline. If you need to install packages on a computer without internet, you can download the package file on another computer and transfer it, but this is uncommon and more complex.

What happens if two libraries have functions with the same name?

If you load two libraries that both have a function called filter(), the one you loaded most recently will be used by default. To use the other one, specify it with the double colon: package1::filter(). This is why it's good practice to load libraries in a consistent order at the top of your script.