Java is a high-class object-oriented programming language that lets programmers or administrators run everywhere without recompiling. Java code is compiled and can run on any Java virtual machine regardless of the underlying computer architecture. The Oracle corporation maintains Java and has automatic garbage collection, which means that it automatically manages the memory and helps the programmer avoid doing that task manually. Learning how to install Java 21 on Ubuntu 24.04 is a straightforward process that may take up to 15 minutes. Let’s get started!
Table of Contents
Prerequisites to install Java 21 on Ubuntu 24.04
- A server running Ubuntu 24.04 or any Linux OS
- User privileges: root or non-root user with sudo privileges
1. Update the system
Before we start to install anything on the server, we need to update the system packages to the latest versions available:
sudo apt update -y && sudo apt upgrade -y
2. Install Java from the default repository
In Ubuntu 24.04, the default Java development repository is set to Java 21. So, there is no need to download the repo, adding GPG keys manually. We can install Java Development Kit 21 with the following command:
sudo apt install openjdk-21-jdk -y
Once installed, we can check the Java version with the command below:
java -version
You should receive the following output:
root@host:~# java -version openjdk version "21.0.2" 2024-01-16 OpenJDK Runtime Environment (build 21.0.2+13-Ubuntu-2) OpenJDK 64-Bit Server VM (build 21.0.2+13-Ubuntu-2, mixed mode, sharing)
3. Install Oracle Java
Oracle Java can not be installed with one command because it is not included in the default Ubuntu 24.04 repository. We need to download the package manually with the wget and install it. So, to download Oracle Java 21, execute the following command:
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
Once downloaded, you can install the package with the command below:
sudo apt install ./jdk-21_linux-x64_bin.deb
If you execute the command java -version, you will get the same output as before:
root@host:~# java -version openjdk version "21.0.2" 2024-01-16 OpenJDK Runtime Environment (build 21.0.2+13-Ubuntu-2) OpenJDK 64-Bit Server VM (build 21.0.2+13-Ubuntu-2, mixed mode, sharing)
This is because we installed Java OpenJDK 21 first, and it is set as the default version. In the next paragraph, we will explain how to switch between different versions of Java.
4. Switch between Java versions
On a system with multiple Java versions, we can change the active version very easily. To do that, execute the following command:
sudo update-alternatives --config java
You should receive the following output:
root@host:~# sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/jdk-21-oracle-x64/bin/java 352337920 auto mode * 1 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode 2 /usr/lib/jvm/jdk-21-oracle-x64/bin/java 352337920 manual mode Pressto keep the current choice[*], or type selection number:
As you can see, the selected Java is Java OpenJDK 21. If we want to choose the Java Oracle, we need to type 2 and hit Enter. After changing the Java version, you should receive the following output:
Pressto keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/lib/jvm/jdk-21-oracle-x64/bin/java to provide /usr/bin/java (java) in manual mode
After this, you can execute the java -version to check the output:
root@host:~# java -version java version "21.0.2" 2024-01-16 LTS Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58) Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)
Also, you can check with sudo update-alternatives –config java and will see the *.
root@host:~# sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/jdk-21-oracle-x64/bin/java 352337920 auto mode 1 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode * 2 /usr/lib/jvm/jdk-21-oracle-x64/bin/java 352337920 manual mode Pressto keep the current choice[*], or type selection number:
Now, you can see the * is on the second option for Java Oracle 21.
The main difference between Java Oracle and Java OpenJDK is that Java Oracle is paid and requires a license, or you can use it for non-commercial use. Java OpenJDK is open source and free to use but has fewer commercial features, unlike Java Oracle.
You successfully installed Java 21 on Ubuntu 24.04
Congratulations!
Of course, you do not have to install it yourself. Just sign up for one of our NVMe VPS plans and submit a support ticket. Our admins are available 24/7 and will help you with installing Java 21 OpenJDK or Oracle Java.
If you liked this post on how to install Java 21 on Ubuntu 24.04, please share it with your friends on social networks or leave a reply below. Thanks.