How to Install OpenJDK 17 on Ubuntu 20.04

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 pretty much the same.

In the following tutorial, you will learn how to install the latest OpenJDK 17 LTS on Ubuntu 20.04 LTS Focal Fossa.

Advertisement

Prerequisites

  • Recommended OS: Ubuntu 20.04 or higher
  • User account: A user account with sudo or root access.

Update Operating System

Update your Ubuntu operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@ubuntu ~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on Ubuntu.

To use the root account, use the following command with the root password to log in.

su

The tutorial will utilize the terminal interface, which can be found in the show applications menu.

Example:

How to Install OpenJDK 17 on Ubuntu 20.04

Option 1. Install OpenJDK 17 with APT

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

Example output:

How to Install OpenJDK 17 on Ubuntu 20.04

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 20.04

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-get remove openjdk-17-jre openjdk-17-jdk --purge

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

Alternative Method – Install OpenJDK with PPA

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, which in short terms, has no affiliation with the Ubuntu core team. However, it has been around for a long time and is widely used in larger communities.

Note, you do not need to remove and default OpenJDK installations. You can install this PPA directly. If any updates are available, it will simply 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.

Advertisement

Option 2. Install OpenJDK 17 Manually

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 Latest OpenJDK 17 Build

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

Example:

Linux  AArch64 users:

wget https://download.java.net/java/GA/jdk17.0.1/2a2082e5a09d4267845be086888add4f/12/GPL/openjdk-17.0.1_linux-aarch64_bin.tar.gz

Linux  x64 users (Majority of users):

wget https://download.java.net/java/GA/jdk17.0.1/2a2082e5a09d4267845be086888add4f/12/GPL/openjdk-17.0.1_linux-x64_bin.tar.gz

Once the download is complete, you need to extract the archive.

tar -xvf openjdk-17.0.1_linux-{version number}

Example:

tar -xvf openjdk-17.0.1_linux-x64_bin.tar.gz

Remember, 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.0.1 /opt/

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

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

Remember, change jdk-17.0.1 with the current directory name if you want it or have future updated versions.

To confirm the installation, run the following command.

java --version
echo $JAVA_HOME

If installed correctly, you should see the following output:

How to Install OpenJDK 17 on Ubuntu 20.04

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.

Advertisement

Test Java – Create Hello World Application

To finish off, 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 into the file:

public class hello {
  public static void main(String[] args) {
    System.out.println("G'day 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 20.04

Congratulations, everything is working correctly.

Comments and Conclusion

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

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
adplus-dvertising
0
Would love your thoughts, please comment.x
()
x