How to Install R Language on Ubuntu

R is a popular language for statistical analysis and data visualization, essential for both professionals and enthusiasts. R offers tools for statistical analysis, customizable graphics, and access to more than 15,000 packages available on CRAN. It is open-source, integrates with various languages and databases, and can be used with IDEs like RStudio for an improved coding experience.

You can install R on Ubuntu either from the default Ubuntu repositories or the CRAN repository. The default Ubuntu repository is simple but may provide an older version while CRAN offers the latest R version but requires a few extra steps. In this article, we will show you both these methods to install R language on Ubuntu 24.04.

Installing R Language from Ubuntu Repository

To install R on Ubuntu, first, update the packages list, then install R along with the required dependencies, and finally verify the R installation on Ubuntu:

Step 1: Update apt Package Lists

Let’s update the apt packages list before starting the R installation on Ubuntu:

sudo apt update
update the apt packages list before starting the R installation

Before installing R on Ubuntu, it’s recommended to check your Ubuntu version to avoid compatibility issues with the R packages and repositories.

Step 2: Installing R Language from the Official Repository 

After updating system packages, run the following command to install R from the Ubuntu repository along with the required dependencies:

sudo apt install r-base r-base-dev -y
installing r language from the ubuntu repository

Step 3: Confirm R installation

To confirm the R installation on Ubuntu, simply type R on the terminal and hit the enter key:

R

This command will open the R console if R is installed. Moreover, it shows the version information and a prompt (usually >). However, if R is not installed, you’ll see a message like “command not found”, which means R isn’t available on your system:

confirm r installation

You can type q() at the R prompt to quit the session, after which R will prompt you to save the workspace before exiting.

Installing R Language from CRAN Repository

CRAN is the primary repository for R, maintained by the R Foundation, and includes current and past versions along with contributed packages. To install R on Linux from the CRAN repository, you must go through the following steps:

Step 1: Update apt Package Lists

Let’s update system repositories to make sure we get the latest version of R:

sudo apt update
updating system packages

Step 2: Install Helper Packages

Run the following command to install software-properties-common for managing software repositories and dirmngr for handling certificates and secure connections:

sudo apt install software-properties-common dirmngr -y
installing helper packages for r language

Step 3: Add GPG Key for CRAN

Now, run the following command to download the GPG key for CRAN from the specified URL and add it to your trusted keys in the Ubuntu system:

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
adding gpg key for cran

You can check the CRAN repository key fingerprint by verifying the downloaded key with the following command:

gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
verifying cran repository key fingerprint

Step 4: Include CRAN Repository in System Package Sources

Now add the CRAN repository to your system, using lsb_release -cs to automatically insert your Ubuntu distribution’s codename and ensure the correct repository is included:

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
adding cran repository in system packages

Step 5: Install R Language on Ubuntu

Now you’re all set to install the base R package and its dependencies from the CRAN repository. For this purpose, you can execute the following command from the terminal:

sudo apt install r-base r-base-dev -y
installing r on ubuntu

The r-base-dev package offers the essential tools needed to compile and install R packages from source via the install.packages() function.

Step 6: Verify R Language Installation

Finally, you can confirm the R language installation on your Ubuntu machine using the following command:

R
verifying r installation

How to Install R Packages on Ubuntu?

R has thousands of packages created by its community for tasks like data visualization, machine learning, statistical modeling, and text mining. Among them, the most widely used are listed below:

  • dplyr: It helps us summarize, filter, and transform data in a simple way.
  • tidyr: It helps us organize messy data into a clean format.
  • purrr: It lets us work with functions and vectors to keep code consistent and flexible.
  • ggplot2: It’s a powerful package for creating and customizing beautiful plots.
  • data.table: It lets us handle and manipulate large datasets quickly.
  • stringr: It provides useful functions for working with text and strings easily.

You can install R packages directly from the R console by using the install.packages(“package_name”) function. For instance, the following command installs the data.table package from the CRAN repository:

install.packages("data.table")
installing r packages

After installing the package, you can use/load the package using the below-given syntax:

library("package_name")

Similarly, you can verify the package’s installation by listing all the installed packages using the following command:

installed.packages()[,1]

This is how you can install and start using the R language on Ubuntu.

Conclusion

R is free, open-source software used for statistical computing and graphics. You can install R on Ubuntu using either the default Ubuntu repository or the CRAN repository. The Ubuntu repository simplifies the process, while CRAN provides the latest version of R with a few extra steps. After installation, you can manage R packages easily through the R console, giving you access to powerful tools for data analysis and visualization. In this article, we discussed how to install the R language on Ubuntu using step-by-step instructions.

We hope this guide has assisted you in installing R on Ubuntu 24.04. Setting up R on a Linux system is straightforward with the right hosting platform. Ultahost’s Linux VPS hosting is an excellent choice, providing fast performance and the flexibility to scale resources as your data analysis projects expand.

FAQ

What is R used for?
Is R open-source?
How can we install R on Ubuntu?
How do I confirm if R is installed on my Ubuntu system?
How can I install R packages?
Why should I use CRAN to install R on Ubuntu?
What dependencies are needed to install R from the CRAN repository on Ubuntu?

Related Post

How to Check Ubuntu Version via Terminal and

Understanding your Ubuntu version is important for stay...

How to Create and Setup a Cron Job in Ubuntu

You will be wondering what is cron job? Cron is a time-...

How to Install the MERN on Ubuntu 24.04

The MERN stack, an acronym for MongoDB, Express.js, Rea...

How to Install Fail2ban on Ubuntu 22.04

Fail2Ban provides a protective shield for Ubuntu 22.04 ...

How to Install Deb Files / Packages on Ubuntu

The .deb packages are the standard format for installin...

How to Install Webmin on Ubuntu

Webmin is an open-source tool for Linux distributions t...

Leave a Comment