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:
- 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.
- 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 Version | Active Support | Extended Support |
---|---|---|
8 (LTS) | March 31, 2022 | December 21, 2030 |
11 (LTS) | September 30, 2023 | September 30, 2026 |
17 (LTS) | September 30, 2026 | September 30, 2029 |
19 | March 21, 2023 | March 21, 2023 |
20 | September 19, 2023 | Unavailable |
21 (LTS) | September 30, 2028 | September 30, 2031 |
22 | September 17, 2024 | Unavailable |
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 -version
Code 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.
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-jdk
Code language: JavaScript (javascript)
Step 3: Verify the Installation
With the command provided below, verify that the installation was successful. It should output similar to the following:
java -version
Code language: Bash (bash)
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)
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 -version
Code language: Bash (bash)
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 java
Code 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 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 java
Code language: Bash (bash)
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/environment
Code language: Bash (bash)
Then, add in it the line:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64/bin/java"
Code language: Bash (bash)
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_HOME
Code language: Bash (bash)
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)
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-headless
Code language: Bash (bash)
Finally, clean your Ubuntu 24.04 system of any remaining dependencies.
sudo apt autoremove --purge
Code 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.