Java 21 is the latest long-term support (LTS) release of the Java platform, released in September 2023 with major features like virtual threads, pattern matching for switch, record patterns, and sequenced collections. It is the recommended version for production workloads and new development projects.
This guide covers installing Java 21 (OpenJDK) on Ubuntu 24.04 and Debian 13 using three methods – default OS repositories, the Eclipse Temurin (Adoptium) repository, and Oracle JDK 21. We also cover setting JAVA_HOME, managing multiple Java versions with update-alternatives, and verifying your installation.
Prerequisites
- Ubuntu 24.04 LTS or Debian 13 server/desktop
- A user account with sudo privileges
- Internet access to download packages
Step 1: Install Java 21 (OpenJDK) from Default Repositories
Both Ubuntu 24.04 and Debian 13 ship OpenJDK 21 in their default repositories. This is the simplest installation method and receives security updates through the OS package manager.
Update your package index first:
sudo apt update
Install the full JDK (development kit with compiler, debugger, and tools):
sudo apt install openjdk-21-jdk -y
If you only need to run Java applications without compiling, install the JRE instead:
sudo apt install openjdk-21-jre -y
Verify the installation by checking the Java version:
java -version
The output should confirm OpenJDK 21 is installed and active:
openjdk version "21.0.6" 2025-01-21
OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-124.04.1)
OpenJDK 64-Bit Server VM (build 21.0.6+7-Ubuntu-124.04.1, mixed mode, sharing)
Check the Java compiler version as well:
javac -version
Expected output confirming the JDK compiler is available:
javac 21.0.6
Step 2: Install Eclipse Temurin 21 (Adoptium) on Ubuntu / Debian
Eclipse Temurin is the community-driven OpenJDK distribution from the Adoptium project. It offers TCK-certified builds with predictable release schedules and longer support windows than distro packages. This is a solid choice for production Java workloads.
Install the prerequisite packages needed to add the Adoptium repository:
sudo apt install -y wget apt-transport-https gpg
Download and add the Adoptium GPG key:
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/adoptium.gpg > /dev/null
Add the Adoptium repository to your system. This command automatically detects your distribution codename:
echo "deb https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
Update the package index to include the new repository, then install Temurin 21 JDK:
sudo apt update
sudo apt install temurin-21-jdk -y
Verify the Temurin installation:
java -version
The output identifies this as an Eclipse Adoptium build:
openjdk version "21.0.6" 2025-01-21 LTS
OpenJDK Runtime Environment Temurin-21.0.6+7 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.6+7 (build 21.0.6+7-LTS, mixed mode, sharing)
Step 3: Install Oracle JDK 21 on Ubuntu / Debian
Oracle provides its own JDK 21 builds under the Oracle No-Fee Terms and Conditions license. Oracle JDK includes commercial features like Flight Recorder and Mission Control that are useful for performance profiling. If you’re running RHEL-family systems, check our guide on installing Java 21 on AlmaLinux, CentOS, and RHEL.
Download the Oracle JDK 21 .deb package from Oracle’s download page. Visit https://www.oracle.com/java/technologies/downloads/ and copy the link for the Linux x64 Debian package, then download it:
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
Install the downloaded package:
sudo dpkg -i jdk-21_linux-x64_bin.deb
Oracle JDK installs to /usr/lib/jvm/jdk-21-oracle-x64. Verify it:
/usr/lib/jvm/jdk-21-oracle-x64/bin/java -version
You should see the Oracle JDK version string confirming a successful install:
java version "21.0.6" 2025-01-21 LTS
Java(TM) SE Runtime Environment (build 21.0.6+8-LTS-188)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.6+8-LTS-188, mixed mode, sharing)
Step 4: Set JAVA_HOME Environment Variable
Many Java applications, build tools like Apache Maven and Gradle, and application servers require the JAVA_HOME environment variable to be set.
First, find the Java installation path. The update-alternatives command shows where Java is installed:
sudo update-alternatives --list java
This lists all installed Java paths. The path before /bin/java is your JAVA_HOME. Common paths:
- OpenJDK from repos:
/usr/lib/jvm/java-21-openjdk-amd64 - Temurin:
/usr/lib/jvm/temurin-21-jdk-amd64 - Oracle JDK:
/usr/lib/jvm/jdk-21-oracle-x64
Set JAVA_HOME permanently for all users by creating a profile script. Open the file:
sudo vi /etc/profile.d/java.sh
Add the following lines (adjust the path to match your Java installation):
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Load the new environment variables in your current session:
source /etc/profile.d/java.sh
Confirm JAVA_HOME is set correctly:
echo $JAVA_HOME
The output should show your Java installation directory:
/usr/lib/jvm/java-21-openjdk-amd64
Step 5: Manage Multiple Java Versions with update-alternatives
If you have multiple Java versions installed, use update-alternatives to switch between them. This is common when different projects require different Java versions. For a deeper look at managing Java versions, see our guide on setting the default Java version on Ubuntu and Debian.
List all available Java alternatives:
sudo update-alternatives --config java
This displays a numbered list of installed Java versions. Enter the selection number to set your preferred default:
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-21-openjdk-amd64/bin/java 2111 manual mode
2 /usr/lib/jvm/temurin-21-jdk-amd64/bin/java 2112 manual mode
Press to keep the current choice[*], or type selection number:
Do the same for the Java compiler if you have multiple JDKs:
sudo update-alternatives --config javac
After switching, verify the active version:
java -version
javac -version
Remember to update your JAVA_HOME in /etc/profile.d/java.sh if you switch to a different Java installation path.
Step 6: Quick Java Development Environment Setup
With Java 21 installed, you can set up common build tools for Java development. Install Apache Maven for project builds and dependency management:
sudo apt install maven -y
Verify Maven picks up your Java 21 installation:
mvn -version
The output shows the Maven version along with the Java home path it detected:
Apache Maven 3.8.7
Maven home: /usr/share/maven
Java version: 21.0.6, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.8.0-51-generic", arch: "amd64", family: "unix"
For Gradle, install it from the default repositories or follow the dedicated Gradle installation guide for the latest version:
sudo apt install gradle -y
gradle -version
For a full Java IDE setup, you can install Eclipse IDE on Ubuntu or use IntelliJ IDEA or VS Code with the Java extension pack.
Conclusion
Java 21 LTS is now installed and configured on your Ubuntu 24.04 or Debian 13 system. You can use OpenJDK from the default repositories for simplicity, Eclipse Temurin for a community-supported production-ready build, or Oracle JDK for commercial features. For production deployments, keep Java updated through your package manager and set up automated security updates with unattended-upgrades.