Share your love
Efficient Way to Install Oracle JDK 21 on Ubuntu and Debian
In this guide, you will learn to Install Oracle JDK 21 on Ubuntu and Debian. Oracle Java or Oracle JDK is a popular Java platform that provides useful tools and libraries for developing Java apps. At the current time, the LTS version is JDK 21 which is supported until Sep 2026. Also, the packages are available for free.
You can follow this guide to get the installation steps on Ubuntu and Debian Linux including Ubuntu 22.04 and Debian 12.
Quickly Install Oracle JDK 21 on Ubuntu and Debian
Before you start your JDK 21 installation, you must log in to our Ubuntu and Debian Linux as a non-root user with sudo privileges. You check the following initial guides:
Initial Server Setup with Ubuntu 22.04
Initial Server Setup with Debian 12
In this guide, to show you the guide steps we use Ubuntu 22.04. Now proceed to the following steps to complete this guide.
Step 1 – Download JDK 21 on Debian and Ubuntu Linux
First of all, you must visit the Official Oracle Java Website and get the latest Debian binary packages. To do this, you can use the following wget command:
sudo wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
Example Output:
Step 2 – Installing Oracle JDK 21 on Debian and Ubuntu
Once your download is completed, you can Install Oracle JDK 21 on Ubuntu and Debian by using the following command:
sudo dpkg -i jdk-21_linux-x64_bin.deb
Example Output:
Then, you can verify your installation by checking the Java version:
java --version
Example Output:
Step 3 – Check Java JDK Installation Path on Debian and Ubuntu
At this point, you can use the following command to check your JDK 21 path installation on your server:
sudo update-alternatives --config java
Example Output:
In my case, the Java path is located under the /usr/lib/jvm/jdk-21-oracle-x64/bin/java directory.
Now you can proceed to the next step to configure your environment path for Java.
Step 4 – Set up Java JDK 21 Environment Path on Debian and Linux
To set up the Java environment path, you must open the /etc/environment file with your desired text editor like Vi editor or Nano editor:
sudo vi /etc/environment
At the end of the file, add the following line, making sure to replace the path with your own, and not include the bin/ portion of the path:
JAVA_HOME="/usr/lib/jvm/jdk-21-oracle-x64"
Note: This file may be blank.
When you are done, save and close the file.
Then, run the following command to apply the changes:
source /etc/environment
Also, you can verify your Java Home Path with the command below:
echo $JAVA_HOME
Exmaple Output
/usr/lib/jvm/jdk-21-oracle-x64
Step 5 – Test Oracle JDK 21 Installation on Debian and Ubuntu
At this point, we want to create a sample Hello World project to check if JDK 21 is working correctly. To do this, create and open a file with the command below:
sudo vi HelloWorld.java
Add the following sample Java code to the file:
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
When you are done, save and close the file. Then, compile and run your Java code with the command below:
sudo java HelloWorld.java
Exmaple Output
Hello, World
That’s it you are done. Now you can start to use your Oracle Java on your Debian-based distros.
Conclusion
At this point, you have learned to Install Oracle JDK 21 on Ubuntu and Debian Linux. Also, you configured your Java environment home path and tested your installation by using a sample project.
Hope you enjoy using it. You may be interested in these articles too:
Install Plesk on Debian 12 From CLI