Install Java 18 in Ubuntu 22.04

This tutorial is going to take you through on how to Install Java 18 in Ubuntu 22.04. Java is a high-abstraction object-oriented programming language with as few implementation dependencies as possible. It’s a general-purpose programming language that allows programmers to write once and run anywhere, which implies that compiled Java code may run on any platform that supports Java without having to be recompiled. Java programs are often compiled to byte code, which may run on any Java virtual machine (JVM) regardless of computer architecture.

The following are some of the new features in Java 18;

  • 400:UTF-8 by Default
  • 408:Simple Web Server
  • 413:Code Snippets in Java API Documentation]
  • 416:Reimplement Core Reflection with Method Handles
  • 417:Vector API (Third Incubator)
  • 418:Internet-Address Resolution SPI
  • 419:Foreign Function & Memory API (Second Incubator)
  • 420:Pattern Matching for switch (Second Preview)
  • 421:Deprecate Finalization for Removal

We’ll use the process outlined below to install Java 18 in Ubuntu 22.04;

How to Install Java 18 in Ubuntu 22.04

  • Make sure system is up to date. Run the command below to update and upgrade your system.
sudo apt update && sudo apt upgrade -y

Install Java 18 in Ubuntu 22.04 from Ubuntu APT Repo

  • Run the command below to Install OpenJDK 18.
 sudo apt install openjdk-18-jdk -y

Sample output

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfwupdplugin1
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  ca-certificates-java fonts-dejavu-extra
  java-common libatk-wrapper-java
  libatk-wrapper-java-jni libice-dev
  libpthread-stubs0-dev libsm-dev libx11-dev
  libxau-dev libxcb1-dev libxdmcp-dev libxt-dev
  openjdk-18-jdk-headless openjdk-18-jre
  openjdk-18-jre-headless x11proto-dev
  xorg-sgml-doctools xtrans-dev
Suggested packages:
  default-jre libice-doc libsm-doc libx11-doc
  libxcb-doc libxt-doc openjdk-18-demo
  openjdk-18-source visualvm fonts-ipafont-gothic
  fonts-ipafont-mincho fonts-wqy-microhei
  | fonts-wqy-zenhei
The following NEW packages will be installed:
  ca-certificates-java fonts-dejavu-extra
  java-common libatk-wrapper-java
  libatk-wrapper-java-jni libice-dev
  libpthread-stubs0-dev libsm-dev libx11-dev
  libxau-dev libxcb1-dev libxdmcp-dev libxt-dev
  openjdk-18-jdk openjdk-18-jdk-headless
  openjdk-18-jre openjdk-18-jre-headless
  x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 266 MB of archives.
After this operation, 426 MB of additional disk space will be used.
Get:1 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 java-common all 0.72build1 [6,892 B]
Get:2 http://ke.archive.ubuntu.com/ubuntu impish/universe amd64 openjdk-18-jre-headless amd64 18~15ea-4 [47.3 MB]
Get:3 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 ca-certificates-java all 20190909 [12.1 kB]
Get:4 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 fonts-dejavu-extra all 2.37-2build1 [2,041 kB]
Get:5 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libatk-wrapper-java all 0.38.0-2ubuntu3 [53.0 kB]
Get:6 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libatk-wrapper-java-jni amd64 0.38.0-2ubuntu3 [49.3 kB]
Get:7 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 xorg-sgml-doctools all 1:1.11-1.1 [10.9 kB]
Get:8 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 x11proto-dev all 2020.1-1 [594 kB]
Get:9 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libice-dev amd64 2:1.0.10-1build1 [51.3 kB]
Get:10 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libpthread-stubs0-dev amd64 0.4-1build1 [5,456 B]
Get:11 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libsm-dev amd64 2:1.2.3-1build1 [18.1 kB]
Get:12 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libxau-dev amd64 1:1.0.9-1build3 [9,352 B]
Get:13 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libxdmcp-dev amd64 1:1.1.3-0ubuntu4 [26.5 kB]
Get:14 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 xtrans-dev all 1.4.0-1 [68.9 kB]
Get:15 http://ke.archive.ubuntu.com/ubuntu impish/main amd64 libxcb1-dev amd64 1.14-3ubuntu1 [80.5 kB]
  • Verify Java 18 is installed.
java --version

Install Java 18 in Ubuntu 22.04 using DEB binary package

  • Begin by downloading the installer package (.deb)
wget https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.deb
  • After that, install the package.
sudo apt install ./jdk-18_linux-x64_bin.deb
  • Then set environmental variable.
export JAVA_HOME=/usr/lib/jvm/jdk-18
export PATH=$PATH:$JAVA_HOME/bin
  • Verify Java 18 is installed.
java --version

Test the Java Installation in Ubuntu 22.04

  • Using the command below, we’ll create a sample Java program.
sudo nano demo.java

Paste the sample code below

public class hello {
  public static void main(String[] args) {
    System.out.println("Hello from itnixpro!");
  }
}
  • Then use the following command to compile and run the code.
java demo.java
  • It will print the following text, indicating that it runs.
Hello from itnixpro!
  • Congratulations on successfully installing Java 18 in Ubuntu 22.04. This brings us to the end of our article.

Read more on Java 18 Documentation

Other Tutorials

Install Bpytop System Monitor Tool on Ubuntu 22.04

Install Velociraptor agents on Ubuntu 22.04

Install Sublime Text on Ubuntu 22.04

System administrator | Software Developer | DevOps

Leave a Comment