Install R in Ubuntu: How to Set up R Development Environment

In this guide, we’ll learn to install R in Ubuntu. R is an open-source programming language used extensively for statistical and scientific computing. Due to the ever-growing demand for data science and analytics, the popularity of R has increased multiplefold. At the time of this writing, R is placed 14th in the most popular languages list by TIOBE, beating out other commercial data science languages such as SAS and Stata.

Also read: How to Setup a Python Web Development Environment on Ubuntu VPS?

Steps to install R on Ubuntu

Let’s get into the steps to install and set up R in Ubuntu and its derivatives, including Linux Mint, Xubuntu, and many others!

1. Install Prerequisites

First, we have to download some prerequisites that will help us manage external software sources and manage GPG keys.

apt install --no-install-recommends software-properties-common dirmngr

2. Setup GPG Keys

Now we can go ahead and the GPG key of the CRAN repository. Adding GPG keys will tell the system that this external repository (CRAN) that we are going to add is trusted.

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

3. Add the R repository

Now we can go ahead and add the actual repository.

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

NOTE: This uses the lsb_release -cs command to access the Ubuntu version and add the relevant repository. For example, if you are running Ubuntu 20.04 the $(lsb_release -cs) will be replaced by “Focal”. So if you are using an Ubuntu derivative like Mint or Elementary, you will have to replace $(lsb_release -cs) with the corresponding Ubuntu release.

4. Update system and install R-base

Now we can go ahead and install the R package.

sudo apt update 
sudo apt install r-base

We have successfully installed R. To verify you can run R --version which will give you the version of the R package that we just installed.

R Version 1 Update system and install R-base

Installing R packages

You can install any R package using install.packages('package_name') in the R console

>  install.packages('tidyverse')

Here is a list of all the packages offered by R.

Steps to Install R GUI mode

You can use this command to run R in graphical mode instead of a terminal.

R -g Tk &

Run your first R script

There are two ways to do it.

You can start R in GUI using the above command and then go to File > Source R code and choose the program that you want to run there.

Another way is to use the RScript command.

Rscript /path/to/R/source/code
Rscript

NOTE: Any graphs/plots will be saved as PDF in the same directory as the R source code instead of being shown in the terminal.

Installing RStudio on Ubuntu

There are many IDEs available for R for example, Emacs with ESS and VimR but Rstudio is probably the most popular RStudio across platforms.

To install RStudio, we have to install the relevant deb package from here and then install it.

wget https://download1.rstudio.org/desktop/bionic/amd64/rstudio-2021.09.0%2B351-amd64.deb
sudo dpkg -i rstudio-2021.09.0+351-amd64.deb

We have downloaded the package for Ubuntu 18 (Bionic) because that is the latest package available.

Also read: 9 Best Linux Distros For Programming

FAQs

What is R, and why would I want to install it on Ubuntu?

R is a programming language and environment used for statistical computing and data analysis. Installing it on Ubuntu allows you to use R for data analysis, statistical computing, and more.

How can I install R on Ubuntu 20.04?

You can install R on Ubuntu 20.04 by adding the CRAN repository and then installing the R package. Detailed instructions can be found in the official documentation.

What is CRAN, and how does it relate to R?

CRAN, or the Comprehensive R Archive Network, is a collection of R packages and documentation. It provides a centralized location for R users to access packages for specific tasks in R.

Can I install R Studio on Ubuntu?

Yes, you can install R Studio on Ubuntu. You can download the Ubuntu package from the official R Studio website and then install it using the package manager.

How do I install R packages on Ubuntu?

You can install R packages on Ubuntu using the install.packages() function in R. This will download and install the package and its dependencies from CRAN.

How can I install the latest version of R on Ubuntu?

You can install the latest version of R on Ubuntu by adding the CRAN repository for the latest version and then installing the R package from that repository.

What do I do if I encounter issues when compiling R packages on Ubuntu?

If you encounter issues when compiling R packages on Ubuntu, ensure that you have all the necessary dependencies installed. You can also check the package documentation for specific instructions.

What is the default Ubuntu version for R installation?

The default Ubuntu repositories may not always have the most up-to-date version of R. It is recommended to use the CRAN repository for the latest version, especially for LTS releases.

How do I maintain an up-to-date version of R on Ubuntu?

To maintain an up-to-date version of R on Ubuntu, you can regularly check for updates on the CRAN repository and install the latest version when it becomes available.

Where can I find comprehensive documentation for R installation and usage on Ubuntu?

You can find comprehensive documentation for R installation and usage on Ubuntu on the official website of the R Foundation for Statistical Computing. They provide detailed guides and tutorials for Ubuntu users.

Conclusion

We have covered how to install R in Ubuntu and its derivatives. For any additional help, you can check out the official R website. The versatility of R extends beyond installation, offering a vast array of statistical tools and data manipulation capabilities. Have fun, and keep exploring 🙂