How to Install OpenJDK 17 on Ubuntu 22.04 LTS

Java is a general-purpose, class-based, object-oriented multipurpose programming language that is popular due to the design of having lesser implementation dependencies, meaning that the compiled Java code can be run on all platforms that support Java without the need for recompilation. Java is also fast, secure, and reliable, therefore. It is widely used for developing Java applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, etc.

The tutorial will look at installing the OpenJDK version instead of the default Oracle JDK. The difference between these two is licensing. OpenJDK is an entirely free, open-source Java with a GNU General Public License, and Oracle JDK requires a commercial license under the Oracle Binary Code License Agreement. Other differences are release schedules and other factors that come into play; however, performance is the same.

In the following tutorial, you will learn how to install the OpenJDK 17 LTS on Ubuntu 22.04 LTS Jammy Jellyfish using three alternative methods to suit your installation needs using the command line terminal.

Install OpenJDK 17 – APT Method with Ubuntu Repository

The first and most recommended option is to install OpenJDK using the default Ubuntu 20.04 repository. First, search to find what is available.

In your terminal, use the following command.

apt-cache search openjdk | grep openjdk-17

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

As the above output states, OpenJDK 17 JDE and JRE are available to install.

To begin the installation, use the following terminal command.

Install OpenJDK 17 – JRE

sudo apt-get install openjdk-17-jre

Install OpenJDK 17 – JDK

sudo apt-get install openjdk-17-jdk

Confirm the installation by running the following command.

java --version

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

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.


sudo apt-get 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.

Install OpenJDK 17 – APT Method with PPA (Latest Java 17)

Ubuntu 20.04 repository often has the latest up-to-date and secure versions. An alternative method is to add the ppa:openjdk-r/ppa repository, which is always up to date and may see updates deployed quicker than Ubuntu’s default archives.

The PPA is untrusted and has no affiliation with the Ubuntu core team in the short term. However, it has been around for a long time and is widely used in larger communities.

Note that you do not need to remove and default OpenJDK installations. You can install this PPA directly. If any updates are available, it will prompt you to update your version from its repository.

First, add the PPA using the following command.

sudo add-apt-repository ppa:openjdk-r/ppa -y

Next, you can install JDK or JRE.

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

Users that already have the above installed just run an update to check if any newer versions are available.

sudo apt update && sudo apt upgrade

For users that no longer wish to use this PPA, you will need to remove the OpenJDK versions off your system first.

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

Then remove the PPA with the -r flag for removal.

sudo add-apt-repository -r ppa:openjdk-r/ppa -y

Then repeat the original process if you want to re-install from the default Ubuntu repository or use the manual method below.

Install OpenJDK 17 – Manual Method with Archive

The second option is to install OpenJDK 17 by downloading the .tar.gz archive package. This is often more up-to-date than the Ubuntu version repository, but you will need to keep an eye on new updates and re-do the process, which can be a burden if you keep forgetting.

Download the Latest OpenJDK 17 Build

Visit the downloads page to get the latest build version link, then use the following wget command.


Linux  AArch64 users:

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

Linux  x64 users (Majority of users):

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 is complete, you need to extract the archive.

tar -xvf openjdk-17.*

In future versions, and the file name will change, a quick method is to type OpenJDK-17 then press your TAB key to complete the file name.

Configure and Install OpenJDK 17

Next, move the extracted archive directory into the /opt/ location.

sudo mv jdk-17.* /opt/

Now you need to set the environment variables and copy and paste the below into your terminal.

Example only: {version enter here}

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

To confirm the installation, run the following command.

java --version

If installed correctly, you should see the following output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

As you can see, you can see the version is the latest OpenJDK 17 build. Remember, you must check back and re-do the process for future updates.

Test Java – Create Hello World Application

It is always handy to test installations of these kinds to confirm everything is working correctly after being installed. The easy way is to create a small test using the famous Hello World example.

First, create the Java program file as follows

sudo nano hello.java

Next, add the following Java code to the file.

public class hello {
  public static void main(String[] args) {
    System.out.println("Ahoi hoi from LinuxCapable!");

Save the file (CTRL+O), then exit (CTRL+X).

Next, compile the code using the following terminal command javac.

javac hello.java

Finally, run the Java code with the following command:

java hello

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

Congratulations, everything is working correctly.

Switching Alternative Java Versions

Once you installed Java, you may have tried to check the version and see a newer version in the output. Use the following command first to list the available alternatives to switch to.

sudo update-alternatives --config java

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

So as above, you see the Java 11 in the listing with the number 1, type this and press enter key.

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

Once done, re-check the default version available using the following command.

java --version

Example output:

How to Install OpenJDK 17 on Ubuntu 22.04 LTS

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

Comments and Conclusion

In the tutorial, you have learned how to download and install OpenJDK 17 latest version by learning three methods, downloading and future versions and installing with the same process for your Ubuntu 22.04 LTS Desktop or Server.

Ideally, the PPA would be best for users wanting quick updates, but the choice is yours.

Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!