How to install R on Ubuntu 18.04

s2 0

R is a fast growing open source programming language and free environment that specializes in statistical calculations and graphical representations. It is supported by the R Foundation for Statistical Computing and is mainly used by statisticians and data miners for developing statistical software and performing data analysis.

This tutorial will walk you through the steps to install R on an Ubuntu 18.04 computer.

requirements

Before starting this tutorial you will need an Ubuntu 18.04 computer with:

  • at least 1 GB of RAM. If your system has less than 1 GB of RAM, you can create a paging file.
  • a non-root user with sudo privileges.

Install R on Ubuntu

At the time of this writing, the latest stable release of R is version 3.5. The R packages from the Ubuntu repositories are often out of date, so we’ll install R by adding the repository managed by CRANE
.

To install the latest stable version of R on Ubuntu 18.04, do the following:

  1. Install the packages required to add a new repository over HTTPS:

    sudo apt install apt-transport-https software-properties-common
  2. Activate 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 E298A3A825C0D65DFD57CBB651716619E084DAB9sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
  3. After the apt repository is added, update the package list and install the R package by typing:

    sudo apt updatesudo apt install r-base
  4. To verify that the installation was successful, run the following command that prints 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
    https://www.gnu.org/licenses/.

Install R packages from CRAN

One of the main reasons R is so popular is the wide range of packages available through the Comprehensive R Archive Network (CRAN).

Install that build-essential Package that contains the tools required to compile R packages.

sudo apt install build-essential

For demonstration purposes, we’ll install a package called stringr

, which provides quick, correct implementations of common string manipulations.

When starting as root, the packages are installed globally and are available to all system users. If you start R without sudo, a personal library will be set up for your user.

First, open 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.

>
All of the following commands are executed from within the R console.

Install that stringr Package by entering:

install.packages("stringr")

The installation will take some time and when done, load the library with:

library(stringr)

Create a simple drawing vector called tutorial:

tutorial <- c("How", "to", "Install", "R", "on", "Ubuntu", "18.04")

Run the following function that returns 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 it with install.packages().

diploma

You have successfully installed R on your Ubuntu system and learned how to install R packages.

If you run into any problem or have any feedback, please leave a comment below.