How to Install Java on Ubuntu 24.04 LTS: A Step-by-Step Guide

Learn to install Java on Ubuntu 24.04 LTS (Noble Numbat) quickly with our easy-to-follow step-by-step guide.

If you’re looking to get Java up and running on your Ubuntu 24.04 LTS system, you’re in the right place. We have carefully designed this article to guide you with ease through the installation process, explaining each step in detail.

Of course, we have personally tested every step to guarantee that you will have all the necessary tools to develop applications or run software that relies on Java’s extensive framework.

But before installing Java on your Ubuntu 24.04 system, let’s take a moment to explain the different editions and versions of Java so that we can get more clarity on the implementation itself.

Differences Between OpenJDK and OpenJRE

OpenJDK and OpenJRE are related but, at the same time, distinct software components used in the Java development and execution environment. Here are the two key differences between them:

  1. OpenJDK is an open-source Java SE (Standard Edition) platform implementation. It includes a Java Development Kit (JDK), which provides tools for developing and compiling Java applications, and a Java Runtime Environment (JRE), which is necessary for running Java applications on a userโ€™s computer.
  2. OpenJRE is a runtime environment for Java applications. It includes the Java Virtual Machine (JVM), the engine that runs Java code, and the Java class libraries, which provide the core functionality of the Java platform. OpenJRE does not include the development tools and compilers that are part of the JDK.

In summary, OpenJDK is a complete Java development and runtime environment, while OpenJRE is a runtime environment only. Therefore, the JDK component of OpenJDK includes the JRE component but not vice versa.

That means you must have OpenJDK installed if you are developing Java applications while having OpenJRE is sufficient if you want to run Java applications.

Oracleโ€™s JDK or OpenJDK: Which to Choose?

You can get Java from two main sources โ€“ install the Oracle implementation or rely on the OpenJDK available in the Ubuntu repositories. The main and recommended one, which we will use in this guide, is to install it from the official Ubuntu repos, giving you a system with OpenJDK installed.

Yes, you can get Java by installing its Oracle implementation but that will result in some limitations and unnecessary complications. For example, this requires manual interventions, such as downloading and installing the DEB package. More importantly, you won’t get automatic security updates for your Java implementation as part of system updates on your Ubuntu 24.04 system.

Now, if you’re wondering if Oracle’s Java has any advantages over OpenJDK, the short answer is no, it doesn’t. After all, OpenJDK has the same code as Oracle’s JDK, with the most significant difference being the licensing model.

Which Java Version Should You Install on Your Ubuntu 24.04 System?

Ubuntu 24.04 LTS includes different OpenJDK releases in its repositories. So, if you search for available Java packages, you will find a long list of choices for versions 8, 11, 17, 19, 20, 21, and 22. For new users, this may be a bit confusing. Which version should I install? Don’t worry; let us help you make the right choice.

Like some Linux distributions, the Linux kernel, or other software, Java provides LTS (Long Term Support) versions. These are the ones with a 5-year support period. In comparison, the non-LTS versions get only six months of support. Therefore, the best choice is to pick an LTS release.

Ubuntu 24.04 offers four Java LTS releases: 8, 11, 17, and 21. According to the table below, Java 8 and 11 have already reached the end of their active support, leaving us with two LTS versions to choose between โ€“ 17 and 21.

Java VersionActive SupportExtended Support
8 (LTS)March 31, 2022December 21, 2030
11 (LTS)September 30, 2023September 30, 2026
17 (LTS)September 30, 2026September 30, 2029
19March 21, 2023March 21, 2023
20September 19, 2023Unavailable
21 (LTS)September 30, 2028September 30, 2031
22September 17, 2024Unavailable

While Java 17 LTS is currently the more widely adopted, looked at in the long term, Java 21 LTS is expected to become the primary version that developers will depend on in the years ahead, making it our logical choice to rely on in this guide.

Install Java on Ubuntu 24.04 LTS (Noble Numbat)

Now that you’re clear on the various Java implementations and versions let’s proceed with the installation.

Step 1: Check If Java Is Already Installed

First, ensure Java is not already installed on your Ubuntu 24.04 system. Type the following:

java -versionCode language: Bash (bash)

If it is not installed, you need to get a result similar to the one shown below so we can move forward.

Check if Java is already installed.
Check if Java is already installed.

