Read in 3 minutes

last updated 

How to Install R on Debian 9

R is a fast growing open source programming language and 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 walk you through how to install R on an Debian 9 system.

Prerequisites

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

Installing R on Debian

At the time of writing this article, the latest stable version of R is version 3.5. The R packages from the Debian repositories are often outdated so we’ll install R by adding the repository maintained by CRAN.

The following steps describe how to install the latest stable version of R on Debian 9:

  1. Install the dependencies necessary to add a new repository over HTTPS:

    sudo apt install dirmngr apt-transport-https ca-certificates software-properties-common gnupg2
  2. Enable the CRAN repository and add the CRAN GPG key to your system by running the following commands:

    sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C2AD5F960A256A04AF'
    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/debian stretch-cran35/'
  3. Once the repository is added, update the packages list and install the R package by typing:

    sudo apt update
    sudo apt install r-base
  4. Verify the installation by running the following command which will print 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
    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).

If you already don’t have installed, 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.

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

install.packages("stringr")

Installation will take some time and once completed, load the library:

library(stringr)

Create a simple character vector named tutorial:

tutorial <- c("How", "to", "Install", "R", "on", "Debian", "9")

Run the following function which prints the length of a 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 Debian system and learned how to install R packages.

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