How to Install OpenJDK 21 on Ubuntu 22.04 or 20.04

OpenJDK 21 is poised to be a game-changer in the Java development landscape, offering new features designed to improve performance, security, and ease of use. If you’re running Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, installing OpenJDK 21 is a straightforward process that can significantly enhance your Java development experience.

Key Features of OpenJDK 21

  • Structured Concurrency: Simplifies multi-threading by treating related tasks as a single unit, improving reliability.
  • Scoped Values: Allows for the sharing of immutable data across threads, offering an alternative to thread-local variables.
  • Key Encapsulation Mechanism (KEM) API: Introduces next-gen encryption techniques supporting various KEM algorithms.
  • Virtual Threads: Provides lightweight threads for more accessible high-throughput concurrent applications.
  • Generational Z Garbage Collector (ZGC): Optimizes application performance by segregating young and old objects for more efficient garbage collection.

These features make OpenJDK 21 a compelling upgrade for Java developers. Our upcoming guide will walk you through the steps to install OpenJDK 21 on Ubuntu 22.04 or 20.04, ensuring you can take full advantage of these new capabilities. Stay tuned for detailed installation instructions.

Section 1: Install OpenJDK 21 on Ubuntu 22.04 or 20.04

This section will guide you through manually installing OpenJDK 21 on Ubuntu Linux. This hands-on approach provides a high degree of customization, giving you complete control over the installation process.

Step 1: Download OpenJDK 21 Archive on Ubuntu 22.04 or 20.04

The first step in this process is to download the OpenJDK 21 archive. We’ll use the wget command for this purpose is a powerful tool for downloading files from a server using various protocols, including HTTP and FTP.

Before proceeding, ensure that wget is installed on your Ubuntu system. If it’s not, you can install it using the following command:

sudo apt install wget

Once wget is installed, we can download the OpenJDK 21 archive. Here are example commands for downloading OpenJDK 21 build 28 for Linux/AArch64 and Linux/x64.

The commands below for downloading OpenJDK 21 using the wget command are just examples. These versions will become outdated quickly, although they will still be downloadable. Make sure to visit the release page to obtain the latest download link, or at least to find out the new version number so you can modify the command accordingly.

AArch64 OpenJDK 21 Download Command for Ubuntu 22.04 or 20.04:

wget https://download.java.net/java/early_access/jdk21/28/GPL/openjdk-21-ea+xx_linux-aarch64_bin.tar.gz

x64 OpenJDK 21 Download Command for Ubuntu 22.04 or 20.04:

wget https://download.java.net/java/early_access/jdk21/28/GPL/openjdk-21-ea+xx_linux-x64_bin.tar.gz

After downloading the archive, we’ll extract it using the tar command:

tar -xvf openjdk-21-ea+28_linux-x64_bin.tar.gz

This command extracts the OpenJDK 21 archive, preparing us for the next stages of the installation process.

Step 2: Moving the Extracted OpenJDK 21 Files

Next, we’ll navigate to the directory where the archive was extracted:

cd jdk-21

Once inside the directory, we’ll move the OpenJDK 21 files to a new directory:

sudo mkdir -p /usr/local/jdk-21
sudo mv * /usr/local/jdk-21

Step 3: Setting Up Environment Variables on OpenJDK 21 and Ubuntu

With the OpenJDK 21 files in place, we need to set up the environment variables. This step is crucial as it allows your system to recognize the location of the Java installation and interact with it appropriately.

To do this, we’ll add the following lines to your .bashrc or .bash_profile file:

export JAVA_HOME=/usr/local/jdk-21
export PATH=$JAVA_HOME/bin:$PATH

To apply these changes to your environment variables, source the .bashrc or .bash_profile file with the following command:

source ~/.bashrc

To make these changes permanent across all terminal sessions, append the export commands to your .bashrc or .bash_profile file:

echo 'export JAVA_HOME=/usr/local/jdk-21' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc

These commands add the export lines to the end of your .bashrc file. After executing these commands, you’ll need to source your .bashrc or .bash_profile file again for the changes to take effect immediately. If you choose not to source it now, the changes will be applied when you start a new terminal session.

To source the .bashrc file, use:

source ~/.bashrc

Step 4: Verifying OpenJDK 21 Installation on Ubuntu

The final step is verifying that OpenJDK 21 is installed correctly and that your environment variables are set accurately. To do this, we’ll check the installed Java version and the JAVA_HOME variable:

java --version
echo $JAVA_HOME

If the installation was successful, the java --version command should display the version of OpenJDK 21 that you installed, and the echo $JAVA_HOME command should display the path to the JDK directory (/usr/local/jdk-21).

Screenshot Showing Successful Installation of OpenJDK 21 on Ubuntu 22.04 via Terminal Command
Terminal screenshot demonstrating the successful installation of OpenJDK 21 on Ubuntu 22.04.

Section 2: Verify OpenJDK 21 Installation via Test App on Ubuntu 22.04 or 20.04

After successfully installing OpenJDK 21 on your Ubuntu system, validating the setup is crucial. A practical way to do this is by creating and running a simple Java application. This process confirms the successful installation and introduces you to building, compiling, and running Java applications on Ubuntu.

Step 1: Creating Your First Java Application on Ubuntu with OpenJDK 21

The first step is to create a new Java file using a text editor. The nano text editor is often used for this purpose due to its simplicity and user-friendly interface. Use the following command to create a new file named Greeting.java:

nano Greeting.java

In this newly created file, you’ll write your first Java application. Here’s an example of a simple program that, when executed, will display a friendly greeting:

public class Greeting {
  public static void main(String[] args) {
    System.out.println("Hello, Ubuntu world!");
  }
}

Save your code by pressing CTRL+O and then close the text editor using CTRL+X.

Step 2: Compile and Run the Java Application on Ubuntu with OpenJDK 21

With your code ready, the next step is to compile it. Java uses a two-step process to execute code: it first compiles it into bytecode and then runs the bytecode. You can compile your code with the javac command:

javac Greeting.java

If your code is error-free, this command will generate a file named Greeting.class that contains the Java bytecode.

Next, to execute the bytecode and see your carefully crafted message, use the java command:

java Greeting

The output should be “Hello, Ubuntu world!” indicating the successful execution of your application and, therefore, the successful setup of your Java installation.

Step 3: Create a Second Java Test App on Ubuntu with OpenJDK 21

To further validate the functionality of your Java development environment and solidify your understanding and comfort with Java on Ubuntu, it’s a good idea to create and test a second Java application.

Create another file, Goodbye.javaand insert the following simple code:

public class Goodbye {
  public static void main(String[] args) {
    System.out.println("Goodbye, Ubuntu world!");
  }
}

Compile and run this program in the same way as the previous one:

javac Goodbye.java
java Goodbye

The output will read: “Goodbye, Ubuntu world!” further confirming the successful operation of your Java development environment.

Conclusion

In this guide, we’ve walked through installing OpenJDK 21 on Ubuntu 22.04 or 20.04, from acquiring the OpenJDK 21 archive to verifying the installation with test Java applications. This hands-on approach provides a high degree of customization, giving you complete control over the installation process. Following these steps, you should now have a fully functional OpenJDK 21 installation on your Ubuntu Linux system, ready to build and run a wide range of Java applications.