How to install the R language on Linux

R is a programming language mainly used in data science for data visualization and statistical analysis. Due to the wide range of applications of R language in machine learning, researchers and statisticians require it in various fields like academia, finance, and marketing. In this Answer, we will discuss how to install the R on different distributions (illustrated below) of Linux.

  • R on Ubuntu

  • R on Debian

  • R on Fedora

Let us go through all these individually.

R on Ubuntu

Open a terminal to install R on Ubuntu. First, update the package lists and upgrade the packages with the help of the command below.

sudo apt update -qq
sudo apt upgrade
Commands to update the packages

The -qq in the update command makes the output less verbose.

  • We need to add software repositories. Before it, we will install the software-properties-common , and dirmngr packages. Run the following command on Linux to perform this action.

sudo apt install --no-install-recommends software-properties-common dirmngr:
  • Now install the public key for the CRAN repository to the system's trusted keyring. The tee and -a commands are used to write and append the output to the file specified.

wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
  • Your system is ready to add the CRAN repository. In the command given below, the $(lsb_release -cs) part dynamically retrieves the Ubuntu code to ensure the correct repository version is added.

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/
  • Update the packages; now, your system has an R language installed. To check the version of the installed R language, follow the command below.

R --version

The output of the command will be as:

widget

R on Debian

Follow the instructions presented below to install the R language on Debian.

  • Import the GPG key into the system. As shown in the command below, the key server used here is keyserver.ubuntu.com.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7
  • To add the CRAN repository in Debian, run the following command.

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/debian $(lsb_release -cs)-cran40/

After doing this, don't forget to update the packages.

R on Fedora

Installing the R language on Fedora is very simple because Fedora supports R and is readily available for installation using the package manager dnf.

Only run the following command to install R on Fedora:

sudo dnf install R

To open the R terminal on your systems, you can type R and press enter; here, you can run the R commands and make your program. Here is a simple program that prints "Hello World" in R.

R program
R program

Some programs in the R language require packages that need to be installed. There are several packages in the R language, which are represented below.

R packages
R packages

We can install these packages with a very simple command, install.packages(package_name).

Look at the below illustration where we installed the magrittr package in R.

Package installation
Package installation

Conclusion

The R programming language is a powerful tool widely used in data science. Moreover, it has a large number of packages for analyzing data. In this Answer, we walked you through the step-by-step installation of R on different distributions of Linux, namely Ubuntu, Debian, and Fedora.

Copyright ©2024 Educative, Inc. All rights reserved