Install R on Ubuntu 20.04

R is a programming language that commonly used by data scientists for statistical computing, data analysis, and graphics representation.

This tutorial demonstrates how to install R on Ubuntu 20.04.

Install R

Download GPG key:

sudo wget -qO /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc

The CRAN repository contains R packages. So add this repository to sources list:

echo "deb $(lsb_release -sc)-cran40/" | sudo tee /etc/apt/sources.list.d/cran_ubuntu.list

Update the package lists and install R:

sudo apt update
sudo apt install -y --no-install-recommends r-base

When it finished, we can check R version:

R --version

Testing R

Create a main.r file:

nano main.r

Once file is opened, add a line of code:

print("Hello world")

Use Rscript command to run script:

Rscript main.r

Uninstall R

If you decided to completely remove R and related dependencies, run the following command:

sudo apt purge --autoremove -y r-base

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
sudo rm -rf /etc/apt/sources.list.d/cran_ubuntu.list

Leave a Comment

Your email address will not be published.