How To Install Java 18 On Ubuntu 20.04 LTS

How To Install Java 18 On Ubuntu 20.04 LTS

It provides all the steps required to install Java 18 on Ubuntu 20.04 LTS and getting started with Java development using the Hello World example.

June 26, 2022

This tutorial provides all the steps required to install Java 18 on the popular Linux distribution i.e. Ubuntu. It provides all the steps for Ubuntu 20.04 LTS (Focal Fossa). The steps should be the same for other versions of Ubuntu, and Linux.

You can also follow the steps to install How To Install VSCode For Java On Ubuntu, How To Install IntelliJ IDEA for Java on Ubuntu, and How To Install Eclipse For Java Development On Ubuntu published by Tutorials24x7. You may also follow the How To Install OpenJDK 18 On Windows to install the latest OpenJDK on Windows and How To Install Java 18 On Windows to install Oracle JDK.

Notes: The Java 17 is an LTS version which means there will be long-time support from Oracle for the same. The premier support will be available till September 2026 and extended support will be available till September 2029. You can follow How To Install Java 17 On Ubuntu 20.04 LTS.

Download JDK

Open the browser and search for Download JDK 18 or click the link to download from the Oracle website. It will show the JDK download page as shown in Fig 1.

Install Java 18 on Ubuntu 20.04 LTS - Download Options

Fig 1

Click the download link as highlighted in Fig 1. It will start downloading JDK 18 for Ubuntu.

Install JDK

Open the terminal and make directory /usr/java/oracle to keep all the oracle java installations at the same place. We can install multiple versions of Java in this directory. Now copy the downloaded file to this location and extract it as shown below:

# Make directory for Oracle JDKs
sudo mkdir -p /usr/java/oracle
# CD
cd /usr/java/oracle
# Copy the download to Oracle JDKs directory
sudo cp /data/setups/jdk-18.0.1.1_linux-x64_bin.tar.gz jdk-18.0.1.1_linux-x64_bin.tar.gz
# Extract JDK
sudo tar -xzvf jdk-18.0.1.1_linux-x64_bin.tar.gz

The above steps will install JDK to the path /usr/java/oracle/jdk-18.0.1.1.

Set Environment Variables

In this step, we will configure the environment variable to use the JDK installed by us via the command line.

# Update Profile
sudo nano /etc/profile

Scroll down by pressing Page Down button and add at the end of this file:

# Java 18
JAVA_HOME=/usr/java/oracle/jdk-18.0.1.1
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Now press Ctrl + O and hit Enter to write our change. Press Ctrl + X to exit the nano editor. The nano editor should be similar to Fig 2.

Install Java 18 on Ubuntu 20.04 LTS - System Path

Fig 2

Configure Java Commands

We can configure the Java commands to use the newly installed JDK by default. We can check the installed Java before and after executing these commands as shown below:

# Check version
java -version
# Output
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

It shows that Java 16 was installed on my system. In case JDK is not installed on your system, the messages should be similar as shown below.

# Check version
java -version
# Output
Command 'java' not found, but can be installed with:

sudo apt install openjdk-11-jre-headless # version 11.0.15+10-0ubuntu0.20.04.1, or
sudo apt install default-jre # version 2:1.11-72
sudo apt install openjdk-16-jre-headless # version 16.0.1+9-1~20.04
sudo apt install openjdk-17-jre-headless # version 17.0.3+7-0ubuntu0.20.04.1
sudo apt install openjdk-8-jre-headless # version 8u312-b07-0ubuntu1~20.04
sudo apt install openjdk-13-jre-headless # version 13.0.7+5-0ubuntu1~20.04

If JDK is not installed on your system, use the below mentioned commands to configure the java commands.

# Configure Java Alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/oracle/jdk-18.0.1.1/bin/java" 1
# Configure Javac Alternatives
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/oracle/jdk-18.0.1.1/bin/javac" 1
# Check version
java -version

