Written and maintained by CASRAI Editorial Board
Published
On this page
Short answer: R calls what you install a package and what you load a library. Install once with install.packages("name"), then load it in every new session with library(name). This guide is for researchers and analysts who need a package on their machine and want to know where it went, why it failed, and how to install several at once, from a file, or from a terminal.
What it is used for in research
Base R ships with a core set of functions; most statistical methods used in published work (matching, mixed models, plotting extensions, survey tools) live in add-on packages. Installing is a one-time download and build step, while loading with library() is a per-session step. The R documentation states that a package “is loaded from a library by the function library()“, so installing alone does not make its functions available. For example, the MatchIt guide and the guide to importing data in R both assume this install-then-load pattern.
Install or access (official source only)
Packages come from repositories. install.packages() downloads from “CRAN-like repositories or from local files”, per the R reference page for utils::install.packages. You need Internet access for the default route. R itself is obtained from CRAN (cran.r-project.org); see Update R and RStudio if your version is old, because a package may not be available for an older R.
Step-by-step: install and load a package
- Choose a CRAN mirror (optional). The R installation manual notes you can select one with
chooseCRANmirror(). In a script or non-interactive session, name the repository directly:install.packages("ggplot2", repos = "https://cloud.r-project.org"). - Install. Run
install.packages("ggplot2")in the R console (this is the same in the RStudio Console, in R on macOS or Windows, and in a terminal R session). - Check where it was installed. Run
.libPaths(). If you do not passlib, the documentation says the package goes into the first directory listed there. If that directory is not writable, an interactive session offers to create a personal library. - Load it. Run
library(ggplot2). Do this in every new R session and at the top of every script. - Confirm. Run
"ggplot2" %in% rownames(installed.packages())to list-check, or call a function from the package.
Install several packages at once
The pkgs argument is a character vector, so pass one: install.packages(c("dplyr", "readr", "haven")).
Which dependencies get installed
The dependencies argument defaults to NA, which means c("Depends", "Imports", "LinkingTo"): the packages your package needs to run. Setting dependencies = TRUE also installs Suggests; the documentation says this applies only when lib has length one.
Install from a local file or a terminal
For a downloaded source archive, use install.packages("path/to/pkg.tar.gz", repos = NULL, type = "source"). The reference page says repos = NULL installs “from local files, directories or URLs”. From a system shell, source packages can be installed with R CMD INSTALL, optionally with -l libdir to set the target library, as described in the R Installation and Administration manual.
Install routes at a glance
| Route | Command or setting | Documented behaviour |
|---|---|---|
| One package from CRAN | install.packages("pkg") |
Downloads from the configured repositories (getOption("repos")) |
| Several packages | install.packages(c("a", "b")) |
pkgs is a character vector |
| Other repositories | repos = ... or setRepositories() |
repos takes repository base URLs; setRepositories() chooses among known ones |
| Local source file | install.packages(file, repos = NULL) |
Installs from local files, directories or URLs |
| Terminal | R CMD INSTALL pkg.tar.gz |
Source install; -l sets the library |
| Custom library location | lib = "dir", or R_LIBS_USER |
Overrides the first entry of .libPaths() |
Windows and macOS: binary versus source
Per the reference page, binary packages apply on Windows and CRAN builds of R for macOS. The default type there is "both": use the binary if available and current, otherwise try source. Source installs of packages with compiled code then need build tools on your machine. An R built from source on macOS is not a CRAN build, so binaries may not apply.
Output and how to read it
Installation prints progress messages in the console. If more than one library is on the path, the documentation says R prints a message about which one it is using. installed.packages() returns a matrix with one row per installed package; use rownames(installed.packages()) for a plain list of names. To make your analysis reproducible, record the R version and package versions with sessionInfo() and cite them in your methods.
Troubleshooting
“package is not available”
Check getOption("repos"). The package may be in a different repository, may not exist for your R version or operating system, or the name may be misspelled (names are case-sensitive).
“not writable” or permission errors
Inspect .libPaths(). Accept the personal-library prompt in an interactive session, or set a user library through the R_LIBS_USER environment variable (colon-separated paths, semicolon-separated on Windows).
Compilation fails
A source install of a package with compiled code needs the platform’s build tools. Prefer the binary route where one exists.
“could not find function” after installing
Installing does not load the package. Run library(pkg).
Free alternatives
| Need | Option | Source |
|---|---|---|
| Install from the console | install.packages(), part of base R’s utils package |
R reference manual |
| Install from a shell | R CMD INSTALL, included with R |
R Installation and Administration |
| Choose repositories interactively | setRepositories(), included with R |
R reference manual |
FAQ
What is the difference between a package and a library in R?
A package is the installed unit of code; a library is a directory holding installed packages. library(pkg) loads a package from a library into the session.
How do I install a library in R on a Mac?
Use the same install.packages("name") call. CRAN builds of R for macOS can use binary packages; the default type tries a binary first and falls back to source.
How do I install packages from the command line?
Use R CMD INSTALL for a source archive, or start R and call install.packages().
How do I see which packages are installed?
Run installed.packages() and, to see where they live, .libPaths().
Sources (official docs)
- R Core Team, install.packages reference page (utils package), read 2026-11-07.
- R Core Team, R Installation and Administration, section “Add-on packages”, read 2026-11-07.
Related: Dataframe in R programming.







