Many Java stacks still target 21 even after Ubuntu 26.04 moved its default JDK to OpenJDK 25. If you need to install OpenJDK 21 on Ubuntu for Spring Boot 3.x, build servers, or pinned enterprise apps, Ubuntu’s own archives still carry the package. Eclipse Temurin remains an alternate vendor build on 24.04 and 22.04.
Ubuntu 24.04 makes OpenJDK 21 the default Java line, Ubuntu 22.04 keeps it in Universe, and Ubuntu 26.04 currently ships OpenJDK 21 as a compatibility package alongside OpenJDK 25. Both package paths use APT for installation and updates, and Ubuntu handles Java switching through update-alternatives, so you avoid manual tarball downloads and hand-set symlinks unless you intentionally want a different archive-based workflow.
Install OpenJDK 21 on Ubuntu
Refresh APT first so Ubuntu pulls the current package metadata and security updates. The -y flag accepts the upgrade prompt automatically.
sudo apt update
sudo apt upgrade -y
These commands use
sudofor package-management tasks that need root privileges. If your account is not in the sudoers group yet, follow the steps to add a new user to sudoers on Ubuntu before continuing.
Two package sources matter here. Ubuntu’s own archive is the simplest path, while Eclipse Temurin gives Ubuntu 24.04 and 22.04 users an Adoptium build that still updates through APT.
| Method | Source | Works On | Updates | Best Fit |
|---|---|---|---|---|
| Ubuntu archive | Ubuntu packages | 26.04, 24.04, 22.04 | Via apt upgrade | Fastest setup, distro-managed Java packages |
| Eclipse Temurin APT | Adoptium Linux packages | 24.04, 22.04 | Via apt upgrade | Adoptium vendor build with TCK and AQAvit checks |
Most readers should start with the Ubuntu archive method. Choose Eclipse Temurin only when you specifically want Adoptium’s vendor build on Ubuntu 24.04 or 22.04.
Ubuntu 26.04 can install
openjdk-21-jdkfrom Universe, but the current package reports a21.0.11-earuntime whiledefault-jdkalready points to OpenJDK 25. If your policy requires a non-EA Java 21 build, run the Temurin workflow on Ubuntu 24.04 or 22.04 for now, because Adoptium does not publish aresoluteRelease file yet.
Install OpenJDK 21 from Ubuntu Repositories
Install OpenJDK 21 on Ubuntu with sudo apt install openjdk-21-jdk -y when you want the distro-managed package and normal APT updates. Ubuntu 24.04 publishes openjdk-21-jdk from Main, while Ubuntu 22.04 and 26.04 publish it from Universe.
| Package | Includes | Best For |
|---|---|---|
| openjdk-21-jdk | Full compiler, runtime, and desktop dependencies | Desktop development and general Java work |
| openjdk-21-jdk-headless | Compiler and runtime without GUI libraries | Servers, CI runners, and containers |
| openjdk-21-jre | Desktop runtime only | Running Java apps without compiling them |
| openjdk-21-jre-headless | Headless runtime only | Server-side Java application runtime |
Choose one package variant rather than installing all four. Most readers either want the full JDK or the headless JDK.
For a normal development workstation, install the full JDK:
sudo apt install openjdk-21-jdk -y
For servers, CI runners, or runtime-only hosts, choose the one variant that matches the job:
sudo apt install openjdk-21-jdk-headless -y
sudo apt install openjdk-21-jre -y
sudo apt install openjdk-21-jre-headless -y
Verify the active runtime with java --version. Ubuntu 26.04 currently reports an EA build string here, while Ubuntu 24.04 and 22.04 report stable 21.0.10 builds.
java --version
openjdk 21.0.11-ea 2026-04-21 OpenJDK Runtime Environment (build 21.0.11-ea+8-Ubuntu-1) OpenJDK 64-Bit Server VM (build 21.0.11-ea+8-Ubuntu-1, mixed mode, sharing)
If you installed a JDK package rather than a JRE-only package, confirm the compiler is present too:
javac --version
javac 21.0.11-ea
On amd64, the active Java binary resolves to /usr/lib/jvm/java-21-openjdk-amd64/bin/java. Ubuntu 24.04 packages OpenJDK 21 from noble-updates/main, Ubuntu 22.04 packages it from jammy-updates/universe, and Ubuntu 26.04 packages it from resolute/universe with the EA-labeled 21.0.11 build shown above.
Install Eclipse Temurin 21 on Ubuntu
Install Eclipse Temurin 21 on Ubuntu when you want Adoptium’s TCK-tested and AQAvit-verified build rather than Ubuntu’s archive packages.
This Adoptium repo method currently works on Ubuntu 24.04 (noble) and 22.04 (jammy). Ubuntu 26.04 (resolute) is not ready for this repo yet because
https://packages.adoptium.net/artifactory/deb/dists/resolute/Releasereturns404 Not Found.
Install the local tools used to fetch the Adoptium key and create the source file:
sudo apt install curl gpg ca-certificates -y
Create the Adoptium source file in DEB822 format next. The release guard stops Ubuntu 26.04 before a broken resolute source file or keyring is written.
codename=$(. /etc/os-release && printf '%s' "$VERSION_CODENAME")
version_id=$(. /etc/os-release && printf '%s' "$VERSION_ID")
case "$codename" in
noble|jammy)
curl -fsSL https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo gpg --dearmor --yes -o /usr/share/keyrings/adoptium.gpg
printf '%s\n' \
'Types: deb' \
'URIs: https://packages.adoptium.net/artifactory/deb' \
"Suites: $codename" \
'Components: main' \
"Architectures: $(dpkg --print-architecture)" \
'Signed-By: /usr/share/keyrings/adoptium.gpg' | sudo tee /etc/apt/sources.list.d/adoptium.sources > /dev/null
;;
*)
printf 'Adoptium does not publish a Temurin 21 APT repo for Ubuntu %s yet. Use the Ubuntu archive method on this release.\n' "$version_id"
false
;;
esac
The supported branch uses the curl command in Linux to fetch the signing key, writes noble on Ubuntu 24.04 and jammy on Ubuntu 22.04, and keeps the same file correct on amd64 and arm64 systems without hardcoding one package arch.
Refresh APT after adding the repo so Ubuntu can read the new package metadata:
sudo apt update
Install the Temurin 21 JDK package once the repo metadata is available:
sudo apt install temurin-21-jdk -y
If you only need the runtime, Adoptium also publishes temurin-21-jre.
Verify the Temurin runtime after installation:
java --version
openjdk 21.0.11 2026-04-21 LTS OpenJDK Runtime Environment Temurin-21.0.11+10 (build 21.0.11+10-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.11+10 (build 21.0.11+10-LTS, mixed mode, sharing)
APT should also show the package coming from Adoptium’s repo:
apt-cache policy temurin-21-jdk
temurin-21-jdk:
Installed: 21.0.11.0.0+10-0
Candidate: 21.0.11.0.0+10-0
Version table:
*** 21.0.11.0.0+10-0 500
500 https://packages.adoptium.net/artifactory/deb noble/main amd64 Packages
100 /var/lib/dpkg/status
Update Temurin 21 on Ubuntu
Temurin 21 updates through the same Adoptium repo, so a normal APT refresh and targeted package upgrade keeps it current.
sudo apt update
sudo apt install --only-upgrade temurin-21-jdk -y
If you installed the runtime-only package instead, swap in temurin-21-jre for the upgrade command.
Compare Ubuntu Default Java Versions for OpenJDK 21
Ubuntu’s default Java line changes by release, which is why OpenJDK 21 feels like the default choice on one LTS and a compatibility install on another.
| Ubuntu Release | default-jdk Maps To | OpenJDK 21 Package Status | What It Means |
|---|---|---|---|
| Ubuntu 26.04 | OpenJDK 25.x | openjdk-21-jdk is available in Universe as a Java 21 compatibility package | Use OpenJDK 21 mainly for compatibility testing or pinned application requirements |
| Ubuntu 24.04 | OpenJDK 21.x | openjdk-21-jdk is available in Main and matches the default Java 21 line | Simplest Ubuntu-packaged Java 21 install |
| Ubuntu 22.04 | OpenJDK 11.x | openjdk-21-jdk is available in jammy-updates/universe | Useful when an older LTS host still needs Java 21 for application compatibility |
If your workload still follows Ubuntu 22.04’s older default Java line, compare OpenJDK 11 on Ubuntu. If a framework vendor has only certified Java 17 so far, compare OpenJDK 17 on Ubuntu. If Ubuntu 26.04’s default Java 25 line already matches your framework support, compare OpenJDK 25 on Ubuntu before pinning Java 21 just out of habit.
Manage OpenJDK 21 and Other Java Versions on Ubuntu
Ubuntu uses update-alternatives to switch between installed Java runtimes and compilers. That keeps /usr/bin/java and /usr/bin/javac pointed at the version you actually want to use.
Switch the Default Java Runtime on Ubuntu
Run the interactive selector when multiple Java runtimes are installed and you want to switch the system default.
sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
Choose the number you want, then rerun java --version to confirm the change.
Switch the Default Java Compiler on Ubuntu
Keep the compiler aligned with the runtime so your builds and bytecode targets stay on the same Java version.
sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 2111 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/javac 1711 manual mode 2 /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 2111 manual mode
Use the same major version for java and javac unless you have a very specific cross-compilation reason not to.
Find the Active JAVA_HOME Path on Ubuntu
Check the active Java path first, then use the parent directory as JAVA_HOME.
readlink -f /usr/bin/java
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
Remove the trailing /bin/java to get JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64. If Temurin is the active runtime instead, the same command returns /usr/lib/jvm/temurin-21-jdk-amd64/bin/java. For a full per-user or system-wide walkthrough, follow the guide on setting the Java environment path in Ubuntu.
Troubleshoot OpenJDK 21 on Ubuntu
The most common OpenJDK 21 issues on Ubuntu are missing Universe packages, the wrong Java version staying active, or Temurin repo support not matching the Ubuntu release you are on.
Fix OpenJDK 21 Package Not Found on Ubuntu 22.04 or 26.04
If apt install openjdk-21-jdk fails with Unable to locate package, the Universe repository is usually disabled on a minimal or customized system. Check for active Universe entries with the grep command in Linux first:
grep -R "universe" /etc/apt/sources.list /etc/apt/sources.list.d/ 2>/dev/null
/etc/apt/sources.list.d/ubuntu.sources:Components: main restricted universe multiverse
Older Ubuntu 22.04 systems may show legacy deb lines with jammy universe instead. The important signal is that universe appears in an active source. If it is missing, enable Universe and refresh APT:
sudo apt install software-properties-common -y
sudo add-apt-repository universe -y
sudo apt update
Once Universe is enabled, retry the package install. Only Universe is required for OpenJDK 21, but if you want the broader background on Ubuntu repository components, use the guide to enable Universe and Multiverse in Ubuntu.
Fix Eclipse Temurin 21 Package Not Found on Ubuntu 26.04
Temurin 21 is not a working APT option on Ubuntu 26.04 yet because Adoptium does not publish https://packages.adoptium.net/artifactory/deb/dists/resolute/Release. Use the Ubuntu archive method on 26.04, or move the Temurin 21 workflow to Ubuntu 24.04 or 22.04 until Adoptium adds resolute support. The guarded source command prints this boundary and stops before writing a broken source file.
Fix the Wrong Java Version on Ubuntu
If Java 17, 25, or another version still answers after you install 21, check both the runtime and the compiler before changing alternatives.
java --version
javac --version
openjdk 21.0.10 2026-01-20 OpenJDK Runtime Environment (build 21.0.10+7-Ubuntu-124.04) OpenJDK 64-Bit Server VM (build 21.0.10+7-Ubuntu-124.04, mixed mode, sharing) javac 21.0.10
If those commands disagree, rerun update-alternatives --config java and update-alternatives --config javac, then choose the same JDK path for both.
Remove OpenJDK 21 from Ubuntu
Choose the removal steps that match the package source you used. Ubuntu archive packages and Temurin packages should be cleaned up separately.
Remove Ubuntu Archive OpenJDK 21
Remove the Ubuntu-packaged OpenJDK 21 variants first. If you installed only one JRE or headless package, remove just that package name instead of every variant.
sudo apt remove openjdk-21-jdk openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless -y
Preview orphaned dependencies before removing them. Continue only if the preview lists Java packages you no longer need.
sudo apt autoremove --dry-run
Run the cleanup only after that preview looks safe:
sudo apt autoremove
Remove Eclipse Temurin 21
Remove the Temurin package first. If you installed the runtime-only package, swap in temurin-21-jre.
sudo apt remove temurin-21-jdk -y
Preview unused dependencies next. Packages such as adoptium-ca-certificates, java-common, and fonts can be shared with another Java package, so remove them only when the preview matches what you intend to clean up.
sudo apt autoremove --dry-run
Run the cleanup only after that preview looks safe:
sudo apt autoremove
If you no longer need any Adoptium packages, remove the repo file and keyring too, then refresh APT.
sudo rm -f /etc/apt/sources.list.d/adoptium.sources
sudo rm -f /usr/share/keyrings/adoptium.gpg
sudo apt update
Verify OpenJDK 21 Removal
Check the installed package state first. No output means none of the listed OpenJDK 21 or Temurin 21 packages remains installed.
dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package}\n' openjdk-21-jdk openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless temurin-21-jdk temurin-21-jre 2>/dev/null | grep '^ii' || true
If you also removed the Adoptium source, confirm APT no longer lists packages.adoptium.net as a live source for Temurin packages:
apt-cache policy temurin-21-jdk | grep packages.adoptium.net || true
If you exported
JAVA_HOMEmanually in~/.bashrc,~/.profile, or a service unit, remove or update that path after uninstalling OpenJDK 21. Build tools fail quickly whenJAVA_HOMEstill points to a deleted JDK directory.
Next Steps After Installing OpenJDK 21 on Ubuntu
OpenJDK 21 on Ubuntu is ready for application runtimes, compilers, and version switching through update-alternatives. If your build also needs dependency management, install Apache Maven on Ubuntu. If Ubuntu 26.04’s default Java line already suits your stack, compare OpenJDK 25 on Ubuntu next.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>