How to Install R Packages from the Command Line

  • By:Other
  • 2024-05-12
  • 4

Installing R Packages via Command Line: A Quick Guide

When working with R, installing packages is a common task that allows users to access additional functions and tools to enhance their data analysis and visualization capabilities. While many users are familiar with using RStudio or the R console to install packages, installing packages via the command line can be a convenient and efficient way to manage packages in a more scriptable manner.


Before we dive into the installation process, make sure you have R installed on your system. You can download the latest version of R from the official CRAN website. Additionally, ensure that you have administrative privileges on your system to install packages.

Step 1: Accessing the R Command Line Interface

To install R packages from the command line, you need to access the R command line interface. On Windows, this can be done by opening the Command Prompt and typing ‘R’ to launch the R console. On Unix-based systems like macOS or Linux, you can open a terminal and type ‘R’ to start the R console.

Step 2: Installing Packages

Once you are in the R console, you can use the ‘install.packages()’ function to install packages directly from CRAN. For example, to install the popular ‘ggplot2’ package, you can use the following command:


Running this command will download and install the ‘ggplot2’ package along with any dependencies it requires. You can also specify a specific repository or version of a package by providing additional arguments to the ‘install.packages()’ function.

Tip: Installing Packages in a Specific Directory

If you want to install packages in a specific directory, you can use the ‘lib’ argument in the ‘install.packages()’ function. For example, to install the ‘dplyr’ package in a directory named ‘my_packages’ located in your home directory, you can use the following command:

install.packages("dplyr", lib="~/my_packages")

This will install the ‘dplyr’ package in the ‘my_packages’ directory, allowing you to easily manage packages in a separate location.

Step 3: Loading Installed Packages

Once you have installed a package, you can load it into your R session using the ‘library()’ function. For example, to load the ‘ggplot2’ package that we installed earlier, you can use the following command:


After loading a package, you can start using its functions and features in your R scripts and analyses.

Step 4: Updating and Removing Packages

In addition to installing packages, you can also update and remove packages from the command line using the ‘install.packages()’ function with appropriate arguments. To update a package, use the ‘update.packages()’ function, and to remove a package, use the ‘remove.packages()’ function.


Installing R packages from the command line can streamline the package management process and make it easier to automate the installation of packages in your R workflows. By following the steps outlined in this guide, you can effectively install, update, and remove packages without relying on a graphical user interface.



Foshan Soonk Packaging Machine Co., Ltd.

We are always providing our customers with reliable products and considerate services.

    If you would like to keep touch with us directly, please go to contact us



        Online Service