07.06.2023

How To Install Java with apt on Ubuntu 18.04

Java and JVM (Java Virtual Machine) are required for many types of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.

This guide will help you learn how to install different versions of the Java Runtime Environment (JRE) and Java Developer Kit (JDK) with apt.

How to install Java with apt on Ubuntu 18.04:

Java Runtime Environment

The easiest way to install Java is to use the version included in the Ubuntu package. By default, Ubuntu 18.04 includes Open JDK (open source JRE and JDK version).

This package installs OpenJDK version 10 or 11.

First, update the package lists:

sudo apt update

Then you need to check if Java is already installed:

java -version

If Java was newer installed, yo’ll see the following results:

Run the following command to install OpenJDK:

sudo apt install default-jre

Type y (yes) and press Enter to resume installation.

This command installs the Java Runtime Environment (JRE). It allows you to run almost any Java software.

Now check the Java version:

java -version

JRE is installed!

Java Development Kit

To compile and run some specific Java programs in addition to the JRE, you may need the Java Development Kit (JDK). To install the JDK, run the following command, which also installs the JRE:

sudo apt install default-jdk

Type y (yes) and press Enter to resume installation.

This command installs the Java Development Kit (JDK).

Now check JDK version with command:

javac -version

JDK is installed!