Installing R on Ubuntu for performing data analysis is an easy task. R is widely used in statistical computing and data analytics tasks. Learning to install R on Ubuntu for statistical studies is a must for every data analysis.
With the support of the R Foundation and the open-source community, R consists of many user-generated packages. This tutorial will cover the basic guide on how to install R and run the R packages on Ubuntu.
We will also examine how you can install R Studio, the famous R Integrated Development Environment (IDE), and R packages from Comprehensive R Archive Network (CRAN).
Prerequisites
For this tutorial, you will need a Ubuntu system with root privilege or sudo access. You will also need at least 1 GB of RAM.
Install R on Ubuntu
Unlike Python, which consists of various data science and machine learning packages, R only consists of special statistical packages. Statistical functions and visualizations are its core functionality. Also, R is available for all the major operating systems, including Windows and Linux. Let’s get started with the installation of R on Ubuntu.
1. Update the System
The first step is to update the system. This will ensure that all the existing packages and repositories are up to date. Open the Terminal by pressing “Ctrl + Alt + T”. Execute the commands given below:
sudo apt update && sudo apt upgrade
You should get a similar output as seen below:

2. Install the Required Packages
Secondly, install the dependencies required for the R language installation. In this step, we will install CA Certificates and APT Transport. Use the install
command to perform this task.
For example:
sudo apt install dirmngr gnupg apt-transport-https ubuntu-keyring ca-certificates software-properties-common -y
The output should look something like this:

Type “Y” and hit the “Enter” key.
Make sure to wait for the installation to complete.
3. Import R GPG Key and CRAN Repository
The default R package in Ubuntu’s repository is outdated. For this, we will install R from the CRAN repository. First, import the GPG key. This GPG key is used to verify the R’s authenticity. Execute the command given below to import the GPG key:
wget -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor sudo tee /usr/share/keyrings/cran.gpg
Secondly, import the CRAN repository and add the R to the sources.list.d
directory as shown below:
For example:
echo deb [signed-by=/usr/share/keyrings/cran.gpg] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/ | sudo tee /etc/apt/sources.list.d/cran.list
Alternatively, you can add the GPG Key and repository using the following commands:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
You should get a similar output:

Lastly, wait for the process to finish adding a repository link. After that, refresh the APT package using the apt update
command.
Specifically:
sudo apt update
4. Install R
Now that all dependencies are installed, and you have imported the required links, it is time to install R. Execute the command given below:
sudo apt install r-base -y
You should get a similar output as this:

Additionally, you can install the r-base-dev.
For instance, type:
sudo apt install r-base r-base-dev -y
Furthermore, you can also use the command given below to install R language:
sudo apt install --no-install-recommends r-base
The no-install-recommends
option ensures that no extra packages are installed. Once it is installed, verify it using the –version
option.
For instance:
R --version
The output should look something like this:

Finally, you have installed R on Ubuntu successfully.
5. Install R Packages from CRAN
Most statistical functions require different R packages. You can install these packages easily after launching the R Terminal. To launch the Terminal, type the following command:
sudo -i R
The R Shell Terminal will open:

Now, to install any package, use the install.packages
command. For instance, we will install the txtplot package. This package allows users to create graphs, charts, and plots using R. To install the txtplot package, type:
install.packages('txtplot')
The R Shell will start installing the package as shown below:

Next, activate the installed package.
For example:
library('txtplot')
You can use this package to plot any graph. For example, we will use a Cars dataset to plot a graph between the speed and distance of cars. The cars dataset already exists in the datasets package. Also, you do not have to install the datasets package as it is part of the base package in the R Shell Terminal.
To plot the graph using txtplot, write:
txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')
You should get a similar output as this:

In addition, to view the help related to any package, use the help command.
The syntax:
help(package name)
Example:
help(txtplot)
6. Remove R Packages
Alternatively, to remove any package, use the remove
command. The syntax for this command is:
remove.packages(“package name”);
For example, to remove the txtplot package, type:
remove.packages('txtplot')
7. Exit the R Terminal
Use the command “q()
” to exit the R Terminal. Specifically, type:
q()
You will get a prompt asking to save the R shell Terminal history as shown below:

Type the appropriate option and press the “Enter” key.
Download and Install R Studio
R Studio is an interactive software for the R language. To install it on Ubuntu, grab the package using the wget
command. For example:
wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1l-1ubuntu1.2_amd64.deb
sudo apt-get install ./libssl1.1_1.1.1l-1ubuntu1.2_amd64.deb
Make sure to replace the version in the link with the current version. Proceed with the installation and wait for it to complete. When it is done, launch the software using the command:
rstudio
To run R Studio without a sandbox, use the --no-sandbox
option.
Specifically, type:
rstudio --no-sandbox
In this article, you have learned how to install the R language and the CRAN package installation. You also learned how to install R Studio and launch it for statistical applications. Additionally, you can visit the CRAN page to install the latest R packages in your system. We hope you found this article useful.
If this guide helped you, please share it.