How to Install OpenJDK 18 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 install 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 latest OpenJDK 18 LTS on Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal.

Update Ubuntu

First, update your Ubuntu operating system to ensure all existing packages are up to date.

sudo apt update && sudo apt upgrade -y

Next, you have the choice of installing OpenJDK using two different methods.

Install OpenJDK 18 – Ubuntu APT Method

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 18 on Ubuntu 22.04 LTS

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

To begin the installation, use the following terminal command.

Install OpenJDK 18 – JRE:

sudo apt-get install openjdk-18-jre -y

Install OpenJDK 18 – JDK:

sudo apt-get install openjdk-18-jdk -y

Confirm the installation by running the following command.

java --version

Example output:

How to Install OpenJDK 18 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.

Example:

sudo apt-get autoremove openjdk-18-jre openjdk-18-jdk --purge -y

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

Install OpenJDK 18 – Archive Method

The second option is to install OpenJDK 18 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 18 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/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-aarch64_bin.tar.gz

Linux  x64 users:

wget https://download.java.net/java/GA/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-x64_bin.tar.gz

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

tar -xvf openjdk-18._linux-{version}

Example:

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

Configure and Install OpenJDK 18

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

sudo mv jdk-18* /opt/

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

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

Change JDK-18 with the current directory name if you want it or have future updated versions, for example. /opt/jdk-18.0.1.

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 18 on Ubuntu 22.04 LTS

As you can see, you can see the version is the latest OpenJDK 18 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("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 18 on Ubuntu 22.04 LTS

Congratulations, everything is working correctly.

Comments and Conclusion

In the tutorial, you have learned how to download and install OpenJDK 18 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.



Follow LinuxCapable.com!

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