How to install JAVA with APT on Ubuntu 20.04?

Overview

It's no secret that Java is one of the most widely used programming languages, which requires Java Virtual Machine (JVM) as its run-time environment for executing Java applications.

Here, we'll install JAVA using APT, which simplifies system administration and facilitates software installation. Given that application software in Linux is packaged, package managers are used for their management, of which APT (Advanced Packaging Tool) is the package manager by default.

In this tutorial, we’ll tell you how to use Ubuntu 20.04's default APT package manager to install JRE (Java Runtime Environment) and JDK (Java Development Kit).

Prerequisites

  • Configured Ace compute instance

  • SSH connected Text editor

  • Internet connectivity

Get Started

Step 1: JRE installation:

Ubuntu 20.04 comes pre-installed with the default Open-source JDK-11 version. Since we are using the default JDK version, we must first update the package index using the command:

sudo apt update 

Let’s check if Java is installed already or not:

java –version 

You'll be prompted to the following output if java is currently not installed.

For successful Java installation, we need to install Java Runtime Environment (JRE), which is a software layer that runs on the top of each Java application and fulfils its need for the run-time libraries and dependencies.

Run the command below, to install Java Runtime Environment (JRE) from OpenJDK 11:

sudo apt install default-jre 

Once installed, you can again verify the java installation using the command below:

 java –version 

Step 2: JDK installation:

In addition to JRE, java applications need Java Development Kit (JDK) to compile, debug and run platform-specific java-based applications.

To install JDK, run the following command:

sudo apt install default-jdk 

Let’s verify the JDK installation, using the command below:

javac -version 

After the successful JDK installation, you’ll get the following output:

Step 3: JAVA_HOME Environment Setup:

Most Java programs and applications use the JAVA_HOME Environment variable for determining the Java installation location.

For setting up JAVA_HOME Environment, first, you need to locate, where Java is installed, using the command below:

readlink –f /usr/bin/java 

You’ll get the following output, resulting in each installed java and its installation path:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java 

Copy this path, as we need to put it into the file, in the upcoming steps:

Now, you need to open the /etc/environment file using any text editor.

Here we are using nano text editor.

sudo nano /etc/environment 

Paste the following command at the end of the /etc/environment file.

JAVA_HOME=”/usr/lib/jvm/java-11-openjdk-amd64" 

The JAVA HOME path for each user on your system will be set by now.

Save and exit the text editor.

Now, reload this file to apply the changes to the current session.

source /etc/environment 

Using the following command, you may check if the environment is set up correctly:

Echo $JAVA_HOME 

You’ll get the following output:

Last updated