Cyberithub

How to Install R Programming on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

In this article, I will take you through the steps to install R programming on Ubuntu 20.04 LTS (Focal Fossa). R is a very famous programming language for statistical computing and graphics representation and reporting. It was developed by John Chambers and his colleagues at Bell Laboratories. It looks very similar to the S language and environment but it is actually a different implementation of S. Still, you will find many codes in S implemented as it is in R. It is highly extensible and provides wide variety of statistical such as linear and nonlinear modelling, classical statistical tests, time-series analysis, classification etc and graphical techniques.

R is available as free software under the GNU license to be used in almost all the famous platforms like Windows, Linux and Mac. Here we will the steps to install R programming on Ubuntu 20.04 LTS based systems. More on official website.

 

Features

  • It provides an effective data handling and storage facility.
  • It has an excellent suite of operators for calculations on arrays, in particular matrices.
  • It has a large, coherent, integrated collection of intermediate tools for data analysis.
  • It provides graphical facilities for data analysis and display either on-screen or on hardcopy.
  • It has a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

 

How to Install R Programming on Ubuntu 20.04 LTS (Focal Fossa)

How to Install R Programming on Ubuntu 20.04 LTS (Focal Fossa)

Also Read: How to Install libomp-dev on Ubuntu 20.04 LTS (Focal Fossa)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get and wget utility available in your Server.

 

Step 2: Update Your Server

Before going through the steps to install R software environment, it is always recommended to install all the available updates using sudo apt update command and upgrade the packages to the latest version by using sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
Hit:2 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:3 https://dl.google.com/linux/chrome/deb stable InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:5 https://deb.tableplus.com/debian/20 tableplus InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:1 https://downloads.apache.org/cassandra/debian 40x InRelease
Get:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [59.9 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,384 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [797 kB]
Get:11 http://security.ubuntu.com/ubuntu focal-security/universe i386 Packages [581 kB]
Get:12 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [156 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [788 kB]
.........................................................

 

Step 3: Install R Programming

There are multiple ways to install R packages on Ubuntu Linux based systems. You can choose any of the below given method based on your requirement.

a) Using Default Repo

First and the simplest way to install R packages is through default Ubuntu repo by using sudo apt install r-base command as shown below. This will download and install the package along with all its dependencies.

cyberithub@ubuntu:~$ sudo apt install r-base
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libomp-10-dev libomp5-10
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
bzip2-doc gfortran gfortran-9 libblas-dev libblas3 libbz2-dev libgfortran-9-dev libgfortran5 libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev liblapack-dev
liblapack3 liblzma-dev libncurses-dev libncurses5-dev libpcre16-3 libpcre2-16-0 libpcre2-dev libpcre2-posix2 libpcre3-dev libpcre32-3 libpcrecpp0v5
libpng-dev libpng-tools libreadline-dev libtcl8.6 libtk8.6 libxss1 r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster
r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart
r-cran-spatial r-cran-survival r-doc-html r-recommended
Suggested packages:
gfortran-multilib gfortran-doc gfortran-9-multilib gfortran-9-doc libcoarrays-dev liblapack-doc liblzma-doc ncurses-doc readline-doc tcl8.6 tk8.6
elpa-ess r-doc-info | r-doc-pdf r-mathlib texlive-base texlive-latex-base texlive-plain-generic texlive-fonts-recommended texlive-fonts-extra
texlive-extra-utils texlive-latex-recommended texlive-latex-extra texinfo
The following NEW packages will be installed:
bzip2-doc gfortran gfortran-9 libblas-dev libblas3 libbz2-dev libgfortran-9-dev libgfortran5 libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev liblapack-dev
liblapack3 liblzma-dev libncurses-dev libncurses5-dev libpcre16-3 libpcre2-16-0 libpcre2-dev libpcre2-posix2 libpcre3-dev libpcre32-3 libpcrecpp0v5
libpng-dev libpng-tools libreadline-dev libtcl8.6 libtk8.6 libxss1 r-base r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster
r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart
r-cran-spatial r-cran-survival r-doc-html r-recommended
0 upgraded, 50 newly installed, 0 to remove and 0 not upgraded.
Need to get 66.7 MB of archives.
After this operation, 160 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
..............................................

b) Using CRAN Repo

Next method you can use to install R packages is through CRAN network. But before that you need to first install two helper packages using below sudo apt install --no-install-recommends software-properties-common dirmngr command.

cyberithub@ubuntu:~$ sudo apt install --no-install-recommends software-properties-common dirmngr
Reading package lists... Done
Building dependency tree
Reading state information... Done
dirmngr is already the newest version (2.2.19-3ubuntu2.2).
software-properties-common is already the newest version (0.99.9.10).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

Then download and add the GPG Key using below wget command.

cyberithub@ubuntu:~$ 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
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQENBEy9tcUBCACnWQfqdrcz7tQL/iCeWDYSYPwXpPMUMLE721HfFH7d8ErunPKP
Iwq1v4CrNmMjcainofbu/BfuZESSK1hBAItOk/5VTkzCJlzkrHY9g5v+XlBMPDQC
9u4AE/myw3p52+0NXsnBz+a35mxJKMl+9v9ztvueA6EmLr2xaLf/nx4XwXUMSi1L
p8i8XpAOz/Xg1fspPMRhuDAGYDnOh4uH1jADGoqYaPMty0yVEmzx74qvdIOvfgj1
6A/9LYXk67td6/JQ5LFCZmFsbahAsqi9inNgBZmnfXO4m4lhzeqNjJAgaw7Fz2zq
UmvpEheKKClgTQMWWNI9Rx1L8IKnJkuKnpzHABEBAAG0I01pY2hhZWwgUnV0dGVy
IDxtYXJ1dHRlckBnbWFpbC5jb20+iQFVBBMBCgA/AhsjBgsJCAcDAgYVCAIJCgsE
FgIDAQIeAQIXgBYhBOKYo6glwNZd/VfLtlFxZhnghNq5BQJfdcXRBQkf4HiMAAoJ
EFFxZhnghNq5Nj8IAIqCTIJ/tMjZqiuwe7M4DQjpvGn4/QylkYLxn9dOds2nGz82
FAukEAR3tmi22TETrqm+UOukFYUnAEOez7BJiWPwCDt9WuD4/Be8rzbjL0wEv/xu
D1JHWxVd2DfbJG1PAsuv7ouFYbIdh24a8gYpasd6twdbPOdZa0mbzVOnwAmHiH1l
gv2oY/McOO7unc/YGuD2lTb5ohK8yYdHsL5WYK7REPGqEGBS+ppUPWjI1qQb/g2X
oSVfbLLiXiwdvBS4hzjiwGNe76B2jDuHsfmQFMdVpOzNKN0dk10Nm8MFFPrirUIl
Uwk45UttOrcrs5hluLYZhMYHPJ/UNYnaJeJH58iJAT4EEwECACgFAky9tcUCGyMF
CQlmAYAGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEFFxZhnghNq5PL0H/iGY
............................................

Next you need to add the repo using below add-apt-repository command.

cyberithub@ubuntu:~$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
Hit:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Get:5 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease [3,622 B]
Hit:6 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:7 https://deb.tableplus.com/debian/20 tableplus InRelease
Hit:8 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:1 https://downloads.apache.org/cassandra/debian 40x InRelease
Get:9 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ Packages [61.7 kB]
Fetched 65.3 kB in 3s (19.1 kB/s)
Reading package lists... Done

After adding the repo, you need to update the package cache about recently added repo using sudo apt update command as shown below.

cyberithub@ubuntu:~$ sudo apt update

Then you can install R and all its dependencies from the added repo using below sudo apt install --no-install-recommends r-base command.

