R is a programming language that specializes in working with data. R is free software that supports an extensive catalog of statistical and graphical methods. The list includes various machine learning algorithms, time series, linear regression, and more. It’s used by industry giants like Google, Facebook, Airbnb, Uber, etc.
As the description suggests, R is the go-to option when big data is involved. It’s used for data analysis, machine learning, and statistical inference, to name a few. In this guide, we will go through the steps of installing R on Ubuntu 21.04.
Prerequisites
Here are a couple of prerequisites required to perform the steps demonstrated in this guide:
-
A properly-configured Ubuntu 21.04 server. Learn more about configuring your own Ubuntu server.
-
A non-root user with the permission of running sudo commands. This guide elaborates on managing sudo permission.
R on Ubuntu
There are two ways of installing R on Ubuntu:
-
Ubuntu package repo: Because it’s a popular language, R is directly available from the official Ubuntu package repositories. However, because of the way Ubuntu releases package updates, it’s often not the latest version of R.
-
CRAN (The Comprehensive R Archive Network) project: CRAN is a network of FTP and web servers that serve the up-to-date versions of code and documentation of R for various platforms. It offers a dedicated repo for Ubuntu and it comes with the latest version of R.
We will demonstrate both methods of installing R. However, it’s strongly recommended to install R from the CRAN repo as it offers the latest packages. Whatever method you choose, R will be installed and available for all users of the system.
-
Method 1 – Installing R from Ubuntu Default Repo
First, open up the terminal and update the APT cache:
1 |
sudo apt update |
Next, install R from the Ubuntu package repo:
1 |
sudo apt install r-base |
-
Method 2 – Installing R from CRAN Repo
The CRAN repo supports Ubuntu LTS releases for the most part. Check the CRAN documentation on Ubuntu repo to see if your version is supported.
Step 1- Adding the CRAN Repo
To add the CRAN repo, open up a terminal, and run the following commands. First, update the APT cache:
1 |
sudo apt update |
There are two helper packages needed to work with CRAN and R. Install them right away:
1 |
sudo apt install software-properties-common dirmngr |
Next, we need to add the signing key for the CRAN repo. Download and add the key:
1 |
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 |
We can also verify the key if it was the right one. The fingerprint should be E298A3A825C0D65DFD57CBB651716619E084DAB9:
1 |
gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc |
Finally, add the CRAN repo for Ubuntu:
1 |
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" |
Update the APT cache:
1 |
sudo apt update |
Step 2 – Installing R from CRAN
Now, APT will download and install R from the CRAN repo. Install R:
1 |
sudo apt install r-base |
Working with R Shell
In this section, we will have a simple demonstration of using the R shell. We will install an R package stringr. It comes with a set of cohesive functions designed to transform working with strings as simple as possible. First, check out stringr here.
Launch the R shell:
1 |
sudo -i R |
Then, you can install the package:
1 |
install.packages("stringr") |
Once the installation is complete, load stringr into the current R session:
1 |
library('stringr') |
One function of stringr is str_length that will print the length of strings. Create a string:
1 |
hello_world <- c("the", "quick", "brown", "fox") |
Now, check the length of each of the strings:
1 |
str_length(hello_world) |
Like Linux, R also comes with a neat documentation feature for its packages. Check out the documentation of string:
1 |
help(stringr) |
To exit the R shell, use the following code:
1 |
q() |
Final Thoughts
This guide successfully demonstrates configuring and installing R on Ubuntu 21.04 server. The entire procedure is simple and quick. It also features a quick demonstration of using the R shell and working with R packages. For more information on how to take control of your R code, check out R Studio – an open-source IDE for R.
Happy Computing!
- How To Enable, Create and Use the .htaccess File: A Tutorial - March 8, 2023
- An Overview of Queries in MySQL - October 28, 2022
- Introduction to Cookies: Understanding and Working with JavaScript Cookies - October 25, 2022
- An Overview of Data Types in Ruby - October 24, 2022
- The Architecture of Iptables and Netfilter - October 10, 2022