How to Install R on Debian 9

By 

Updated on

4 min read

How to Install R on Debian 9

R is a fast growing open-source programming language and environment that specializes in statistical computing and graphical 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 a 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:

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

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

    Terminal
    sudo apt update
    sudo apt install r-base
  4. Verify the installation by running the following command which will print the R version:

    Terminal
    R --version
    output
    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/.

Installing R Packages from CRAN

One of the main reasons 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.

Terminal
sudo apt 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 root:

Terminal
sudo -i R
output
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.

> 
Info
All the following commands are executed within the R console.

To install the stringr package simply type:

R
install.packages("stringr")

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

R
library(stringr)

Create a simple character vector named tutorial:

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

Run the following function which prints the length of a string:

R
str_length(tutorial)
output
[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 feedback, leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page