R is a highly extensible programming language and environment primarily used for statistical computing and graphics. It provides a wide array of statistical techniques and graphical tools and is highly extensible through its rich ecosystem of packages. Common use cases include analyzing scientific research data, performing statistical modeling for academic publications, processing bioinformatics datasets, building machine learning prototypes, and creating data visualizations for reports. Additionally, for database-driven analysis workflows, R integrates seamlessly with databases like MariaDB and SQLite.
This guide walks through installing the latest R version from the Comprehensive R Archive Network (CRAN) repository on Debian. You will add the CRAN repository to your system, install R base and development tools, and verify the installation with actual command output. By the end, you will have a working R environment ready to install packages from CRAN and run statistical analyses.
Choose Your R Installation Method
Debian provides R in its default repositories, but the CRAN repository offers significantly newer versions. The following table compares both options:
| Method | Version | Updates | Best For |
|---|---|---|---|
| CRAN Repository | Latest stable (4.5.x) | Frequent updates via APT | Most users who need current R features and packages |
| Default Debian Repository | Distribution default | Security patches only | Users who prioritize system stability over new features |
For most users, the CRAN method is recommended because it provides access to the latest R releases and ensures compatibility with current CRAN packages. This guide focuses on the CRAN installation method.
Update Debian System
Before installing R, first ensure all existing packages are up to date. Run the following command to refresh your package index and upgrade installed packages:
sudo apt update && sudo apt upgrade -y
Install Required Packages
First, install the required dependencies for repository configuration and GPG key management:
sudo apt install ca-certificates curl gnupg lsb-release -y
Import APT CRAN Repository
R is present in Debian’s repositories by default, but the version may be outdated. Therefore, to get the most up-to-date version, we recommend installing R from the Comprehensive R Archive Network (CRAN) repository.
Begin by downloading the CRAN signing key directly from the Ubuntu keyserver using curl:
curl -fsSL 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7' | gpg --dearmor | sudo tee /usr/share/keyrings/cran.gpg > /dev/null
This command fetches the key via HTTPS and converts it to binary format, storing it in /usr/share/keyrings/cran.gpg where APT can verify package signatures.
Next, create the CRAN repository configuration file using the DEB822 .sources format:
sudo tee /etc/apt/sources.list.d/cran.sources > /dev/null <<EOF
Types: deb
URIs: https://cloud.r-project.org/bin/linux/debian
Suites: $(lsb_release -cs)-cran40/
Signed-By: /usr/share/keyrings/cran.gpg
EOF
Debian 13 (Trixie), released in August 2025, is now the current stable release and defaults to DEB822
.sourcesformat. Debian 12 (Bookworm) and Debian 11 (Bullseye) also fully support.sources, though legacy.listfiles remain common on older installations.
Update APT Package Index
After importing the CRAN repository, refresh your APT package index to include the newly added source. This step downloads the package list from CRAN:
sudo apt update
Once the update completes, verify that the package can be installed from the CRAN repository using the apt-cache policy command:
apt-cache policy r-base
Example outcome:
r-base:
Installed: (none)
Candidate: 4.x.x-x~trixiecran.0
Version table:
4.x.x-x~trixiecran.0 500
500 https://cloud.r-project.org/bin/linux/debian trixie-cran40/ Packages
4.x.x-x 500
500 http://deb.debian.org/debian trixie/main amd64 Packages
The version numbers and release names are placeholders. Your output will show the actual R version and your Debian release codename (bullseye, bookworm, or trixie).
The CRAN repository provides significantly newer R versions compared to Debian’s default repositories:
| Debian Version | Default Repository | CRAN Repository |
|---|---|---|
| Debian 13 (Trixie) | 4.5.0 | 4.5.2+ |
| Debian 12 (Bookworm) | 4.2.2 | 4.5.2+ |
| Debian 11 (Bullseye) | 4.0.4 | 4.5.1+ |
With the source verified, you can now proceed to install R on your Debian Linux system.
Install R Base System
With the CRAN repository configured, you can now install the R programming language on your Debian Linux system. Install both the base R system and development tools with the following command:
sudo apt install r-base r-base-dev -y
The r-base-dev package provides the header files and compilation tools required to compile R packages from source. Without this package, many CRAN packages will fail to install because they require C, C++, or Fortran compilation.
After the installation completes, verify its success by checking the installed version:
R --version
Example output:
R version 4.x.x (20xx-xx-xx) -- "Codename" Copyright (C) 20xx The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu
The version number and codename displayed will reflect the current CRAN release. At the time of writing, Debian 12 and 13 receive R 4.5.2, while Debian 11 receives R 4.5.1.
Install Common Dependencies for CRAN Packages
Many CRAN packages require system libraries to compile from source. Installing these dependencies now prevents errors when installing popular packages like ggplot2, dplyr, or tidyverse later. You can install all common dependencies with a single command:
sudo apt install r-recommended libssl-dev libxml2-dev libcurl4-openssl-dev -y
This command installs the following components:
- r-recommended: A collection of commonly used statistical packages including MASS, survival, lattice, and Matrix for data analysis and modeling.
- libssl-dev: Enables HTTPS connections for packages like httr, httr2, and curl that download data from web APIs.
- libxml2-dev: Provides XML parsing capabilities for packages like xml2 and rvest (web scraping).
- libcurl4-openssl-dev: Enables URL transfers and is required by the curl package and many tidyverse components.
If you prefer a minimal installation, you can skip this step and install these dependencies later when specific packages require them. The R package installer will display error messages indicating which system libraries are missing.
Install R Packages from CRAN
This section covers how to install, update, and remove R packages from CRAN on your Debian Linux system.
Launch the R Interpreter
To launch the R interpreter, open the terminal and enter the following command:
R
This launches the R interactive console. For most users, this user-level approach is preferred because it installs packages to your home directory (~/R/) without requiring administrative privileges.
Running R with
sudo -i Relevates privileges and can pose risks if installing untrusted packages. For personal projects, useRas a regular user instead so your package library stays in your home directory. Reservesudo R -e 'install.packages(...)'for explicit system-wide installs only.
Install R Packages
Once inside the R environment, install packages using the install.packages() function. For example, to install the ggplot2 and dplyr packages, enter the following command:
install.packages(c("ggplot2", "dplyr"), repos="https://cloud.r-project.org")
Example output showing dependency installation:
Installing packages into '/usr/local/lib/R/site-library' also installing the dependencies 'colorspace', 'utf8', 'fansi', 'pillar' trying URL 'https://cloud.r-project.org/src/contrib/ggplot2_3.5.1.tar.gz' Content type 'application/x-gzip' length 3032183 bytes (2.9 MB) ================================================== downloaded 2.9 MB * installing *source* package 'ggplot2' ... ** package 'ggplot2' successfully unpacked and MD5 sums checked
Update R Packages
To update R packages installed on your system, use the update.packages() function. For example, to update all installed packages, enter the following command:
update.packages(ask = FALSE, checkBuilt = TRUE, repos="https://cloud.r-project.org")
The ask = FALSE flag updates packages without prompting for confirmation, and checkBuilt = TRUE reinstalls packages built with older R versions.
Remove R Packages
To remove an R package, use the remove.packages() function. For example, to remove the ggplot2 package, enter the following command:
remove.packages("ggplot2")
Set Up RStudio and Data Science Tools (Optional)
While the R command-line interface handles most tasks, a dedicated IDE and version control system improve productivity for larger projects. The following optional tools complement your R installation.
RStudio Desktop
RStudio provides an integrated development environment with features like code completion, debugging, and project management. Debian users should download the .deb package directly from the Posit website. Note that RStudio requires R to be installed first. Then, after downloading, install using sudo apt install ./rstudio-*.deb to automatically resolve dependencies.
R Markdown Dependencies
If you plan to generate reports using R Markdown, you will need Pandoc and TeX Live. Install these dependencies to enable PDF and HTML document generation:
sudo apt install pandoc texlive-latex-base texlive-latex-extra texlive-fonts-recommended texlive-fonts-extra
To verify R Markdown works, first install the rmarkdown package from within R:
install.packages("rmarkdown", repos="https://cloud.r-project.org")
Then create a simple test document to confirm everything works:
rmarkdown::render(text='# Test Document\n\nThis is a test.', output_format='pdf_document', output_file='test.pdf')
This command creates test.pdf in your current directory. If it completes without errors, R Markdown is configured correctly.
Git and VSCode
For version control and alternative editing, install Git and VSCode to manage R projects and collaborate with teams. We have detailed guides on how to install Git on Debian and how to install VSCode on Debian.
Troubleshooting Common Issues
You may encounter a few common hurdles when managing R on Debian. Here is how to resolve them:
GPG Key Errors (NO_PUBKEY)
If apt update returns a NO_PUBKEY error, the signing key may have expired or been removed. The error looks like this:
W: GPG error: https://cloud.r-project.org/bin/linux/debian bookworm-cran40/ InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY DC78B2DDEABC47B7
To fix this issue, re-run the GPG import commands provided in the “Import APT CRAN Repository” section. First, remove the old key:
sudo rm /usr/share/keyrings/cran.gpg
Next, re-import the key using the commands from the repository setup section. Finally, after re-importing, run sudo apt update to verify the error is resolved.
Permission Errors on Library Write
If you see permission denied errors when installing packages, you’re trying to install to a system directory without sudo. The error appears like this:
Warning in install.packages("ggplot2") :
'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel)
You have two options:
- For System-Wide Install: Launch R with
sudo -i Rto install packages system-wide in/usr/local/lib/R/site-library/. - For Personal Install: Type “yes” at the prompt to create a personal library in your home directory (
~/R/). Personal libraries work well for single-user systems and do not require administrative access.
Package Compilation Failures
If a package fails to compile with errors about missing headers or libraries, the system is missing development dependencies. Common missing libraries include:
- libssl-dev: Required for httr, curl, and openssl packages
- libxml2-dev: Required for xml2, rvest, and XML packages
- libcurl4-openssl-dev: Required for curl and RCurl packages
- libfontconfig1-dev and libharfbuzz-dev: Required for ragg, textshaping, and systemfonts packages
Read the error message carefully to identify which library is missing, then install the corresponding -dev package using APT.
CRAN Repository Not Found for Debian Release
If apt update fails with a message indicating the CRAN repository does not have a release file for your Debian version, verify your sources file contains the correct codename:
cat /etc/apt/sources.list.d/cran.sources
Confirm the Suites: line contains a valid codename like bullseye-cran40/, bookworm-cran40/, or trixie-cran40/. CRAN only provides packages for supported Debian releases.
How to Remove R Programming Language
If you no longer need R on your system, you can cleanly remove it using the following steps.
Remove R Package
To uninstall R completely, run the following command. The --purge flag removes configuration files in addition to the packages, and -y automatically confirms the removal:
sudo apt remove --purge r-base r-base-dev -y
Next, remove the orphaned dependencies. R installation pulls in numerous packages including compilers, build tools, and development libraries. After removing R, these dependencies remain installed. Preview what will be removed before proceeding:
sudo apt autoremove -s
Review the output carefully to ensure no packages you need will be removed. Then, if the list looks correct, run the actual autoremove:
sudo apt autoremove -y
This command removes packages like build-essential, g++, gfortran, and development libraries that were automatically installed as R dependencies but are no longer needed.
Remove CRAN Repository
If you do not plan to reinstall R, remove the CRAN repository configuration file:
sudo rm /etc/apt/sources.list.d/cran.sources
Also remove the GPG key:
sudo rm /usr/share/keyrings/cran.gpg
After removing the repository files, refresh your APT package list to reflect the changes:
sudo apt update
Finally, verify the successful removal of the installation:
apt-cache policy r-base
Expected output after removal:
r-base:
Installed: (none)
Candidate: 4.x.x-x
Version table:
4.x.x-x 500
500 http://deb.debian.org/debian release/main amd64 Packages
This output confirms R is no longer installed and the CRAN repository has been removed. The candidate version now shows only the default Debian repository, with version numbers varying by release.
Remove User-Installed Packages and History
Warning: The following command permanently deletes all user-level R data:
~/.R/(R settings),~/.Rhistory(command history), and~/R/(user-installed packages). If you have custom packages or scripts you want to keep, back them up before running this command.
To remove user-specific R data from your home directory, including installed packages, command history, and configuration files:
rm -rf ~/.R ~/.Rhistory ~/R
Conclusion
You now have a working R environment on Debian with the latest version from CRAN. To keep R current, run sudo apt update && sudo apt upgrade periodically. From here, you can install tidyverse for data manipulation, create reproducible reports with R Markdown, or connect to databases using packages like DBI. For larger projects, consider using RStudio or VSCode with the R extension for an improved development workflow.