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.