# Output
java version "18.0.1.1" 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)

In case JDK is already installed on your system, use the below mentioned commands to install JDK 18 with existing JDKs.

# Use only in case of multiple JDKs installed

# Configure Java
sudo update-alternatives --config java

# Output - In case multiple JDKs are already installed
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/java/oracle/jdk1.8.0_261/bin/java 2 auto mode
* 1 /usr/java/openjdk/jdk-15/bin/java 1 manual mode
2 /usr/java/oracle/jdk1.8.0_261/bin/java 2 manual mode

Press <enter> to keep the current choice[*], or type selection number:

# Output - In case only one JDK was installed
There is only one alternative in link group java (providing /usr/bin/java): /usr/java/oracle/jdk-18.0.1.1/bin/java
Nothing to configure.

# Configure Java Compiler
sudo update-alternatives --config javac

# Output - In case JDK is already installed
There are 2 choices for the alternative javac (providing /usr/bin/javac).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/java/oracle/jdk1.8.0_261/bin/javac 2 auto mode
* 1 /usr/java/openjdk/jdk-15/bin/javac 1 manual mode
2 /usr/java/oracle/jdk1.8.0_261/bin/javac 2 manual mode

Press <enter> to keep the current choice[*], or type selection number:

# Output - In case only one JDK was installed
There is only one alternative in link group javac (providing /usr/bin/javac): /usr/java/oracle/jdk-18.0.1.1/bin/javac
Nothing to configure.

Now check the priority number in case multiple JDKs were installed. You can configure your JDK 18 using the priority assigned to it as shown below. You can choose priority based on your requirements.

# Configure Java Alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/oracle/jdk-18.0.1.1/bin/java" 1
# Configure Java
sudo update-alternatives --config java

# Output
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/java/oracle/jdk-16.0.1/bin/java 1 auto mode
1 /usr/java/oracle/jdk-16.0.1/bin/java 1 manual mode
2 /usr/java/oracle/jdk-18.0.1.1/bin/java 1 manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/java/oracle/jdk-18.0.1.1/bin/java to provide /usr/bin/java (java) in manual mode

# Configure Javac Alternatives
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/oracle/jdk-18.0.1.1/bin/javac" 1

# Configure Java Compiler
sudo update-alternatives --config javac

# Output
There are 2 choices for the alternative javac (providing /usr/bin/javac).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/java/oracle/jdk-16.0.1/bin/javac 1 auto mode
1 /usr/java/oracle/jdk-16.0.1/bin/javac 1 manual mode
2 /usr/java/oracle/jdk-18.0.1.1/bin/javac 1 manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/java/oracle/jdk-18.0.1.1/bin/javac to provide /usr/bin/javac (javac) in manual mode

The output of these commands is shown in Fig 3. We might need to configure active Java if it is previously installed on the system as shown in Fig 3.

Install Java 18 on Ubuntu 20.04 LTS - JDK Selection

Fig 3

These are the basic steps required to install the most recent version of Java on Ubuntu.

Hello World

In this section, we will write our first program in Java using the nano editor.

>sudo mkdir -p /data/programs/java
>cd /data/programs/java
>sudo nano HelloWorld.java

Now write the first program in Java as shown below, save the program and exit the editor.

// Hello World
public class HelloWorld {

        // The main method
        public static void main( String[] args ) {

                // Print Hello World
                System.out.println( "Hello World !!" );
        }
}

Use the javac and java to compile and execute the program as shown below.

// Compile the program
sudo javac HelloWorld.java

// Execute the program
sudo java HelloWorld

// Program output
Hello Java !!

These are the basic steps to write, compile, and execute Java programs.

Summary

This tutorial provided all the steps required to install Java 18 on Ubuntu 20.04 LTS and also provided the steps to configure it to use from the console. The last section explained the steps to write, compile, and execute the first Java program. You may submit your comments to join the discussion on installing Java 18 on Ubuntu and other Linux systems.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS