Skip to main content

How to Install R on Debian/Ubuntu

This guide explains how to install the R programming language on your Debian or Ubuntu server. R is an open-source language widely used for developing statistical software, data analysis, and visualization.

Order a Server

To run R and your data analyses, HostMyServers offers several options:

Prerequisites

  • SSH access as root or user with sudo
  • Debian 11/12 or Ubuntu 20.04/22.04/24.04
  • Minimum 1 GB RAM (2 GB+ recommended for heavy computations)

Why Use R?

R is particularly suited for:

  • Statistical Analysis: Tests, regression models, analysis of variance
  • Data Visualization: Publication-ready graphics with ggplot2
  • Machine Learning: Classification, clustering, neural networks
  • Bioinformatics: Genomic and proteomic analysis
  • Finance: Risk analysis, financial modeling

Step 1: Install Dependencies

Since R is an actively developed project, the latest stable version is not always available in Debian/Ubuntu repositories. We will add the external repository maintained by CRAN.

Update the System

sudo apt update && sudo apt upgrade -y

Install Required Tools

sudo apt install -y dirmngr gnupg apt-transport-https ca-certificates software-properties-common

These packages provide:

  • dirmngr: Certificate management and network operations
  • gnupg: GPG key management
  • apt-transport-https: HTTPS support for APT
  • ca-certificates: Certificate authority certificates
  • software-properties-common: Repository management

Step 2: Add CRAN Repository

Add the GPG Key

sudo gpg --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'
sudo gpg --armor --export '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7' | sudo tee /etc/apt/trusted.gpg.d/cran_debian_key.asc

Add the Repository for Your Distribution

For Debian 11 (Bullseye)

echo "deb http://cloud.r-project.org/bin/linux/debian bullseye-cran40/" | sudo tee /etc/apt/sources.list.d/r-project.list

For Debian 12 (Bookworm)

echo "deb http://cloud.r-project.org/bin/linux/debian bookworm-cran40/" | sudo tee /etc/apt/sources.list.d/r-project.list

For Ubuntu 22.04 (Jammy)

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/"

For Ubuntu 24.04 (Noble)

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu noble-cran40/"

Update Package List

sudo apt update

Step 3: Install R

Check Available Version

apt-cache policy r-base

Install R

sudo apt install -y r-base r-base-dev

The r-base-dev package includes development tools needed to compile R packages from source.

Verify Installation

R --version

You should see something like:

R version 4.4.x (2024-xx-xx) -- "Pile of Leaves"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

Step 4: Using R

Launch R in Interactive Mode

For personal use:

R

To install packages available to all users:

sudo -i R

R Interface

Once in R, you will see the > prompt. Here are some basic commands:

# Display version
version

# Get help
help()

# Quit R
q()

Step 5: Install Packages from CRAN

R has thousands of packages available on CRAN (Comprehensive R Archive Network).

Install a Package

In the R interpreter:

install.packages('package_name')

Example: Install Common Packages

# Data manipulation
install.packages('dplyr')
install.packages('tidyr')
install.packages('data.table')

# Visualization
install.packages('ggplot2')

# Machine Learning
install.packages('caret')
install.packages('randomForest')

# Reports
install.packages('rmarkdown')
install.packages('knitr')

Load a Package

library(ggplot2)

Practical Example with txtplot

Let's install a simple package to create ASCII charts:

# Install the package
install.packages('txtplot')

# Load the package
library('txtplot')

# Create a simple plot
txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')

Result:

    +----+--------+--------+-------+--------+--+
120 + * +
| |
d 100 + * +
i | * * |
s 80 + * * +
t | * * * |
a 60 + * +
n | * * * |
c 40 + * ** +
e | * * * * |
20 + * * * +
+----+--------+--------+-------+--------+--+
5 10 15 20 25
speed

Step 6: Install RStudio Server (Optional)

RStudio Server provides a web interface for R.

Download and Install RStudio Server

For Debian/Ubuntu:

# Install dependencies
sudo apt install -y gdebi-core

# Download RStudio Server (check the latest version at https://posit.co/download/rstudio-server/)
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2024.04.2-764-amd64.deb

# Install
sudo gdebi rstudio-server-2024.04.2-764-amd64.deb

Check Status

sudo systemctl status rstudio-server

Configure Firewall

sudo ufw allow 8787/tcp
sudo ufw reload

Access RStudio Server

Open your browser and navigate to:

http://SERVER_IP:8787

Log in with your Linux credentials.

Advanced Configuration

Set a Default CRAN Mirror

Create or edit the .Rprofile file:

nano ~/.Rprofile

Add:

options(repos = c(CRAN = "https://cloud.r-project.org"))

Install System Packages for R

Some R packages require system libraries:

# For packages with common dependencies
sudo apt install -y libcurl4-openssl-dev libssl-dev libxml2-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev

Increase Available Memory

For heavy computations, you can increase the memory limit in R:

# Check available memory
memory.limit()

# On Linux, the limit is typically the available RAM

R Scripts from Command Line

Run an R Script

Rscript my_script.R

Example Script

Create a file analysis.R:

#!/usr/bin/env Rscript

# Load data
data <- read.csv("data.csv")

# Descriptive statistics
summary(data)

# Save results
write.csv(summary(data), "results.csv")

print("Analysis complete!")

Run it:

Rscript analysis.R

Data Science

PackageDescription
tidyverseCollection of data science packages
dplyrData manipulation
ggplot2Visualization
readrData import
tidyrData cleaning

Machine Learning

PackageDescription
caretUnified ML framework
randomForestRandom forests
xgboostGradient boosting
kerasDeep learning
mlr3Modern ML framework

Statistics

PackageDescription
statsBasic statistics (included)
lme4Mixed models
survivalSurvival analysis
forecastTime series

Troubleshooting

Error When Installing a Package

If you get compilation errors:

# Install development tools
sudo apt install -y build-essential

# Install common dependencies
sudo apt install -y libcurl4-openssl-dev libssl-dev libxml2-dev

Package Not Found

Check that you're using the correct package name on CRAN.

Insufficient Memory

If R runs out of memory:

# Clean environment
rm(list = ls())

# Force garbage collection
gc()

Check Installed Packages

# List all packages
installed.packages()

# Check if a package is installed
"ggplot2" %in% rownames(installed.packages())

Updating R

To update R to the latest version:

sudo apt update
sudo apt upgrade r-base r-base-dev
Warning

After a major R update, you may need to reinstall your packages.