Read in 3 minutes

last updated 

How to Install R on CentOS 7

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 teach you how to install R on an CentOS 7 and how to install R packages from the official Comprehensive R Archive Network (CRAN).

Prerequisites

Ensure that you have met the following prerequisites before continuing with this tutorial:

Installing R on Centos

At the time of writing the latest stable version of R is version 3.5.

To install R on CentOS 7, follow these steps:

  1. R packages are available in the EPEL repositories. If you don’t have EPEL repository installed on your machine you can do it by typing:

    sudo yum install epel-release

    You will be prompted to accept the repository GPG key, type y and hit Enter.

  2. Once the repository is added, install R by typing:

    sudo yum install R

    R is a meta package that contains all the necessary R components.

  3. Verify the installation by typing the following command which will print the R version:

    R --version
    R version 3.5.0 (2018-04-23) -- "Joy in Playing"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-redhat-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/.
Advertisement

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).

For demonstration purposes, we’ll install a package named stringr, which provides fast and 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 a root:

sudo -i R
R version 3.5.0 (2018-04-23) -- "Joy in Playing"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-redhat-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.

Install the stringr package by running the following command:

install.packages("stringr")

You will be asked to select a CRAN mirror:

Installing package into ‘/usr/lib64/R/library’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
Secure CRAN mirrors 

Select the mirror that is closest to your location.

The installation will take some time and once completed, load the library by typing:

library(stringr)

Next, create a simple character vector named tutorial:

tutorial <- c("How", "to", "Install", "R", "on", "CentOS", "7")

Run the following function which will print the length of each string:

str_length(tutorial)
[1] 3 2 7 1 2 6 1

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 CentOS machine and learned how to install R packages.

If you hit a problem or have a feedback, leave a comment below.