Cybersecurity Updates & Tools

Install Java on Ubuntu 20.04: OpenJDK 11, JDK 8, and JAVA_HOME

Java is one of the most widely used programming languages in the world. It runs on laptops, servers, mobile devices, and game consoles. You can also find Java at the core of many popular tools like Elasticsearch, Apache Kafka, and Jenkins.

Java applications run on any system with a Java runtime installed, which makes it highly portable across platforms.

This guide shows you how to install Java on Ubuntu 20.04, switch between multiple versions, and set the JAVA_HOME environment variable.

Understanding Java packages:

  • JRE (Java Runtime Environment): runs Java programs but cannot build them
  • JDK (Java Development Kit): includes the JRE plus tools for compiling and debugging
  • OpenJDK: the free, open-source implementation of Java — recommended for most users
  • Oracle Java: includes extra commercial features but requires a paid license for production

If you are not sure which to install, go with OpenJDK 11 JDK. It covers the most use cases.

<strong>Prerequisite:</strong>&nbsp;You need sudo access to follow these steps.

Install Java on Ubuntu: OpenJDK 11

Java 11 is the current long-term support (LTS) version and the default Java in Ubuntu 20.04. Install it with:

bashsudo apt updatesudo apt install openjdk-11-jdk

Verify the installation:

bashjava -version

Output:

openjdk version "11.0.7" 2020-04-14OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

If you only need to run Java programs (not build them), install just the JRE to save disk space:

bashsudo apt install openjdk-11-jre

Install OpenJDK 8

Java 8 is an older LTS version that is still widely used. Many tools built before Java 11 became standard require it, including older enterprise systems, some CI/CD tools, and specific game server software. Check your application’s documentation to find out which version it needs.

Install OpenJDK 8:

bashsudo apt updatesudo apt install openjdk-8-jdk

Verify it installed correctly:

bashjava -version

Set Default Java Version and JAVA_HOME

If you have more than one Java version installed, switch between them with:

bashsudo update-alternatives --config java

The output lists all installed versions. Type the number of the version you want as the default and press Enter.

Setting JAVA_HOME

Some applications like Tomcat and Gradle use the JAVA_HOME variable to locate the Java installation. Run the update-alternatives command above to find your Java path, then open /etc/environment:

bashsudo nano /etc/environment

Add this line at the end:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
<strong>Note</strong>: the path shown by <code>update-alternatives</code> ends in <code>/bin/java</code>. Remove <code>/bin/java</code> and use just the directory portion when setting <code>JAVA_HOME</code>.

Load the variable into your current session:

bashsource /etc/environment

Verify it is set:

bashecho $JAVA_HOME

Output:

/usr/lib/jvm/java-11-openjdk-amd64
<strong>Note:</strong>&nbsp;<code>/etc/environment</code>&nbsp;applies to all users system-wide. To set&nbsp;<code>JAVA_HOME</code>&nbsp;for one user only, add the export line to their&nbsp;<code>~/.bashrc</code>&nbsp;file instead.

Uninstall Java

To remove a Java version you no longer need:

bashsudo apt remove openjdk-11-jdk

Replace openjdk-11-jdk with the package name of the version you want to remove.

Java is now installed and configured on your Ubuntu system. OpenJDK 11 is the safest starting point for most Ubuntu users, whether you are running a Java application or building one from scratch. Got questions? Leave a comment below.