Linux Tutorials, Guides & Latest Tech Stuffs

How to install R on Ubuntu 18.04

R is a popular open source programming language, often use for graphical and statistical computing. R was created by Ross Ihaka and Robert Gentleman. This post will talk about quick installation of R on Ubuntu 18.04 LTS Bionic Beaver. We will also talk about installing and using packages from CRAN ( Comprehensive R Archive Network )

Prerequisites

For installation of R on Ubuntu 18.04 server, we should have

  • atleast 1GB RAM size
  • a non root user with sudo privileges

Step-1: Add GPG Key

Login to your Ubuntu 18.04 Server with non root user and add GPG Keys with following command. GPG Keys maintain package consistency and authenticity by requiring that distributors sign packages with GPG key.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

After running this command, we’ll see following output on terminal

Step-2: Add R official repository source

After adding GPG Key we will add R official repository source.

sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'

Above repository source is for Ubuntu 18.04, If you are not using Ubuntu 18.04 Bionic Beaver LTS.  You can check other repository from the R project Ubuntu packages List

After running above command, Ubuntu will fetch list of all repository and it will also show latest one added, like this

Step-3: Update Ubuntu packages

Now we’ll run update command for loading files from new repository.

sudo apt-get update

Among the output lines, you should see following line.

Step-4: Install R

As repository source file is in place, now we can install R using command

sudo apt install r-base

After running this command terminal will prompt for yes or no as a final input, you have to type in “yes”  and installation will be started. It takes some time to install successfully, In my case total download size was 128 MB.

For verifying R installation you can run simply type R  and hit Enter on terminal.

R

On running “R” command,  you should see following output on terminal

For exiting from R, you can run q(). It will ask for saving workspace image, do as required to quit.

Step – 5:  Installing R packages from CRAN

The popularity of R language can be somewhat attributed to its large store of packages and its simplicity for its installation. For installing a CRAN package in R we use  install.pacakges() function. This command fetches package from specified repository and install on your computer.

For example,  we’ll be installing ggplot2 package by using following command.

install.packages("ggplot2")

After running this command, R started to download package data from https://cloud.r-project.org and compile it. you will see number of lines on terminal, at the time of compilation.

For loading a R package, you can use following command.

library("packagename")

Verfying installed packages in R

you can check whether a particular package is installed or not by using packageVersion function.

packageVersion("ggplot2")

You should see output like this,depending upon version of package. It is output of above command.

ggplot2 examples

If you are looking for ggplot2 examples you can check this website.

http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

Conclusion

Thankyou for following this tutorial, R is now successfully installed on your machine. In case if you are getting any error write to us at nixgyd[at]gmail.com

You can also ask question about any issue by using comment section below. Thanks for reading.

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.