R packages add functions, datasets, documentation, and other tools to a base R installation. Most packages are installed from the Comprehensive R Archive Network, commonly called CRAN, while packages for bioinformatics and related fields are often distributed through Bioconductor.

Steps to Install an R Package from CRAN

R programming language consists of a huge collection of packages at the cran and bioconductor repositories. This is also one of the reason for R language to become so popular.

Use the install.packages() function to download and install a package from CRAN. You normally need to install a package only once for each R library and R version.

  1. Open the R console, RGui, RStudio console, VS Code terminal, or another terminal where R is available.
  2. Run install.packages("package_name"), replacing package_name with the package you need.
  3. If R asks you to select a CRAN mirror, choose a nearby mirror or use the default cloud mirror.
  4. Wait until R reports that the package was successfully installed.
  5. Load the installed package with library(package_name).
</>
Copy
install.packages("package_name")

The package name must be enclosed in quotation marks during installation. For example, the following command installs the jsonlite package.

</>
Copy
install.packages("jsonlite")

Selecting a CRAN mirror during R package installation

Older R setups may display a window asking you to select a CRAN mirror. Choose a geographically close mirror and click OK. You can avoid the prompt by specifying the CRAN repository directly.

</>
Copy
install.packages("jsonlite", repos = "https://cloud.r-project.org")
R install package

Example: Install the compare Package in R

The following console session installs a package named compare. The exact folder paths, package version, download size, and messages may differ on your computer.

> install.packages("compare")
Installing package into ‘C:/Users/TutorialKart/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://cloud.r-project.org/bin/windows/contrib/3.5/compare_0.2-6.zip'
Content type 'application/zip' length 510919 bytes (498 KB)
downloaded 498 KB

package ‘compare’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\TutorialKart\AppData\Local\Temp\RtmpSW6t9I\downloaded_packages
> 

After installation, load the package into the current R session with library().

</>
Copy
library(compare)

If library(compare) runs without an error, R found the installed package and attached it to the current session. You must run library() again in each new R session where you want to use the package.

Install an R Package Only If It Is Missing

A script can check whether a package is already installed before attempting another installation. This is useful in setup scripts and reproducible projects.

</>
Copy
package_name <- "jsonlite"

if (!requireNamespace(package_name, quietly = TRUE)) {
  install.packages(package_name, repos = "https://cloud.r-project.org")
}

library(jsonlite)

requireNamespace() checks whether R can find the package without attaching it. The package is installed only when that check returns FALSE.

Install Multiple R Packages with One Command

Pass a character vector to install.packages() when a project requires several packages.

</>
Copy
install.packages(
  c("dplyr", "ggplot2", "readr"),
  repos = "https://cloud.r-project.org"
)

R installs the requested packages and any required dependencies that are available from the selected repository.

Install an R Package from the Command Line

You can install a CRAN package without opening an interactive R console by running an R expression from a terminal. This requires the R or Rscript executable to be available in your system path.

</>
Copy
Rscript -e 'install.packages("jsonlite", repos="https://cloud.r-project.org")'

On Windows Command Prompt, use double quotation marks around the full R expression and escape the package-name quotation marks.

</>
Copy
Rscript -e "install.packages(\"jsonlite\", repos=\"https://cloud.r-project.org\")"

Install an R Package from a Local File or Folder

Use a local package archive when the computer is offline or when you have downloaded a package file manually. Set repos = NULL so R treats the path as a local package rather than a repository name.

Install a local binary package on Windows

</>
Copy
install.packages(
  "C:/Downloads/package-name.zip",
  repos = NULL,
  type = "win.binary"
)

Install a local R package from source

</>
Copy
install.packages(
  "/home/user/Downloads/package-name.tar.gz",
  repos = NULL,
  type = "source"
)

Installing from source may require development tools and external system libraries. On Windows, some source packages require a compatible Rtools installation. On Linux, a compiler and development headers may be required.

Download R Packages for Offline Installation

For an offline computer, download the package archive and its required dependencies on a computer with internet access. Copy the files to the offline system, then install each local archive with repos = NULL.

</>
Copy
install.packages(
  "C:/OfflinePackages/jsonlite_1.8.9.zip",
  repos = NULL,
  type = "win.binary"
)

