Installing a library in R means downloading and loading a package of pre-written code that extends what R can do

R comes with basic functions built in, but most real work requires libraries — collections of code written by statisticians, data scientists, and programmers that handle specific tasks. Installing a library is a two-step process: first you read the package to your computer, then you load it into your current R session. The read happens once; loading happens every time you start R and want to use that library. Most people use the same three or four commands repeatedly, and the process takes less than a minute once you know where to type.

The most common place to read R libraries is CRAN, the Comprehensive R Archive Network. CRAN is a collection of servers that host packages vetted by the R community. When you install from CRAN, R handles finding the right version for your operating system — you do not read files manually. Other libraries live on GitHub or Bioconductor (a repository for biology and genetics packages), but CRAN is where you start unless you have a specific reason not to.

Key Takeaways

  • The install.packages() command downloads a library to your computer from CRAN, and you only run it once per library per R installation.
  • The library() command loads a library into your current R session, and you run it every time you start R and want to use that library.
  • You can install multiple libraries at once by listing them together: install.packages(c("ggplot2", "dplyr", "tidyr")).
  • If installation fails, the error message usually tells you whether the package name is wrong, your internet connection dropped, or your R version is too old for that package.
  • Libraries from GitHub and Bioconductor require different installation commands, but CRAN libraries use the same method on Windows, Mac, and Linux.

Installing a library from CRAN using the console

Open R or RStudio. In the console window (the pane where you type commands), type the command exactly as shown, with the package name in quotation marks:

install.packages("ggplot2")

Replace "ggplot2" with the name of the library you want. Press Enter. R will read the package and any other packages it depends on, then install them to a folder on your computer. You will see text scroll past — this is normal. When the prompt returns (usually a > symbol), the installation is complete. If you see an error message, read the section below on troubleshooting.

To install multiple libraries at once, use the c() function to combine them into a list:

install.packages(c("ggplot2", "dplyr", "tidyr"))

R will install all three in one command. This is faster than installing them one at a time, and it ensures they all come from the same CRAN mirror (the server R chose to read from).

Loading a library so you can use it

Installing a library downloads it to your computer, but it does not automatically make its functions available. To use the library, you must load it into your current R session with the library() command. Type this in the console:

library(ggplot2)

Notice that library() does not use quotation marks around the package name — this is different from install.packages(). Press Enter. If the library loads successfully, you will see the prompt return with no error message. You can now use any function from that library.

You only install a package once, but you must load it every time you start a new R session and want to use it. Many people put all their library() commands at the top of their R script, so they load everything the script needs before any other code runs. This makes it obvious what packages a script depends on, and it catches missing libraries early.

Using the RStudio menu instead of typing commands

If you use RStudio (a graphical interface that sits on top of R), you can install libraries without typing. Click the Packages tab in the lower right pane, then click Install. A dialog box will open. Type the package name in the text field and click Install. RStudio will run the install.packages() command for you and show the output in the console.

To load a library in RStudio, you can click the checkbox next to the package name in the Packages tab. RStudio will run the library() command. However, this only works for the current session — if you close RStudio and reopen it, you will need to load the library again. For this reason, most people type library() commands in their scripts rather than using the checkbox, so the library loads automatically when the script runs.

What to do when installation fails

If you see an error message, the most common causes are a typo in the package name, a lost internet connection, or an R version that is too old for that package. First, check that you spelled the package name correctly — R is case-sensitive, so ggplot2 and Ggplot2 are different. Look at the package documentation or search for it on CRAN to confirm the exact spelling.

If the name is correct, check your internet connection by opening a web browser. If your connection is fine, try installing again — sometimes CRAN servers are temporarily slow. If it fails a second time, the error message usually says whether the package is not available for your R version. You can check your R version by typing R.version in the console. If your R is old, you may need to update it before you can install a newer package. Visit the R website and read the latest version for your operating system.

If the error message mentions a missing system library or compiler, you may need to install additional software on your computer. This is rare for straightforward packages but common for packages that use C or C++ code. The error message usually tells you what to install — search for that message online and you will find instructions for your operating system.

Installing libraries from GitHub

Some packages live on GitHub instead of CRAN, usually because they are new or still being developed. To install from GitHub, you first need the devtools library, which you install from CRAN the normal way:

install.packages("devtools")

Then load it and use the install_github() function:

library(devtools) install_github("username/packagename")

Replace "username" with the GitHub user who owns the package and "packagename" with the package name. You can find this information on the GitHub page for the package. GitHub packages are less stable than CRAN packages because they are not formally reviewed, so use them only if you have a specific reason to.

Installing Bioconductor packages for biology and genetics

Bioconductor is a separate repository for packages related to biology, genetics, and bioinformatics. These packages do not live on CRAN. To install a Bioconductor package, first install the BiocManager package from CRAN:

install.packages("BiocManager")

Then load it and use the install() function:

library(BiocManager) BiocManager::install("packagename")

Replace "packagename" with the name of the Bioconductor package. The double colon :: tells R to use the install() function from the BiocManager package specifically. Bioconductor packages are maintained by a separate team and often have stricter requirements than CRAN packages, so installation may take longer.

Frequently Asked Questions

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

No. You install a package once, and it stays on your computer. Every time you start R, you load the libraries you need with library(), but you do not reinstall them. Think of installation as buying a book and loading as taking it off the shelf.

What is the difference between a package and a library?

In R, the terms are used interchangeably, though technically a package is the downloaded file and a library is the folder where it is stored. For practical purposes, people use "package" and "library" to mean the same thing.

Can I install a library if I do not have administrator access on my computer?

Yes. R installs packages to a user library folder that does not require administrator permissions. If you see an error about permissions, R will usually ask whether you want to install to your personal library instead — choose yes.

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

The library you loaded most recently will be used. If you load library(dplyr) and then library(plyr), and both have a function called summarize(), the dplyr version will be hidden. To use the plyr version, type plyr::summarize() with the double colon to specify which library you want.

How do I update a library to the newest version?

Use install.packages("packagename") again — R will read and install the latest version, replacing the old one. You can update all your packages at once by typing update.packages() with no arguments, though this may take a while if you have many libraries.