How to Install Java on Ubuntu 24.04

By 

Updated on

5 min read

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

TaskCommand
Install OpenJDK 25sudo apt install openjdk-25-jdk
Enable Universe repositorysudo add-apt-repository universe
Install default JDKsudo apt install default-jdk
Install OpenJDK 21sudo apt install openjdk-21-jdk
Check Java versionjava -version
Change default Javasudo update-alternatives --config java
Set JAVA_HOMEAdd to /etc/environment
Uninstall Java 25sudo 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:

Terminal
java -version

If Java is not installed, the output will tell you the command is not found. Otherwise, it shows the installed version.

Update the package index:

Terminal
sudo apt update

Java 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:

Terminal
sudo add-apt-repository universe
sudo apt update

Install OpenJDK 25:

Terminal
sudo apt install openjdk-25-jdk
Info
The default-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:

Terminal
java -version
output
openjdk 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:

Terminal
sudo apt install default-jdk

You can also install OpenJDK 21 directly:

Terminal
sudo apt install openjdk-21-jdk

To 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.

java 25

If you are installing on a server, use wget to download the file:

Terminal
wget https://download.oracle.com/java/25/latest/jdk-25_linux-x64_bin.deb

Install the package:

Terminal
sudo apt install ./jdk-25_linux-x64_bin.deb

Replace the filename if you downloaded a different version.

Setting the Default Java Version

If you have multiple Java versions installed, check the current default:

Terminal
java -version

Change the default version with update-alternatives:

Terminal
sudo update-alternatives --config java

You will see a list of installed Java versions:

output
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:

Terminal
java -version

Setting 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:

Terminal
sudo update-alternatives --config java

The 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
Info
The java binary is located at JAVA_HOME/bin/java. Set JAVA_HOME to the path above, excluding the bin/java part.

Open the /etc/environment file:

Terminal
sudo nano /etc/environment

Add the following line (adjust the path for your preferred version):

/etc/environmentsh
JAVA_HOME="/usr/lib/jvm/java-25-openjdk-amd64"

Apply the changes:

Terminal
source /etc/environment

Verify the variable is set:

Terminal
echo $JAVA_HOME
output
/usr/lib/jvm/java-25-openjdk-amd64
Info
/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:

Terminal
sudo add-apt-repository universe
sudo apt update
sudo apt install openjdk-25-jdk

Java 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:

Terminal
sudo apt remove openjdk-25-jdk

Replace 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

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