Step 2: Install OpenJDK on Ubuntu 24.04 LTS

Ubuntu 24.04 includes the “default-jdk” meta-package, which automatically installs Java 21 LTS, which is our goal. So, let’s do that by running the following APT command:

sudo apt install default-jdkCode language: JavaScript (javascript)
Install Java on Ubuntu 24.04 LTS.
Install Java on Ubuntu 24.04 LTS.

Step 3: Verify the Installation

With the command provided below, verify that the installation was successful. It should output similar to the following:

java -versionCode language: Bash (bash)
Verify the Java installation.
Verify the Java installation.

As you can see from the image above, we now have OpenJDK 21 successfully installed on our Ubuntu 24.04 system.

Set Default Java Version

Java enables you to install multiple versions on your Ubuntu system simultaneously. This naturally leads to the question, “Which one is used by default then?” Fortunately, you can easily manage this with just a single command.

First, let’s list the available installed versions of Java on our system:

sudo dpkg -l | grep 'jdk\|jre'Code language: Bash (bash)
List the currently installed Java versions.
List the currently installed Java versions.

As can be seen, we have installed a mix of Java 17 and 21 versions. Let’s check which is the default one used:

java -versionCode language: Bash (bash)
Verify the Java installation.
Verify the Java installation.

Okay, Java 21 is currently in use. However, if we need to switch to Java 17 as the default, we can do so quickly and easily using the following command:

sudo update-alternatives --config javaCode language: Bash (bash)

The output will provide a list of available installed Java versions, each associated with a number in the โ€œSelectionโ€ column. To switch to a different than the one currently in use, select the appropriate number corresponding to the specific Java version, type it in, and confirm with โ€œEnter.โ€

If you run the java -version command again, you will see that the default version has already been changed from 21 to 17.

Set default Java version.
Set default Java version.

Set JAVA_HOME Environment Variable

The JAVA_HOME variable is used by various applications, such as development environments and build tools, to locate the JDK installation directory. When JAVA_HOME is set, the system knows where to look for the JDK and can use it to execute Java applications or compile Java code.

Run the now familiar command to find out Java’s path you need:

sudo update-alternatives --config javaCode language: Bash (bash)
Find the JAVA path.
Find the JAVA path.

As we can see, the path we need to set for JAVA_HOME is โ€œ/usr/lib/jvm/java-21-openjdk-amd64/bin/javaโ€œ. So, first, open the โ€œ/etc/environmentโ€ file with the terminal text editor you are using:

sudo nano /etc/environmentCode language: Bash (bash)

Then, add in it the line:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64/bin/java"Code language: Bash (bash)
Set JAVA_HOME environment variable.
Set JAVA_HOME environment variable.

Of course, remember to replace the content between the quotes with the path valid for your Java installation. Finally, save the file and exit.

Apply the changes and verify everything is correct by running the two commands below.

source /etc/environment
echo $JAVA_HOMECode language: Bash (bash)
Check the JAVA_HOME environment variable.
Check the JAVA_HOME environment variable.

Uninstall Java on Ubuntu 24.04 LTS

If you want to remove Java from your Ubuntu 24.04 system, use the command below to find out all locally installed Java packages.

sudo dpkg -l | grep 'jdk\|jre'Code language: Bash (bash)
Uninstall Java on Ubuntu 24.04 LTS.
Uninstall Java on Ubuntu 24.04 LTS.

Then, just pass their names after the sudo apt purge command. In our case, it would look like this:

sudo apt purge default-jdk default-jdk-headless default-jre default-jre-headless openjdk-21-jdk openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headlessCode language: Bash (bash)
Uninstall Java on Ubuntu 24.04 LTS.
Uninstall Java on Ubuntu 24.04 LTS.

Finally, clean your Ubuntu 24.04 system of any remaining dependencies.

sudo apt autoremove --purgeCode language: Bash (bash)

Don’t forget to remove the JAVA_HOME variable from the “/etc/environment” file by simply opening it and deleting the line containing it.

Conclusion

Installing Java on Ubuntu 24.04 LTS (Noble Numbat) is simple and can be completed in minutes. Now, you are all set to run Java applications, develop Java programs, and enjoy the many benefits of this powerful programming language.

Thanks for your time! I hope you find this guide helpful. Your feedback and comments are most welcome.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%