This tutorial is going to take you through on how to Install Java 18 in Ubuntu 20.04. Java is an object-oriented programming language with a high level of abstraction and as few implementation dependencies as possible. It’s a general-purpose programming language designed to let programmers write once and run anywhere (WORA), which means that compiled Java code can run on any platform that supports Java without requiring recompilation. Java applications are usually compiled to bytecode, which may execute on any Java virtual machine (JVM), regardless of the computer architecture.
Some of Java 18 new features include;
- 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 are going to Install Java 18 in Ubuntu 20.04 using the methods listed below.
- Install Java 18 in Ubuntu 20.04 using OpenJDK Archive
- Install Java 18 in Ubuntu 20.04 using DEB Binary package
How to Install Java 18 in Ubuntu 20.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 20.04 using OpenJDK Archive
- Download JDK using
wget
command.
wget https://download.java.net/java/GA/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-x64_bin.tar.gz
- Next extract the downloaded file.
sudo tar xvf openjdk-18_linux-x64_bin.tar.gz
- Then move the extracted directory to /opt/ location.
sudo mv jdk-18/ /opt/
- Set the environment variables using the commands below.
echo 'export JAVA_HOME=/opt/jdk-18' | tee -a ~/.bashrc
echo 'export PATH=$PATH:$JAVA_HOME/bin '|tee -a ~/.bashrc
source ~/.bashrc
- Confirm the version installed.
echo $JAVA_HOME
Check the version;
java --version
Sample output
openjdk 18 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)
Install Java 18 in Ubuntu 20.04 using DEB Binary package
- Lets start by downloading the .deb installer package.
wget https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.deb
- Next install the package.
sudo apt install ./jdk-18_linux-x64_bin.deb
Sample output
Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'jdk-18' instead of './jdk-18_linux-x64_bin.deb' The following additional packages will be installed: libc6-i386 libc6-x32 The following NEW packages will be installed: jdk-18 libc6-i386 libc6-x32 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 5,504 kB/162 MB of archives. After this operation, 348 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 /home/kigz/jdk-18_linux-x64_bin.deb jdk-18 amd64 18-ga [156 MB] Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 libc6-i386 amd64 2.31-0ubuntu9.7 [2,725 kB] Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 libc6-x32 amd64 2.31-0ubuntu9.7 [2,779 kB] Fetched 5,504 kB in 2min 0s (45.8 kB/s) Selecting previously unselected package libc6-i386. (Reading database ... 185541 files and directories currently installed.) Preparing to unpack .../libc6-i386_2.31-0ubuntu9.7_amd64.deb ... Unpacking libc6-i386 (2.31-0ubuntu9.7) ... Selecting previously unselected package libc6-x32. Preparing to unpack .../libc6-x32_2.31-0ubuntu9.7_ amd64.deb ... Unpacking libc6-x32 (2.31-0ubuntu9.7) ... Selecting previously unselected package jdk-18. Preparing to unpack .../kigz/jdk-18_linux-x64_bin. deb ... Unpacking jdk-18 (18-ga) ... Setting up libc6-x32 (2.31-0ubuntu9.7) ... Setting up libc6-i386 (2.31-0ubuntu9.7) ... Setting up jdk-18 (18-ga) ... Processing triggers for libc-bin (2.31-0ubuntu9.7) ...
- Then set environmental variable as shown below.
export JAVA_HOME=/usr/lib/jvm/jdk-18
export PATH=$PATH:$JAVA_HOME/bin
- Confirm version installed.
echo $JAVA_HOME
java --version
Test the Java Installation in Ubuntu 20.04
- We will create a demo java program using the command below.
sudo nano demo.java
Next paste the sample code below into the file.
public class hello { public static void main(String[] args) { System.out.println("Hello from itnixpro!"); } }
- Then compile and run the code using the following command.
java demo.java
- It will output the below output which means everything is running alright.
Hello from itnixpro!
- You have successfully installed Java 18 in Ubuntu 20.04, Cheers! This marks the end of our article.
Read more on Java 18 Documentation
Other Tutorials
Install AngularJS on Ubuntu 22.04