How to Install Java on Ubuntu 24.04

Java is used to run servers, desktop applications, build tools, and development frameworks. On Ubuntu 24.04, you can install the default Java version from the standard repositories, or install Java 25 when your application needs the current LTS release.
This guide covers installing OpenJDK 25, OpenJDK 21, and Oracle JDK on Ubuntu 24.04. It also explains how to switch between installed versions and set the JAVA_HOME environment variable.
Quick Reference
| Task | Command |
|---|---|
| Install OpenJDK 25 | sudo apt install openjdk-25-jdk |
| Enable Universe repository | sudo add-apt-repository universe |
| Install default JDK | sudo apt install default-jdk |
| Install OpenJDK 21 | sudo apt install openjdk-21-jdk |
| Check Java version | java -version |
| Change default Java | sudo update-alternatives --config java |
| Set JAVA_HOME | Add to /etc/environment |
| Uninstall Java 25 | sudo apt remove openjdk-25-jdk |
Before You Begin
There are several Java implementations available. OpenJDK and Oracle JDK are the two most common choices. OpenJDK is open-source and is the default option in Ubuntu. Oracle JDK is distributed by Oracle and has its own licensing terms, so review the current license before using it in production.
The Ubuntu 24.04 repositories include OpenJDK packages for both Java Runtime Environment (JRE) and Java Development Kit (JDK). The JRE includes the Java virtual machine (JVM) and libraries needed to run Java programs. The JDK includes the JRE plus development tools to build Java applications.
Ubuntu 24.04 uses Java 21 for the default-jdk package, but Java 25 is also available from the Ubuntu Universe repository. If you are not sure which Java package to install, check the application documentation first. Pick Java 25 when your application requires that version, and stick with Java 21 for compatibility with the Ubuntu 24.04 default.
Installing OpenJDK in Ubuntu
Check if Java is already installed:
java -versionIf Java is not installed, the output will tell you the command is not found. Otherwise, it shows the installed version.
Update the package index:
sudo apt updateJava 25 is the current long-term support (LTS) release. On Ubuntu 24.04, the openjdk-25-jdk package is available from the Universe repository. If the package is not found on your system, enable Universe and refresh the package index:
sudo add-apt-repository universe
sudo apt updateInstall OpenJDK 25:
sudo apt install openjdk-25-jdkdefault-jdk package on Ubuntu 24.04 installs Java 21, not Java 25. Install openjdk-25-jdk explicitly when you need Java 25.Verify the installation:
java -versionopenjdk version "25.0.2" 2026-01-20
OpenJDK Runtime Environment (build 25.0.2+10-Ubuntu-1~24.04)
OpenJDK 64-Bit Server VM (build 25.0.2+10-Ubuntu-1~24.04, mixed mode, sharing)JRE is included in the JDK package. If you need only JRE, install the openjdk-25-jre package. For a minimal Java runtime without graphical dependencies, install the openjdk-25-jre-headless package.
Installing Java 21 Instead
Java 21 is the default Java version in Ubuntu 24.04 and remains a good choice for applications that do not yet support Java 25. To install the default JDK, run:
sudo apt install default-jdkYou can also install OpenJDK 21 directly:
sudo apt install openjdk-21-jdkTo install another available version, replace the version number. For example, use openjdk-17-jdk for Java 17.
Installing Oracle Java in Ubuntu
Oracle JDK is not available in the default Ubuntu repositories. You can install it by downloading the .deb package from Oracle.
Visit the Oracle Java Downloads page and select the version you need.
In this example, we will download and install Java 25 because it is the current LTS release. Oracle also offers newer feature releases, so choose the Linux x64 Debian Package for the version you need.

If you are installing on a server, use wget
to download the file:
wget https://download.oracle.com/java/25/latest/jdk-25_linux-x64_bin.debInstall the package:
sudo apt install ./jdk-25_linux-x64_bin.debReplace the filename if you downloaded a different version.
Setting the Default Java Version
If you have multiple Java versions installed, check the current default:
java -versionChange the default version with update-alternatives:
sudo update-alternatives --config javaYou will see a list of installed Java versions:
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 2500 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
3 /usr/lib/jvm/jdk-25-oracle-x64/bin/java 2500 manual mode
Press <enter> to keep the current choice[*], or type selection number:Enter the number of the version you want as default and press Enter.
Verify the change:
java -versionSetting the JAVA_HOME Environment Variable
Some Java applications use the JAVA_HOME environment variable
to determine the JDK location.
First, find the Java installation path:
sudo update-alternatives --config javaThe paths are:
- Oracle JDK 25 is located at
/usr/lib/jvm/jdk-25-oracle-x64/bin/java - OpenJDK 25 is located at
/usr/lib/jvm/java-25-openjdk-amd64/bin/java - OpenJDK 21 is located at
/usr/lib/jvm/java-21-openjdk-amd64/bin/java - OpenJDK 17 is located at
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
JAVA_HOME/bin/java. Set JAVA_HOME to the path above, excluding the bin/java part.Open the /etc/environment file:
sudo nano /etc/environmentAdd the following line (adjust the path for your preferred version):
JAVA_HOME="/usr/lib/jvm/java-25-openjdk-amd64"Apply the changes:
source /etc/environmentVerify the variable is set:
echo $JAVA_HOME/usr/lib/jvm/java-25-openjdk-amd64/etc/environment is system-wide. To set JAVA_HOME per user, add the line to .bashrc or another shell configuration file.Troubleshooting
Unable to locate package openjdk-25-jdk
The Java 25 package is in the Ubuntu Universe repository. Enable Universe, update the package index, and try again:
sudo add-apt-repository universe
sudo apt update
sudo apt install openjdk-25-jdkJava still shows version 21 after installing Java 25
Java 21 may still be selected as the default alternative. Run sudo update-alternatives --config java, choose the Java 25 path, and then check again with java -version.
JAVA_HOME points to the wrong Java version
Check the selected Java path with sudo update-alternatives --config java. Set JAVA_HOME to the JDK directory without the /bin/java suffix, such as /usr/lib/jvm/java-25-openjdk-amd64.
Uninstalling Java
Uninstall Java like any other package:
sudo apt remove openjdk-25-jdkReplace the package name with the version you want to remove.
Conclusion
If you manage projects that target different Java releases, install multiple JDKs side by side and switch between them with update-alternatives. Build tools such as Maven and Gradle read JAVA_HOME, so set it explicitly when a project pins a specific version.
For release notes and language changes, see the official OpenJDK documentation .
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page