R is an open-source programming language and environment for statistical computing and data visualization. R's extensive collection of libraries and packages supports various statistical techniques and models such as hypothesis testing, regression analysis, time series analysis, and more.
Data scientists and statisticians use R to manipulate, calculate, and graphically display data in statistical plots and graphs.
In this tutorial, you will learn how to install R on Ubuntu and start working with datasets quickly.

Prerequisites
- Ubuntu installed.
- A user account with root or sudo privileges.
- Access to the terminal.
How to Install R on Ubuntu
There are two ways to install R on Ubuntu:
- From Ubuntu repositories. The installation process is simple and requires only a few steps. However, since the default Ubuntu libraries are not updated as frequently as the CRAN repositories, installing an older version of R is possible.
- From the CRAN repository. Comprehensive R Archive Network (CRAN ) is backed by the R Foundation. It serves as the primary software repository for R. It hosts a collection of current and past R distribution versions, documentation, and contributed R packages. While the installation process from CRAN involves additional steps compared to the Ubuntu repositories method, it always includes the latest available R version.
The following text explains how to install R using these two methods.
Method 1: Install R from Ubuntu Repository
To install R from the Ubuntu repository:
1. Update the apt package lists:
sudo apt update
2. Install the R base package and its dependencies using the following command:
sudo apt install r-base r-base-dev -y
The r-base-dev
package enables users to install new packages using the install.packages()
function within the R console.
3. Type R
and hit enter to verify R has been installed:
R
R has been successfully installed, and you have access to the R console prompt.
Type q()
to exit the R console.
Method 2: Install R on Ubuntu from CRAN Repository
To install R from the CRAN repository:
1. Update the apt package lists:
sudo apt update
2. Install the software-properties-common
and dirmngr
helper packages for managing CRAN repositories and certificates:
sudo apt install software-properties-common dirmngr -y
Note: In newer Ubuntu versions, the software-properties-common
and dirmngr
packages are usually preinstalled, as shown in the example above.
3. Download and add the signing key for the CRAN repository with wget:
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
4. The CRAN repository key fingerprint is E298A3A825C0D65DFD57CBB651716619E084DAB9
. Verify the downloaded key using the following command:
gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
5. Add the CRAN repository to your system:
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
Using $(lsb_release -cs)
instructs the system to automatically recognize which Ubuntu version your machine is running.
6. Update the repositories again:
sudo apt update
7. Enter the following command to install the base R package and its dependencies from the CRAN repository:
sudo apt install r-base r-base-dev -y
The r-base-dev
package allows users to install packages from the R console using the install.packages()
function.
8. Enter the R
command to access the R console:
R
You have successfully installed the latest R version (4.4.2) from the CRAN repository.
Installing R Packages
The R community actively develops and maintains thousands of packages for specific tasks like data visualization, machine learning, statistical modeling, text mining, etc.
Some popular R packages include:
- dplyr. Provides practical and intuitive functions for summarizing, filtering, and transforming data.
- tidyr. Enables users to reshape and organize messy data into a tidy format.
- purrr. Offers functional programming package to improve flexibility and consistency when working with functions and vectors.
- ggplot2. Creates and customizes visually appealing plots.
- data.table. Manipulates data in large datasets.
- stringr. Provides functions for streamlining string and text manipulation tasks.
- lubridate. Working with dates and times, making it easier to parse and manipulate them.
- shiny. Builds interactive web applications.
- caret. Builds predictive models and handles machine learning workflows.
To install R packages from within the R console, use the following syntax:
install.packages("package_name")
This standard syntax allows you to download and install packages from CRAN. For example, if you want to install the ggplot2
package, which is widely used for data visualization, enter the following command:
install.packages("ggplot2")
If you encounter the 'lib = "/usr/local/lib/R/site-library"' is not writable
prompt, type yes
to use and create a personal library for your user.
Once the installation completes, load the package into your R environment using the following command.
library(ggplot2)
However, the command doesn't produce any output unless there is an error. However, the process makes the functions and features of ggplot2
available for use.
The ggplot2
package also includes many preinstalled datasets for users to test its functionalities. To view the list of these datasets, use:
data()
This displays a list of available datasets. Press q
to exit the list and return to the R console.
For example, to explore the variables in the CO2
dataset, use the summary()
function, which provides a concise overview of the dataset's statistics:
summary(CO2)
To visualize the relationship between numeric and categorical variables, use the ggplot()
function.
For example, in the CO2
dataset, plot the Tree
variable (categorical) against the circumference
variable (numeric) with the following command:
ggplot(Orange, aes(x=Tree, y=circumference, col=age))+geom_point()
R generates a visual statistical plot in a new window.
The ggplot2 package is one of many R packages for static data visualization. CRAN hosts thousands of packages designed for a wide range of data-related tasks.
Explore and discover more packages by browsing the official CRAN package repository.
Note: Learn about the differences between R and Python.
Conclusion
This tutorial explained how to install R on Ubuntu using two different methods. It also elaborated on how to install and manage different R packages.
It is common for researchers to use R in tandem with Python libraries like pandas. By combining R and Python, you can take advantage of the vast range of packages and functionalities available in both ecosystems.