How to Install R on Ubuntu 18.04
Updated on
•6 min read

R is a fast growing open-source programming language and free environment that specializes in statistical computing and graphical representation. It is supported by the R Foundation for Statistical Computing and mainly used by statisticians and data miners for developing statistical software and performing data analysis.
This tutorial will guide you through the steps of installing R on an Ubuntu 18.04 machine.
Prerequisites
Before you get started with this tutorial, you’ll need an Ubuntu 18.04 machine with:
- at least 1G of RAM. If your system has less than 1GB of RAM, you can create a swap file .
- a non-root user with sudo privileges .
Installing R on Ubuntu
At the time of writing this article, the latest stable version of R is version 3.5. The R packages from the Ubuntu repositories are often outdated so we’ll install R by adding the repository maintained by CRAN .
To install the latest stable version of R on Ubuntu 18.04, follow these steps:
Install the packages necessary to add a new repository over HTTPS:
sudo apt install apt-transport-https software-properties-common
Enable the CRAN repository and add the CRAN GPG key to your system 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 bionic-cran35/'
Now that the apt repository is added , update the packages list and install the R package by typing:
sudo apt update
sudo apt install r-base
To verify that the installation was successful run the following command which will print the R version:
R --version
R version 3.5.1 (2018-07-02) -- "Feather Spray" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under the terms of the GNU General Public License versions 2 or 3. For more information about these matters see http://www.gnu.org/licenses/.
Installing R Packages from CRAN
One of the main reasons why R is so popular is the wide array of packages available through the Comprehensive R Archive Network (CRAN).
Install the build-essential
package which contains the tools required for compiling R Packages.
sudo apt install build-essential
For demonstration purposes, we’ll install a package named stringr
, which provides fast, correct implementations of common string manipulations.
When started as root the packages will be installed globally and available for all system users. If you start R without sudo, a personal library will be set up for your user.
Start by opening the R console as root:
sudo -i R
R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
Install the stringr
package by typing:
install.packages("stringr")
The installation will take some time and once complete, load the library with:
library(stringr)
Create a simple character vector named tutorial
:
tutorial <- c("How", "to", "Install", "R", "on", "Ubuntu", "18.04")
Run the following function which prints the length of a string:
str_length(tutorial)
[1] 3 2 7 1 2 6 5
You can find more R packages at Available CRAN Packages By Name
and install them with install.packages()
.
Conclusion
You have successfully installed R your Ubuntu system and learned how to install R packages.
If you hit a problem or have feedback, leave a comment below.