Read in 3 minutes
How to Install R on Ubuntu 18.04
R is a fast growing open source programming language and free environment that specializes in statistical computing and graphics 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.
Before you get started with this tutorial, you’ll need an Ubuntu 18.04 machine with:
- at least 1G of RAM. If you are using a server with less than 1GB of RAM, you can create a swap file.
- you are logged in as a 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:
The first step is to enable the CRAN repository 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/'
If you get an error message saying
add-apt-repository command not foundthen you will need to install the
Now that the 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 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 reason why R is so popular is the wide array of packages available through the Comprehensive R Archive Network (CRAN).
Before proceeding you need to install the
build-essential package which contains the tools required for compiling R Packages.
sudo apt-get install build-essential
For demonstration purposes, we’ll install a package named
stringr, which provides fast, correct implementations of common string manipulations.
Start by opening the R console:
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. >
All the following commands are executed within a R console.
To install the
stringr package simply type:
When the installation is complete, load the library:
Create a simple character vector named
tutorial <- c("How", "to", "Install", "R", "on", "Ubuntu", "18.04")
Run the following function which prints the length of a string:
 3 2 7 1 2 6 5
You can find more R packages at Available CRAN Packages By Name and install them with
You have successfully installed R your Ubuntu system and learned how to install R packages.
If you hit a problem or have a feedback, leave a comment below.