How to install Java (OpenJDK) on Ubuntu 20.04

Java is one of the most famous programming languages ​​used to build various applications such as Jenkins, Tomcat server, IntelliJ IDEA and many other system software.

This tutorial will show you how to install various versions of Java, Java Runtime Environment (JRE) and Java Development Kit (JDK) on an Ubuntu 20.04 LTS computer.

Install the default Java

The easiest way to install Java on Ubuntu 20.04 is to use the default version that comes with the Ubuntu operating system.

First, let us update the software repository by running the following command:

$ sudo apt update

Next, it is recommended that you check whether Java is installed? Run the command:

$ java -version

The following output indicates that Java is not installed on your Ubuntu 20.04:

Java is not installed

Now, you can install the default JRE by executing the following command:

$ sudo apt install default-jre

After the installation is complete, let us verify the Java version:

OpenJDK version 11.0.8

OpenJDK version 11.0.8

If you want to compile and run Java applications, you need a program called “javac”, which is provided with the JDK (Java Development Kit). To install the default JDK, let us run the following command:

$ sudo apt install default-jdk

You can verify the JDK installation by checking the version of the Java compiler:

$ javac -version

javac-version

javac-version

Install OpenJDK 11

At the time of writing, Java 11 is the latest LTS version. It is the default development and runtime environment for Java on Ubuntu 20.04.

To install OpenJDK 11, run the following command:

$ sudo apt install openjdk-11-jdk

Check the Java version:

$ java -version

Output:

openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Install OpenJDK 8

In some cases, your application requires an earlier version of Java to run. For example, if you want to install Java version 8 on Ubuntu 20.04, run the following command:

$ sudo apt install openjdk-8-jdk

Verify the Java version by typing the command’java -version’.

Output:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1ubuntu1-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Install Oracle Java 11

Unlike OpenJDK, Oracle JDK is not released under the license GPL v2, but under the Oracle Binary Code License Agreement. By default, the Oracle JDK cannot be installed through the package manager. You must download the Oracle JDK 11 installer on the Oracle website: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html

Select Linux compressed archive and click download:

Oracle JDK installer

Oracle JDK installer

You will be redirected to the login screen of the Oracle website. You must create an account to download the Oracle JDK Installer.

Accept the agreement to download the oracle JDK installer

Accept the agreement to download the oracle JDK installer

After downloading the Java installer (jdk-11.0.8_linux-x64_bin.tar.gz), let us install the “add-apt-repository” command:

$ sudo apt install software-properties-common

Then, import the signing key to verify the software installation on Ubuntu 20.04:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

Signing key

Signing key

Add the Java repository to the package source list and update:

$ sudo add-apt-repository ppa:linuxuprising/java
$ sudo apt update

Create a local directory for the downloaded Oracle JDK installer:

$ sudo mkdir -p /var/cache/oracle-jdk11-installer-local/
$ sudo cp jdk-11.0.8_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

Now, you can install Oracle JDK 11 by running the following command:

$ sudo apt install oracle-java11-installer-local

Let us accept the Oracle Technology Network License Agreement for Oracle Java SE to complete the installation:

Click the

Click the “OK” button to accept the Oracle Technology Network License Agreement

Manage multiple Java installations

So far, you have installed multiple Java versions on Ubuntu 20.04. The default Java version used by the operating system must be configured. You can do this by running the following command:

$ sudo update-alternatives --config java

Update alternatives

Update alternatives

Enter the desired option and press Enter to select the desired Java version.

To configure the default Java compiler version (javac), let us run:

$ sudo update-alternatives --config javac

Set environment variables

In many cases, developers must specify the $JAVA_HOME environment variable when building Java programs.

In order to set the $JAVA_HOME variable, you need to use the’update-alternatives’ command to find the Java installation directory.

For example, Oracle Java is located in “/usr/lib/jvm/java-11-oracle/bin/”.

Add the path to the “/etc/environment” file

JAVA_HOME="/usr/lib/jvm/java-11-oracle/bin/"

Reload the environment file to apply the changes:

$ source /etc/environment

Verify the $JAVA_HOME environment:

$ echo $JAVA_HOME
/usr/lib/jvm/java-11-oracle/bin/

in conclusion

In this tutorial, we learned how to install and configure Java on Ubuntu 20.04. Java is essential for many software applications.

Thank you for reading, please leave your suggestions in the comments section below.

Sidebar