Install R in Ubuntu: How to Set up R Development Environment

Install R in Ubuntu: How to Set up R Development Environment

APT installs r-base, giving a saved script such as mean.R the runtime it needs. I’m eager to see how the package source changes which R version APT installs.

Your first choice is between Ubuntu’s release package and CRAN’s signed repository. See which R version APT would install from each source for your Ubuntu release.

Ubuntu’s archive and CRAN offer different R versions

R is a programming language and environment for statistical computing and graphics, and Ubuntu packages it as r-base.

On Ubuntu 24.04 Noble arm64, the two sources offered different candidates for r-base. I ran apt policy r-base with Ubuntu’s default sources and saw 4.3.3-2build2 from noble/universe.

SourceCandidate on Noble arm64Choose it when
Ubuntu archive4.3.3-2build2 from noble/universeYou want the R package maintained with Ubuntu’s release.
CRAN4.6.1-6.2404.0 from noble-cran40Your project needs CRAN’s current R 4.6 series.

The Ubuntu candidate comes from its release archive. The screenshot shows the version APT selected before CRAN was added.

APT policy shows r-base candidate 4.3.3-2build2 from noble/universe on arm64.
On Noble arm64, the Ubuntu archive candidate is 4.3.3-2build2 from noble/universe.

After I loaded CRAN’s signed noble-cran40 source, the same command selected 4.6.1-6.2404.0 and kept Ubuntu’s 4.3.3-2build2 as an alternative.

APT policy shows CRAN's 4.6.1-6.2404.0 candidate and Ubuntu's 4.3.3-2build2 archive version on Noble arm64.
APT selects CRAN’s current candidate after you add the noble-cran40 source. Noble’s Ubuntu archive candidate remains available below.

The Candidate row names what APT would install, and the indented line names its repository. The Installed row tells you whether R is already present.

Check the Ubuntu codename and architecture

CRAN builds packages for specific Ubuntu suites and CPU architectures. Check both before you add its repository, because a valid key cannot make an unsupported suite appear.

lsb_release -cs && dpkg --print-architecture

The first command prints the Ubuntu suite, and the second prints the package architecture.

Terminal output from lsb_release -cs and dpkg --print-architecture showing noble and arm64.
The first output is the Ubuntu suite. The second is the Debian package architecture.

CRAN’s current list includes Jammy 22.04 and Resolute 26.04 alongside Noble. Each listed suite has amd64 and arm64 packages.

On an Ubuntu derivative, use its base Ubuntu codename rather than the derivative’s own codename. Our guide to checking the Ubuntu version helps identify the release.

Install R from Ubuntu or CRAN

The Ubuntu archive needs no extra package source. Add CRAN only when you need its candidate, then inspect the repository APT selected before installing.

Step 1. Install from Ubuntu’s archive

This route installs the R package maintained for your Ubuntu release.

sudo apt update
sudo apt install --no-install-recommends r-base
R --version

On Noble arm64, the archive candidate is 4.3.3-2build2. A different Ubuntu release can offer another version.

Step 2. Add CRAN’s signing key

APT verifies the repository’s signed Release metadata. Download CRAN’s public key and store it in a dedicated keyring.

Compare its fingerprint with CRAN’s published value before adding the source. Our curl download guide shows the key-download syntax.

sudo apt update
sudo apt install --no-install-recommends ca-certificates curl gnupg
sudo install -d -m 0755 /etc/apt/keyrings
curl -fsSL https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/cran-r-project.gpg >/dev/null
sudo chmod 0644 /etc/apt/keyrings/cran-r-project.gpg
gpg --show-keys --with-fingerprint /etc/apt/keyrings/cran-r-project.gpg

CRAN’s published fingerprint is E298 A3A8 25C0 D65D FD57 CBB6 5171 6619 E084 DAB9, so stop if your output differs.

Step 3. Add the matching CRAN suite

The source line uses the codename you just identified and limits this repository to its own key. On a derivative, use the underlying Ubuntu codename.

