How to Install Java on Ubuntu 24.04

By 

•

Updated on

•

7 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 comes from the Universe repository, and it is published in the noble-updates pocket rather than the original 24.04 release pocket. If the package is not found on your system, enable Universe and refresh the package index. If it is still unavailable, check your repository configuration as described in the troubleshooting section:

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.4" 2026-07-21
OpenJDK Runtime Environment (build 25.0.4+7-1-24.04-Ubuntu)
OpenJDK 64-Bit Server VM (build 25.0.4+7-1-24.04-Ubuntu, 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

In automatic mode, installing a JDK with a higher alternatives priority changes the default. In manual mode, your selected version is kept.

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.0.4.1-oracle-x64/bin/java   419464192 auto mode
  1            /usr/lib/jvm/java-21-openjdk-amd64/bin/java     2111      manual mode
  2            /usr/lib/jvm/java-25-openjdk-amd64/bin/java     2511      manual mode
  3            /usr/lib/jvm/jdk-25.0.4.1-oracle-x64/bin/java   419464192 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.

The Java compiler has a separate alternative. Select the same JDK version for javac:

Terminal
sudo update-alternatives --config javac

Verify that the runtime and compiler use the version you selected:

Terminal
java -version
javac -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.0.4.1-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. The Oracle directory name carries the full release number, so confirm it before you copy the path.

Open the /etc/environment file:

Terminal
sudo nano /etc/environment

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

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

Apply the changes to your current shell and export the variable so child processes can read it:

Terminal
source /etc/environment
export JAVA_HOME

Verify the variable is exported:

Terminal
printenv JAVA_HOME
output
/usr/lib/jvm/java-25-openjdk-amd64

This path is now available to programs started from this shell. Sign out and sign back in for other login sessions to pick up the system-wide setting.

Info
/etc/environment is system-wide. To set JAVA_HOME per user, add export JAVA_HOME="/usr/lib/jvm/java-25-openjdk-amd64" to ~/.bashrc or another shell configuration file, adjusting the path for your preferred JDK. Source that file or open a new terminal to apply it.

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

If APT still cannot find the package, check /etc/apt/sources.list.d/ubuntu.sources. For standard Ubuntu 24.04 sources, the Suites: line for the Ubuntu archive should include noble-updates, and Components: should include universe. If your system uses /etc/apt/sources.list or .list files instead, check that the noble-updates entry includes universe. After correcting the configuration, run sudo apt update and try the installation again.

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.

A build tool reports that Java 21 must be replaced with version 25 or later
The build tool may be using a different JDK from the one selected by java -version. Check Maven with mvn -v or Gradle with ./gradlew --version from your project directory. Set and export JAVA_HOME to the Java 25 JDK directory. If Java 21 is still selected on your PATH, switch the java and javac alternatives as shown above. For Gradle, also check org.gradle.java.home and gradle/gradle-daemon-jvm.properties, since these settings can select a different JVM for the build.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page