How to Install Java on Ubuntu 20.04 LTS Linux

Can you explain how to install Java on Ubuntu 20.04 LTS Linux system?

Java is one of the most popular software and specifications initially created by James Gosling at Sun Microsystems. We can create a cross-platform application in Java for mobile phones or desktop users. Currently, Java is owned by Oracle Corporation. OpenJDK and Oracle Java are the most prominent implementation of Java specifications. Oracle Java comes with restrictive a License and is preferred by enterprise users. OpenJDK is the default on Ubuntu 20.04 LTS and many Linux distros. Let us see how to install Java Runtime Environment (JRE) and Java Development Kit (JDK) on Ubuntu Linux 20.04 LTS.
Tutorial requirements
Operating system/appUbuntu Linux
Root privileges requiredYes
DifficultyEasy (rss)
Estimated completion time5m
Table of contents


How to install Java on Ubuntu 20.04 LTS

Ubuntu Linux ships with OpenJDK version 8 and 11. Let us find out about versions using the apt-cache command and sort command:
$ apt-cache --names-only search 'openjdk-[0-9]*-(jre|jdk)' | sort -t '-' -k 2 -n
Different versions of JDK and JRE available for installation:

openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-8-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark
openjdk-11-jdk - OpenJDK Development Kit (JDK)
openjdk-11-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-11-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-11-jre-dcevm - Alternative VM for OpenJDK 11 with enhanced class redefinition
openjdk-11-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-11-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-13-jdk - OpenJDK Development Kit (JDK)
openjdk-13-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-13-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-13-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-13-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-14-jdk - OpenJDK Development Kit (JDK)
openjdk-14-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-14-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-14-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-14-jre-zero - Alternative JVM for OpenJDK, using Zero

The headless version is for server users where you don’t need desktop GUI components of JAVA.

Step 1 – Installing OpenJDK version 14 on Ubuntu 20.04 LTS

Update the repo and then install the OpenJDK version 14 using the apt command:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install openjdk-14-jdk
## for servers try headless version ##
$ sudo apt install openjdk-14-jdk-headless

How to Install Java on Ubuntu 20.04

Installing OpenJDK version 14 on Ubuntu server

Step 2 – Verification

Let us print the Java version:
$ java --version

openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)

Step 3 – Install multiple versions of Java/OpenJDK

We can install multiple versions on Ubuntu. It allows us to run different apps as per the Java version. For example, here I am going to install OpenJDK 11:
$ sudo apt install openjdk-11-jdk

Setting up the default Java version

We can use symbolic links to setup the default Java version. For example, change the default Java version, run the update-alternatives command:
$ sudo update-alternatives --config java
Make sure you type the number of the version you want using as the default Java and press the Enter key:

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

  Selection    Path                                         Priority   Status
* 0            /usr/lib/jvm/java-14-openjdk-amd64/bin/java   1411      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-14-openjdk-amd64/bin/java   1411      manual mode

Press  to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

I changed version to JDK 11. Let us print default version:
$ java --version

openjdk 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)

Step 4 – Set up JAVA_HOME environment variable

The syntax is as follows using the export command for bash:
export JAVA_HOME=/path/to/openjdk
# For OpenJDK version 14 #
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
# For OpenJDK version 11 #
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Pro tip: Use the update-alternatives --list java command to list all possible JDK paths.

Display the settings using the echo command/printf command:

printf "Java HOME PATH : %s\n" $JAVA_HOME

Finally, update the environment variables config file and set JAVA_HOME as follows:
$ sudo vim /etc/environment
## OR ##
$ sudo nano /etc/environment

Append the settings:
Save and close the file.
How To Install Java with Apt on Ubuntu 20.04

How to set JAVA_HOME for individual users

The JAVA_HOME set in /etc/environment file is for all Linux users. However, individual users and apps can override JAVA_HOME and PATH settings using the ~/.bash_profile or ~/.bashrc. For instance, Linux user can append PATH settings to end of file as follows:

echo "export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'" >> ~/.bashrc
echo "export PATH='$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin'" >> ~/.bashrc

Step 5 – Testing your Java version by writing “Hello World” program

Create a new file called using a text editor such as vim/nano:
$ vim
Append the following code:

// Old good Hello World in Java:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class HelloWorld {
  public static void main(final String[] args) {
    final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("h:mm:ss a 'on' MMMM d, yyyy'.'");
    final LocalDateTime now =;
    System.out.println("Hello, World! The current time is " + dtf.format(now));

Run it:

Hello, World! The current time is 8:47:20 AM on September 12, 2020.


This tutorial explained how to install different versions of Java and manage them using the CLI. Once Java is installed, we can write an application. Moreover, your Ubuntu 20.04 LTS is now ready to install other popular opensource apps such as Tomcat.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

5 comments… add one
  • Nina Sep 12, 2020 @ 9:12

    Nice tutorial. Can someone explain sort -t '-' -k 2 -n to me?

    • 🐧 Vivek Gite Sep 12, 2020 @ 9:17
      • -t '-' : Use ‘-‘ as separator field instead of non-blank to blank transition.
      • -k 2 : Sort on 2nd field. In this example, JDK version.
      • -n : Numeric sort.

      In other words, I am sorting out apt-cache output using the sort command as per the JDK version so that we can easily read it.

  • Marco Sep 12, 2020 @ 12:42

    Nice tutorial, but really consider using sdkman for installing Java. The versions are always up to date and installation of both sdkman and the JDK is a breeze.

    • 🐧 Vivek Gite Sep 12, 2020 @ 13:44

      That is a good suggestion. Thanks!

    • Deepa Sep 16, 2020 @ 3:30

      Why do you trust and recommend sdkman over in built package manager?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.