cyberithub@ubuntu:~$ sudo apt install --no-install-recommends r-base
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libblas3 libgfortran5 liblapack3 libtcl8.6 libtk8.6 libxss1 r-base-core r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign
r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-recommended
Suggested packages:
tcl8.6 tk8.6 elpa-ess r-doc-info | r-doc-pdf r-mathlib r-base-html
Recommended packages:
r-base-html r-doc-html r-base-dev
The following NEW packages will be installed:
libblas3 libgfortran5 liblapack3 libtcl8.6 libtk8.6 libxss1 r-base r-base-core r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign
r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-recommended
0 upgraded, 24 newly installed, 0 to remove and 2 not upgraded.
Need to get 50.8 MB of archives.
After this operation, 87.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
..........................................................

 

Step 4: Check Version

After successful installation, you can check the current installed version by using R --version command as shown below.

cyberithub@ubuntu:~$ R --version
R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 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/.

 

Step 5: Launch R Shell

To launch R shell, you need to go to your terminal and run sudo R command as shown below.

cyberithub@ubuntu:~$ sudo R
[sudo] password for cyberithub:

R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 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.
>

 

Step 6: Install a Package

In R programming language, a package is basically a set of functions. To use a specific function, you need to install a package and then load the library. Here we are going to use a library called stringr. So to use this library, you need to first install the library package from R console by using install.packages("stringr") as shown below.

> install.packages("stringr")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘cli’, ‘glue’, ‘lifecycle’, ‘magrittr’, ‘rlang’, ‘stringi’, ‘vctrs’

trying URL 'https://cloud.r-project.org/src/contrib/cli_3.6.0.tar.gz'
Content type 'application/x-gzip' length 565150 bytes (551 KB)
==================================================
downloaded 551 KB

trying URL 'https://cloud.r-project.org/src/contrib/glue_1.6.2.tar.gz'
Content type 'application/x-gzip' length 106510 bytes (104 KB)
==================================================
downloaded 104 KB

trying URL 'https://cloud.r-project.org/src/contrib/lifecycle_1.0.3.tar.gz'
Content type 'application/x-gzip' length 106854 bytes (104 KB)
==================================================
downloaded 104 KB

trying URL 'https://cloud.r-project.org/src/contrib/magrittr_2.0.3.tar.gz'
Content type 'application/x-gzip' length 267074 bytes (260 KB)
==================================================
downloaded 260 KB
.....................................................

 

Step 7: Write a Program

Once the stringr package is installed, you can now load the library using library(stringr) and then write a simple character vector named cyberithub as shown below.

> library(stringr)
> cyberithub <- c("Hi", "This", "is", "from", "CyberITHub")
> str_length(cyberithub)
[1] 2 4 2 4 10

To quit from the R shell, you need to use q() as shown below.

> q()
Save workspace image? [y/n/c]: n

 

Step 8: Uninstall R Programming

Once you are done using R programming, you can choose to uninstall the R packages by using sudo apt remove r-base command as shown below.

NOTE:

Please be careful in running below command as sometimes you might end up in removing some of the packages which are used by some other active running applications
cyberithub@ubuntu:~$ sudo apt remove r-base
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gfortran gfortran-9 libblas-dev libblas3 libbz2-dev libgfortran-9-dev libgfortran5 libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev liblapack-dev liblapack3
liblzma-dev libncurses-dev libncurses5-dev libomp-10-dev libomp5-10 libpcre16-3 libpcre2-16-0 libpcre2-dev libpcre2-posix2 libpcre3-dev libpcre32-3
libpcrecpp0v5 libpng-dev libpng-tools libreadline-dev libtcl8.6 libtk8.6 libxss1 r-base-core r-base-dev r-base-html r-cran-boot r-cran-class
r-cran-cluster r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet
r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
r-base
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 62.5 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 199208 files and directories currently installed.)
Removing r-base (3.6.3-2) ...

Leave a Comment