Install Java 21 (OpenJDK 21) on Ubuntu 24.04|22.04|20.04

Welcome to this guide on how to install Java 21 (OpenJDK 21) on Ubuntu 24.04|22.04|20.04. Java is a high-level object-oriented programming language and computing platform intended to let application developers write once and run everywhere. This means that a compiled java code runs on all platforms that support Java without the need for recompilation. JDK is a collection of various programming tools such as JRE(Java Runtime Environment), Java, Javac, Jar, and many others. Java 21 LTS is the latest long-term support release for the Java SE platform released on 19 September 2023.

Java 21 comes with the following amazing features:

  • Enhanced pseudo-random number generators
  • New rendering pipeline for MacOS, using the Apple Metal API as an alternative to the existing pipeline that uses the deprecated OpenGL API
  • Remove the Experimental AOT and JIT Compiler
  • Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
  • Removal of the Remote Method Invocation (RMI) Activation mechanism
  • Deprecate the Applet API for Removal
  • Porting the JDK to MacOS/AArch64 in response to Apple’s plan to transition its Macintosh computers from x64 to AArch64
  • Strong encapsulation for JDK internals
  • Context-specific deserialization filters
  • The foreign function and memory API which allows Java programs to interoperate with code and data outside of the Java runtime

With the above information, we are now set to dive into the Java 21 LTS installation. In this guide, we will cover 3 methods to get Java 21 installed on Ubuntu 24.04|22.04|20.04.

  1. OpenJDK from APT repos.
  2. OpenJDK Manually.
  3. Oracle JDK manual install.

Option 1 – Install OpenJDK 21 on Ubuntu 24.04|22.04|20.04 from APT

The easiest and quickest way is installation OpenJDK 21 on Ubuntu 24.04|22.04|20.04 from OS upstream repositories.

sudo apt update -y
sudo apt install -y openjdk-21-jdk 

For JRE run the following commands to install.

sudo apt install openjdk-21-jre

Check Java version to validate a successful installation:

$ java --version
openjdk 21.0.6 2025-01-21
OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-124.04.1)
OpenJDK 64-Bit Server VM (build 21.0.6+7-Ubuntu-124.04.1, mixed mode, sharing)

Option 2 – Install OpenJDK 21 on Ubuntu 24.04|22.04|20.04 Manually

Java OpenJDK 21 is an open-source implementation of the Java SE platform. Since the OpenJDK versions available in the default repositories are not up to date, we will have to download the Open-source JDK 21 using the wget command.

wget https://download.java.net/openjdk/jdk21/ri/openjdk-21+35_linux-x64_bin.tar.gz

Extract the downloaded tarball.

tar xvf openjdk-21+35_linux-x64_bin.tar.gz

Then move the extracted file to the /opt/ directory as shown.

sudo mv jdk-21/ /opt/jdk-21/

Set the environment variables.

echo 'export JAVA_HOME=/opt/jdk-21' | sudo tee /etc/profile.d/java21.sh
echo 'export PATH=$JAVA_HOME/bin:$PATH'|sudo tee -a /etc/profile.d/java21.sh
source /etc/profile.d/java21.sh

Verify the Java installation as below.

$ echo $JAVA_HOME
/opt/jdk-21

Check the installed version of Java.

$ java --version
openjdk 21 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

Option 3 – Manually install Oracle JDK 21 on Ubuntu 24.04|22.04|20.04

The official Oracle JDK is a development environment for building applications and components using the Java programming language. This toolkit includes tools for developing and testing programs written in the Java programming language and running on the Java platform.

We’ll download Oracle JDK 21 Debian installer

wget https://download.oracle.com/java/21/archive/jdk-21.0.5_linux-x64_bin.deb

Run the installer after it’s downloaded:

sudo apt install ./jdk-21.0.5_linux-x64_bin.deb

Additional dependencies should be installed automatically:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'jdk-21' instead of './jdk-21.0.5_linux-x64_bin.deb'
The following NEW packages will be installed:
  jdk-21
0 upgraded, 1 newly installed, 0 to remove and 108 not upgraded.
Need to get 0 B/168 MB of archives.
After this operation, 348 MB of additional disk space will be used.
Get:1 /home/cloudspinx/jdk-21.0.5_linux-x64_bin.deb jdk-21 amd64 21.0.5-ga [168 MB]
Selecting previously unselected package jdk-21.
(Reading database ... 86602 files and directories currently installed.)
Preparing to unpack .../jdk-21.0.5_linux-x64_bin.deb ...
Unpacking jdk-21 (21.0.5-ga) ...
Setting up jdk-21 (21.0.5-ga) ...

Set JAVA environment:

echo 'export JAVA_HOME=/usr/lib/jvm/jdk-21/' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' | sudo tee -a /etc/profile

Source the file and confirm Java version:

$ source /etc/profile

$ echo $JAVA_HOME
/usr/lib/jvm/jdk-21/

$ java -version
java version "21.0.5" 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)

Set Default Java Version on Ubuntu 24.04|22.04|20.04

Setting the default Java version applies in instances where you have multiple Java versions installed on your system.

First, you need to list all the available versions.

sudo update-alternatives --config java

Output:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1111      auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/jdk-21.0.5-oracle-x64/bin/java   1091      manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

From the output, select the Java version you want to set as your default version by entering the number as shown above.

Verify the set version:

$ java -version
java version "21.0.5" 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)

Test the Java Installation

We will now test the Java installation using a simple HTML file. In this guide, we will create an HTML file with the name Hello_World.java.

cat > Hello_World.java <<EOF
public class helloworld {
  public static void main(String[] args) {
    System.out.println("Hello Java World from Kenya! Java 21 is amazing!");
  }
}
EOF

Compile the code:

java Hello_World.java

Sample Output:

$ java Hello_World.java
Hello Java World from Kenya! Java 21 is amazing!

Conclusion

Congratulations! You have triumphantly installed Java 21 (OpenJDK 21) on Ubuntu 24.04|22.04|20.04. In addition to that, you have learned how to configure Java alternatives and set the JAVA_HOME Environment Variable. I hope this was helpful.

See more guides:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

One of the most effective control panels for web hosting that is used to host websites and administer web servers […]

Beekeeper Studio is an open-source completely free cross-platform SQL editor and database manager. It is available for Mac, Linux, and […]

OpenResty® is a full-featured web platform that uses our improved Nginx core to scale online applications and services. Its purpose […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.