The archive type must match the operating system and R version. A Windows binary package cannot normally be installed as a Linux binary, and a binary built for an incompatible R version may fail to load.

Install a Bioconductor Package in R

Bioconductor packages are generally installed through the BiocManager package rather than by calling install.packages() directly for the target package.

</>
Copy
if (!requireNamespace("BiocManager", quietly = TRUE)) {
  install.packages("BiocManager")
}

BiocManager::install("Biostrings")

This approach lets Bioconductor select package versions that are compatible with the installed R and Bioconductor releases.

Choose a Custom R Package Library

R installs packages into a library directory. If you do not have permission to write to the system library, create a personal library and pass its path through the lib argument.

</>
Copy
personal_library <- "~/R/library"
dir.create(personal_library, recursive = TRUE, showWarnings = FALSE)

install.packages(
  "jsonlite",
  lib = personal_library,
  repos = "https://cloud.r-project.org"
)

library(jsonlite, lib.loc = personal_library)

Use .libPaths() to see the library directories that R currently searches.

</>
Copy
.libPaths()

Update Installed R Packages

Use update.packages() to check installed packages against versions available in the configured repositories.

</>
Copy
update.packages(
  ask = FALSE,
  repos = "https://cloud.r-project.org"
)

Review project compatibility before updating all packages in an important or reproducible analysis. A newer package version can introduce changed behavior or dependency requirements.

Uninstall an R Package

Use remove.packages() to remove an installed package from a library.

</>
Copy
remove.packages("compare")

If the package is installed in a non-default library, specify that directory.

</>
Copy
remove.packages("compare", lib = "~/R/library")

Why an R Package Installation Fails

An installation error usually points to a repository, permission, dependency, network, library-lock, compiler, or version-compatibility problem. Read the first error message in the console rather than relying only on the final warning.

  • Package is not available: Check the package spelling, configured repository, and whether the package supports your R version.
  • Permission denied: Install into a personal library that your user account can modify.
  • Cannot open URL: Check the internet connection, proxy settings, firewall, repository URL, and TLS support.
  • Non-zero exit status: Look earlier in the output for a missing dependency, compiler error, or system-library error.
  • Package lock error: Close other R sessions and remove the stale 00LOCK directory only after confirming no installation is still running.
  • Compilation tools are missing: Install the required compiler toolchain, such as Rtools on Windows, when a source package must be compiled.
  • Dependency is unavailable: Install the missing R dependency or the required operating-system development library.

Check the installed R version and package libraries

</>
Copy
R.version.string
.libPaths()
getOption("repos")

These commands help confirm which R version is running, where packages are being installed, and which repository R is using.

Test installation outside RStudio

If package installation fails inside RStudio, try the same command in the standard R console or through Rscript. This helps determine whether the problem is related to R itself, the selected library, or the development environment.

Frequently Asked Questions About Installing R Packages

Why can I not install packages in R?

Common causes include an unsupported R version, an incorrect package name, a repository or network problem, insufficient write permission, a missing dependency, or unavailable compilation tools. Inspect the earliest error in the installation output for the specific cause.

Do I need to install an R package every time?

No. Install a package once into an R library, then use library(packageName) in each new R session. You may need to reinstall packages after changing R versions, library locations, computers, or project environments.

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

install.packages() downloads and installs package files into a library. library() loads an already installed package into the current R session.

How do I install an R package in VS Code?

Open an R terminal or an integrated terminal in VS Code, start R, and run install.packages("packageName"). You can also run the installation through Rscript -e when Rscript is available in the system path.

How do I know whether an R package is installed?

Use requireNamespace("packageName", quietly = TRUE). It returns TRUE when the package is available in the active library paths and FALSE when R cannot find it.

Editorial QA Checklist for R Package Installation Instructions

  • Verify that every CRAN example encloses the package name in quotation marks.
  • Confirm that installation and loading are explained as separate operations.
  • Check Windows, macOS, and Linux path examples for correct slash and quotation-mark usage.
  • Confirm that local package examples use repos = NULL and the appropriate package type.
  • Test troubleshooting advice against the exact installation error before recommending deletion of a lock directory or installation of build tools.