Home  »  ArticlesGuidesHow ToProgrammingTechnologyTools   »   How to Install OpenJDK 17 on Ubuntu 22.04

How to Install OpenJDK 17 on Ubuntu 22.04

In this tutorial we will be showing how to install OpenJDK 17 on Ubuntu 22.04. The steps outlined here can be applied to prior Ubuntu releases as well as other Debian-based Linux distros.

OpenJDK is the development kit for Java. Java is a general-purpose, class-based, object-oriented multipurpose programming language. Java’s core strengths is that the compiled Java code can be run on all platforms that support Java without the need for recompilation. It is secure and reliable by design therefore it is widely used for developing Java applications in Enterprise, consumer situations.

Why OpenJDK 17

OpenJDK 17 for Java 17 LTS is the latest long-term support release supported until September 2024 while Java 19 will receive updates under these terms, until March 2023 when it will be superseded by JDK 20.

OpenJDK 17 is available both through Ubuntu’s default repository as well as a standalone download package for manual installation from the official download sites.

Prerequisites

A working Ubuntu system with a sudo user set up and configured.

Install OpenJDK 17 on Ubuntu 22.04

The easiest approach to installing OpenJDK 17 on Ubuntu is to use the Apt package manager. The first this we need to do is to update the Apt package manager and upgrade the system using the following commands:

$ sudo apt update
$ sudo apt upgrade

Next, install the package using the following command:

$ sudo apt install openjdk-17-jdk

Side Note: You can always install the default OpenJDK release for your Ubuntu release by running this command:

$ sudo apt install default-jdk

Verify the location of the newly installed java files with the update-alternatives command below. We do this in case you have other Java versions on your system:

$ update-alternatives --list java
$ update-alternatives --list java

Output
/usr/lib/jvm/java-12-openjdk-amd64/bin/java
/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Install OpenJDK 17 From Source

Alternatively, you can install the OpenJDK package manually. The latest version as of this posting is JDK 17.0.2 General-Availability Release.

Copy the URL for the Linux/x64 build from the 17.0.2 (build 17.0.2+8) section and download the files with the wget command:

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

Then, extract the downloaded file. In this example we will to extract it to the /opt directory with the following command:

$ sudo tar xf openjdk-17.0.2_linux-x64_bin.tar.gz -C /opt

Set Java 17 as the Default Version

Whether or not you have another Java version on your system you need to set the JAVA_HOME environment variable. This will tell applications on your system that depend on java where to find it.

Edit or create a jdk_home.sh script in the /etc/profile.d directory:

$ sudo nano /etc/profile.d/jdk_home.sh

Enter or edit these directives into the file with the following result:

export JAVA_HOME=/opt/jdk-17.0.2
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file.

You can confirm your OpenJDK 17 installation by running this:

$ java --version

You can also check your JAVA_HOME environment variable anytime by running:

$ echo $JAVA_HOME

The above steps should be performed for any future updates to your Java installations.

Conclusion

You have seen how simple it is to install install OpenJDK 17 on Ubuntu 22.04 where the same step can be applied to other versions of Ubuntu and Debian-based Linux distros.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.