Install R on Ubuntu
Installation

Install R on Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

R is a programming language commonly used for r statistical computing and graphics and it is getting popular day by day.

So in this tutorial, I will walk you through two ways to install R on Ubuntu:

  • Using apt (the easy way)
  • Using CRAN Repository (gives you the most recent stable version)

While writing, the default repository gave me R version 4.2.2 whereas the CRAN repository gave me version 4.3.1.

Let's start with the first one.

Install R using apt on Ubuntu

If you don't care about having the latest version of R then you can definitely go with this method as all it takes is one command to install R in Ubuntu.

Also, if you are using a non-LTS version of Ubuntu, this is the only option you have as CRON repositories are only available for the LTS release.

To use apt to install R in Ubuntu, use the following command:

sudo apt update && sudo apt install r-base

Once done, you can start the R prompt using the following:

R
Check the installed verison of R installed from Ubuntu's repository

The above image indicates the installed version of R (4.2.2 in my case).

To quit from this prompt, type q():

q()
exit from the R prompt in Ubuntu Linux terminal

Install R using the CRAN repository in Ubuntu

ūüďč
CRAN repositories are only available for the LTS release of Ubuntu so if you are on a non-LTS release, it will give you multiple dependency errors.

If you want to have the most recent version of R and want to get the updates as soon as possible, then this is the method you should opt for.

First, download the key using the following command:

wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor -o /usr/share/keyrings/r-project.gpg

Next, add the R resources to the sources.list.d directory using the following:

echo "deb [signed-by=/usr/share/keyrings/r-project.gpg] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee -a /etc/apt/sources.list.d/r-project.list

That's it! Now, to take effect from the changes you've made, update the system repositories:

sudo apt update

Finally, you can install R through the CRAN repository:

sudo apt install r-base

Once done, start the R prompt:

R
Install the latest verison of R on Ubuntu using CRAN repository

As you can see, it gave me R version 4.3.1 which is a little ahead of what the default repository gave me.

Install R packages

The ability to download packages is one of the key reasons behind R is so popular and in this section, I will walk you through how you can install the txtplot library and use it to create a simple graph.

First, start the R prompt:

R

Next, use the following command to install txtplot:

install.packages('txtplot')

For those who don't know, txtplot is a library to print ASCII graphs, scatterplots, line plots, and more.

Once you are done with the installation, load the library using the following:

library('txtplot')

Now, create a sample data. First, start with the X-axis:

x <- c(1, 2, 3, 4, 5)

Next, execute the following for the Y-axis:

y <- c(2, 3, 5, 7, 11)

Finally, create a scatter plot with the provided data using the following:

txtplot(x, y, xlab = 'X-axis', ylab = 'Y-axis')

Here's the final result:

Use txtplot library in R

Pretty easy. Right?

How about creating custom commands?

Yep, you can create custom commands in Ubuntu or any other Linux distribution pretty easily.

To make it even easier, here's a detailed guide on how you can create custom commands on Ubuntu:

Create Custom Commands in Ubuntu
Save time by converting frequently used command and option combinations as custom command.

I hope you will find this guide helpful.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.