How to Install Rscript on Ubuntu

Install Rscript on Ubuntu with sudo apt install r-base from universe, optionally add the CRAN apt repository on supported LTS releases, verify with Rscript --version, run example.R from the shell or cron, and uninstall with apt purge.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Install Rscript on Ubuntu banner with R language logo and terminal statistical computing motif

R is a language and environment for statistical computing and graphics. Rscript is the non-interactive front-end that runs .R files from the shell—what you want for cron jobs, CI pipelines, or calling R from PHP with exec (Stack Overflow).

On Ubuntu, Rscript is not a separate download. It installs with r-base (which depends on r-base-core) from universe. Ask Ubuntu walks through the same starting point—sudo apt install r-base; this guide adds verification, a CRAN repository path for newer R on LTS releases, a runnable script example, and uninstall steps.

Tested on: Ubuntu 25.04 (Plucky Puffin); kernel 6.14.0-37-generic; R 4.4.3; Rscript at /usr/bin/Rscript.

NOTE
Do not confuse Rscript (from r-base-core) with the littler package, which provides a different scripting helper. For batch .R files, install r-base and use Rscript.

Prerequisites

  • Ubuntu 22.04 LTS, 24.04 LTS, or newer (25.04 tested here) on amd64 (CRAN also publishes arm64 for supported LTS suites).
  • sudo for apt installs.
  • universe enabled (r-base is packaged there).

See check Ubuntu version if you are unsure which release you are on.


Quick command summary

Task Command
Install sudo apt update && sudo apt install -y r-base
Verify R --version and Rscript --version
Run a script Rscript example.R
One-liner Rscript -e 'print(1+1)'
Update sudo apt update && sudo apt upgrade r-base
Uninstall sudo apt purge -y r-base

Choose an install method

Method Best for Jump to
Ubuntu universe (r-base) Most users, servers, and cron/CI jobs that accept Ubuntu’s pinned R Method 1
CRAN apt repository LTS desktops or servers that need a newer R than universe ships Method 2

For most Ubuntu systems, sudo apt install r-base is enough. Add CRAN only on a supported LTS codename when you need a newer R build.


Refresh indexes and check the candidate:

bash
sudo apt update
apt-cache policy r-base r-base-core

On Ubuntu 25.04:

text
r-base:
 Candidate: 4.4.3-1
r-base-core:
 Candidate: 4.4.3-1

Install the metapackage (pulls in r-base-core, recommended CRAN R packages, and Rscript):

bash
sudo apt install -y r-base

Verify the interactive shell and batch runner:

bash
R --version
Rscript --version
which R Rscript
text
R version 4.4.3 (2025-02-28) -- "Trophy Case"
Rscript (R) version 4.4.3 (2025-02-28)
/usr/bin/R
/usr/bin/Rscript

Ubuntu also ships hundreds of r-cran-* packages through apt—search before installing from source:

bash
apt search '^r-cran-' | grep -i xtable
text
r-cran-xtable/plucky 1:1.8-4-2 all

Method 2: Install R from the CRAN apt repository

CRAN’s Ubuntu README lists supported suites—typically jammy-cran40 (22.04) and noble-cran40 (24.04). If you run an interim release (for example 22.10 or 25.04), $(lsb_release -cs)-cran40 often fails with no Release file (Ask Ubuntu) because CRAN does not publish for every Ubuntu codename. On those releases, stay on Method 1 or point the suite at the nearest LTS you intentionally support.

Add the CRAN repository (deb822, Ubuntu 24.04 example)

Install tooling and import the signing key into /usr/share/keyrings/:

bash
sudo apt install -y wget gpg
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | \
 sudo gpg --dearmor -o /usr/share/keyrings/cran-archive-keyring.gpg

Create the DEB822 source file—use noble-cran40/ on 24.04 or jammy-cran40/ on 22.04:

bash
sudo tee /etc/apt/sources.list.d/cran-r.sources > /dev/null <<'EOF'
Types: deb
URIs: https://cloud.r-project.org/bin/linux/ubuntu/
Suites: noble-cran40/
Components:
Signed-By: /usr/share/keyrings/cran-archive-keyring.gpg
EOF

Refresh and install:

