How to Install OpenJDK 17 on Ubuntu 22.04 or 20.04

Java remains an enduring and versatile language in programming, with OpenJDK 17 marking its latest evolution. If you’re looking to harness the capabilities of OpenJDK 17 on your system, this guide will detail how to install OpenJDK 17 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa.

OpenJDK 17: A Glimpse into its Features

  • Performance and Security: OpenJDK 17 has been optimized for enhanced performance and fortified against potential threats, ensuring a robust application development and deployment platform.
  • Developer Tools: The release brings refined tools and libraries, streamlining the process of building and debugging applications.
  • Functional Programming: OpenJDK 17 facilitates functional code writing with its bolstered support for functional interfaces and enriched lambda expressions.
  • Concurrent Programming: The release simplifies multi-threaded application development, offering improved atomic operations and synchronization handling.
  • Modular Programming: Modular application development becomes more intuitive with the Java Platform Module System (JPMS) enhancements.
  • Type Inference: OpenJDK 17’s improved type inference and generics handling allow for more concise and readable code.
  • Garbage Collection: The garbage collector in this release is more efficient, minimizing its impact on application performance.
  • Records: Enhanced features simplify the creation and management of records, with better pattern matching and record component handling.
  • Sealed Classes: OpenJDK 17 offers improved support for sealed classes, refining the definition and management of class member visibility.
  • Pattern Matching: The release introduces features that simplify pattern-matching code, enhancing support for instanceof and pattern handling.

The subsequent sections of this guide will present a comprehensive walkthrough on installing OpenJDK 17 on Ubuntu, catering to both Ubuntu 22.04 Jammy Jellyfish and Ubuntu 20.04 Focal Fossa users. Whether you’re a command-line aficionado or seek alternative methods, we have the steps to ensure a successful installation.

For users wishing to install the Oracle Java 17 (JDK) version, please see the guide on How to Install Java 17 on Ubuntu 22.04 or 20.04.

Step 1: Update Ubuntu before OpenJDK 17 Installation on Ubuntu

Before installing OpenJDK, updating Ubuntu to ensure all system packages are current with the “apt update” command is essential. This helps to prevent any potential conflicts during the installation process. To update your Ubuntu system, run the following command in your terminal:

sudo apt update
sudo apt upgrade

Step 2: Select OpenJDK Installation Method on Ubuntu

Method 1: Install OpenJDK 17 with Ubuntu Repository

The first and most recommended method for installing OpenJDK is through the default Ubuntu repository. You can search for available packages using the “grep” command. To do this, run the following command in your terminal:

apt-cache search openjdk | grep openjdk-17

Example output:

Screenshot showing the list of OpenJDK packages available for Ubuntu 22.04 or 20.04 LTS.
Display of OpenJDK packages for Ubuntu 22.04 or 20.04 LTS.

Depending on your version of Ubuntu, you may be able to install OpenJDK 17 JDE and JRE using the following command. This is only applicable if these packages are available in your distribution:

sudo apt install openjdk-17-jre
sudo apt install openjdk-17-jdk

Confirm the installation by running the following command.

java --version

Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.

Example:

sudo apt remove openjdk-17-jre openjdk-17-jdk --purge

Note that this will remove any unrequited leftover dependencies and thoroughly wipe the installation and data as much as possible from your system.

Method 2: Install OpenJDK 17 Manual Method on Ubuntu

The second method for installing OpenJDK 17 is downloading the .tar.gz archive package. While this option is often more up-to-date than the version in the Ubuntu repository, it requires more manual maintenance, as you’ll need to keep an eye on new updates and repeat the installation process.

Visit the downloads page to obtain the latest build version link. Then, use the following wget command to download the package.

Here’s an example of downloading OpenJDK 17 version 17.0.2 for x64 (however, it is recommended to obtain a new link from the downloads page rather than copying this example):

wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

Once the download process is complete, extracting the archive is next.

tar -xvf openjdk-17.*

Next, navigate to the directory.

cd <directory_name>

Run the following command to install the OpenJDK 17 files.

sudo mkdir -p /usr/local/openjdk-17
sudo mv * /usr/local/openjdk-17

The next step is to set up the environment variables by adding the following lines to your .bashrc or .bash_profile file.

export JAVA_HOME=/usr/local/openjdk-17
export PATH=$JAVA_HOME/bin:$PATH

Source the .bashrc or .bash_profile file to apply the changes.

source ~/.bashrc

To verify that the installation was successful, run the following command.

java --version
echo $JAVA_HOME

If installed correctly, you should see the following output:

Screenshot confirming the successful manual installation of OpenJDK 17 on Ubuntu 22.04 or 20.04 LTS.
Verification of OpenJDK 17’s manual installation on Ubuntu 22.04 or 20.04 LTS.

As demonstrated, the version displayed is the latest OpenJDK 17 build.

Step 3: Switching Alternative Java Versions on Ubuntu (Additional Commands)

After installing Java, you may want to check for newer versions and switch to them if necessary. To do this, use the following command to list the available alternatives:

sudo update-alternatives --config java

Example output:

Screenshot confirming the successful manual installation of OpenJDK 17 on Ubuntu 22.04 or 20.04 LTS.
Verification of OpenJDK 17’s manual installation on Ubuntu 22.04 or 20.04 LTS.

The previous output shows that Java 11 is listed with “1”. To switch to this version, type the following and press the enter key, and you should see a similar printout:

update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

Once the switch is complete, you can verify the default version using the following command.

java --version

Example output:

Screenshot of terminal output displaying OpenJDK versions during a switch from OpenJDK 17 on Ubuntu 22.04 or 20.04 LTS.
Visual representation of OpenJDK versions in the terminal during a transition from OpenJDK 17 on Ubuntu 22.04 or 20.04 LTS.

To switch to any other installed versions, repeat the same process.

Conclusion: Installing OpenJDK 17 on Ubuntu Linux

In conclusion, installing OpenJDK 17 on Ubuntu is a straightforward process that can be accomplished through a few different methods. Whether you use the default Ubuntu repository, download the .tar.gz archive package, or switch to a different version, the steps outlined in this article will help you successfully install and manage OpenJDK 17 on your Ubuntu system.