. /etc/os-release
suite="${UBUNTU_CODENAME:-$VERSION_CODENAME}"
printf 'deb [signed-by=/etc/apt/keyrings/cran-r-project.gpg] https://cloud.r-project.org/bin/linux/ubuntu %s-cran40/\n' "$suite" | sudo tee /etc/apt/sources.list.d/cran-r-project.list
sudo apt update
apt policy r-base
sudo apt install --no-install-recommends r-base

The Signed-By option keeps CRAN’s key scoped to this repository, and APT checks the Release signature during update.

The cran40 suffix marks binary compatibility after R 4.0. CRAN’s current Noble candidate is in the R 4.6 series.

Run an R script with Rscript

Rscript runs a saved R file and returns its output to the shell. The Rscript manual documents the file argument and the version option.

Save this example as mean.R. It computes the mean of three values and prints the result.

values <- c(2, 4, 6)
print(mean(values))
Rscript mean.R

I ran the saved file with Rscript 4.6.1, and it printed [1] 4.

Rscript runs mean.R and prints [1] 4 in the terminal.
Rscript mean.R prints [1] 4 after evaluating the saved file.

The [1] marks the first printed value, and 4 is the calculated mean. Rscript exits when the file finishes, so you can call it from a shell script or a scheduled job.

Add R packages and choose an optional IDE

APT and R install packages into different libraries. Pick one manager for each package so an older system copy does not shadow the version in your user library.

What you needUseWhere it comes from
R and the packages bundled with the releaser-base through APTUbuntu archive or CRAN APT source
Build tools for R packages compiled from sourcer-base-dev through APTCRAN’s package instructions recommend it for source builds
An R package in your user libraryinstall.packages() from RCRAN repository selected in the call
A desktop code editor and R consoleRStudio DesktopOptional Posit IDE, installed separately

CRAN recommends r-base-dev when compiling packages from source. Some packages also need separate Ubuntu development libraries.

sudo apt install r-base-dev
Rscript -e 'install.packages("jsonlite", repos = "https://cloud.r-project.org")'

The source install placed jsonlite in the R user library, and I loaded version 2.0.0 under R 4.6.1.

Posit’s current downloads page lists an amd64 Desktop DEB, with no arm64 build in its public table. Use Posit’s RStudio guide to choose a supported package, then our guide to installing a local DEB package for APT’s file syntax.

Use the installed APT package list to inspect system packages. R packages in your user library remain separate, and our APT removal guide covers removing the system package.

Fix APT key and suite errors

APT reports whether the source key or suite prevented it from loading a package index. Keep signature checks enabled and repair the source APT rejected.

Message or resultWhat it meansNext action
NO_PUBKEY 51716619E084DAB9APT cannot verify CRAN’s Release signature with the configured key.Compare the downloaded key fingerprint and check the Signed-By path in cran-r-project.list.
The repository does not have a Release fileThe suite is absent from CRAN’s supported Ubuntu list or the source uses a derivative codename.Check the Ubuntu base codename and choose a supported CRAN suite.
Ubuntu remains the CandidateAPT did not load the CRAN package index or the suite did not match.Read apt update errors, then inspect apt policy r-base again.
A package build reports missing tools or headersThe source build needs r-base-dev or an operating-system development library.Install the named APT build dependency and retry the R package installation.

The Candidate line identifies the R source

APT can list several r-base versions, but the Candidate line is the version it will install from the configured sources. Check its repository after adding or removing a source.

apt policy r-base

R installation questions

CRAN’s suite label and the optional IDE are separate from the R runtime itself.

Does cran40 install R 4.0?

No. cran40 marks the binary compatibility break after R 4.0. CRAN’s current packages come from its R 4.6 series.

Can I run R without RStudio?

Yes. R includes Rscript for saved files, and RStudio Desktop is a separate IDE.

Why does apt show NO_PUBKEY?

APT cannot verify CRAN’s Release signature with the configured key. Compare the downloaded key’s fingerprint with CRAN’s value. Keep signature checks enabled and verify that Signed-By points to that key.

Which Ubuntu releases does CRAN support?

CRAN lists Jammy 22.04 and Noble 24.04. It also lists Resolute 26.04 with amd64 and arm64 packages. On derivatives, use the underlying Ubuntu codename.