How to install R in Ubuntu?


R is a programming language and free software environment that is used for statistical computing and graphics. In recent years the popularity of R programming grows rapidly because there is rapid growth in the fields like data visualization, data analysis, and data science, etc.

The R programming language is written primarily using C, Fortran, and R itself. Its syntax is pretty easy you can find so many learning resources on R programming online.

In this article, I will discuss how to install R programming in Ubuntu.

Prerequisites

To follow this article you should have the following:

  • A system with a recent version of Ubuntu (here I will use Ubuntu 20.04 LTS) installed on it.
  • You should have the access to a user account with superuser privilege.
  • Working internet connection.

Installing R from Ubuntu’s repository

If you install R from Ubuntu’s official repository you will get a slightly older version but installing this way is one of the easiest methods.

sudo apt install r-base

Press y and then enter if it asks you for confirmation.

Installing R from CRAN repository

If you want to install the latest version of R then use CRAN (Comprehensive R Archive Network) which is a list of mirrors for downloading the latest version of R.

First, use the given command to download the required dependencies –

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Next, add the repository key of CRAN in your 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

Now add the repository on your system by using –

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

Here we use lsb_release -cs to access which Ubuntu flavor you are using.

Finally, use the given command to install the repository and its dependencies –

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

Verify the R installation

After installing R from any of the given methods you can verify that if R programming is installed properly or not by using the given command in your terminal.

R --version

If it is installed correctly you will see the output as given in the image below.

R version

How to use R in Ubuntu

The R programming language can be used in several ways on a Ubuntu system.

Using R in interactive mode

After installing R open your terminal and use –

R

This will open an R console which is given in the image below.

R interactive mode

Here you can enter an R command OR perform basic mathematical operations.

command in R console

You can exit from the R console by using the q().

Using R in non-interactive mode

In non-interactive mode, you can execute an R program from  a file with .r extension.

To demonstrate this I will run the hello world program which is saved in a file i.e. hello.r you can see its content in the image below.

hello.r

Now to run this program use –

Rscript hello.r

This will display the given result in your terminal.

output

You can also use an IDE such as RStudio for executing R scripts.

Conclusion

By following the above instructions I hope you are able to use the R programming language on your Ubuntu system.

For any query write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.