bash
sudo apt update
sudo apt install -y r-base
R --version
Rscript --version
WARNING
Avoid the older apt-key add and one-line deb format unless you are maintaining legacy docs—the keyring + .sources layout matches current Ubuntu apt security guidance and recent Ask Ubuntu answers.

Run an example R script with Rscript

An R script is a plain-text .R file with the same expressions you would type in the interactive R shell.

Create example.R:

bash
nano example.R
r
# Example R script
x <- 42
y <- 25
z <- x + y
w <- y / x
print("z")
print(z)
print("w")
print(w)

Run it:

bash
Rscript example.R
text
[1] "z"
[1] 67
[1] "w"
[1] 0.5952381

One-liner (no file):

bash
Rscript -e 'print(1+1)'
text
[1] 2

From cron or PHP exec, use the full path so a minimal environment still finds R:

bash
/usr/bin/Rscript /path/to/example.R

Interactive R shell (optional)

Launch the REPL with uppercase R (not the littler package’s lowercase r):

bash
R
r
print("This article is about R")
quit(save = "no")

Keep R and Rscript updated

bash
sudo apt update && sudo apt upgrade -y r-base
Rscript --version

If you use the CRAN repository, apt upgrade tracks CRAN builds once the source file is configured.


Uninstall R and Rscript

bash
sudo apt purge -y r-base r-base-core
sudo apt autoremove -y

If you added CRAN:

bash
sudo rm -f /etc/apt/sources.list.d/cran-r.sources
sudo apt update

Confirm removal:

bash
Rscript --version
text
Rscript: command not found

Troubleshooting

Symptom Likely cause Fix
Rscript: command not found r-base not installed sudo apt install r-base or r-base-core
Ubuntu suggests r-cran-littler for r Confusion with littler Use uppercase R for the REPL; use Rscript for scripts
CRAN does not have a Release file Unsupported $(lsb_release -cs) (interim release) Use universe r-base, or set suite to jammy-cran40 / noble-cran40 on purpose
apt cannot find r-base universe disabled sudo add-apt-repository universe && sudo apt update
Script works in shell but not cron PATH omits /usr/bin Call /usr/bin/Rscript explicitly in the crontab
Need IDE, not just CLI RStudio is separate Install RStudio or use R in the terminal—out of scope for Rscript alone

References


Summary

Install Rscript on Ubuntu with sudo apt install r-base from universeRscript lives in r-base-core at /usr/bin/Rscript. Verify with Rscript --version, run Rscript example.R, and use the full path in cron or PHP backends. Add the CRAN apt source on 22.04/24.04 LTS when you need a newer R than Ubuntu pins; interim releases should use universe or a deliberate LTS suite name—not blind $(lsb_release -cs)-cran40.

Frequently Asked Questions

1. How do I install Rscript on Ubuntu?

Run sudo apt update && sudo apt install -y r-base. The r-base metapackage pulls in r-base-core, which installs /usr/bin/Rscript. Verify with Rscript --version.

2. Is Rscript a separate package from R on Ubuntu?

Rscript ships inside r-base-core, installed automatically with r-base. You do not need a separate Rscript package. The littler package provides a different lowercase r helper—not the same as Rscript.

3. Why does apt say Rscript is not installed after I installed r-base?

Install r-base-core explicitly with sudo apt install r-base-core, or the full r-base metapackage. Confirm with which Rscript (typically /usr/bin/Rscript). A partial or failed install can leave R without Rscript on PATH.

4. How do I install a newer R version than Ubuntu universe provides?

On supported LTS releases (22.04 jammy, 24.04 noble), add the CRAN apt repository with the correct suite name (jammy-cran40 or noble-cran40), then sudo apt install r-base. Interim Ubuntu releases are not listed by CRAN—use universe r-base or point the repo at the nearest LTS codename.

5. How do I run an R script from the command line on Ubuntu?

Save commands in a .R file and run Rscript example.R. For one-liners use Rscript -e "print(1+1)". Cron and web backends should call the full path /usr/bin/Rscript.

6. How do I uninstall R and Rscript from Ubuntu?

Run sudo apt purge -y r-base r-base-core and sudo apt autoremove -y. Remove /etc/apt/sources.list.d/cran-r.sources if you added the CRAN repository.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …