How to Install Java on Ubuntu

June 19, 2019

Introduction

Java is one of the most popular programming languages. It is used for developing anything from lightweight mobile to desktop applications.

This step-by-step guide will show you how simple it is to install Java on Ubuntu 18.04 or 20.04.

How to install Java on Ubuntu.

Note: This guide provides instructions that work on Ubuntu 18.04, Ubuntu 20.04 and any other Ubuntu-based distribution (including Linux Mint, Kubuntu, and Elementary OS). If you are looking for other Java installation guides, please refer to:

Prerequisites

  • A user account with sudo privileges
  • Access to the command-line/terminal window
  • The apt (apt-get) command

Java Versions

There are four Java platforms available:

  1. Java Standard Edition (Java SE)
  2. Java Micro Editions (Java ME)
  3. Java Enterprise Edition (Java EE)
  4. JavaFX

In this document, we look at different packages within the Java SE.

The Ubuntu repository offers two (2), open-source Java packages, Java Development Kit (Open JDK) and Java Runtime Environment (Open JRE). You use JRE for running Java-based applications, while JDK is for developing and programming with Java.

Also available is Oracle Java, another SE implementation, which has additional commercial features. You can find the official Oracle JDK through a third-party repository. However, bear in mind the license only allows non-commercial use of the software.

Installing Java on Ubuntu

You can install one or several Java packages. You can also decide which version you want on your system by installing a specific version number. The current default and LTS version is Java 11.

Install OpenJDK

1. Open the terminal (Ctrl+Alt+T) and update the package repository to ensure you download the latest software version:

sudo apt update

2. Then, you can confidently install the latest Java Development Kit with the following command:

sudo apt install default-jdk

3. Confirm the installation by typing y (yes) and press Enter.

Command to install default Java Development Kit on Ubuntu.

Install OpenJRE 11

1. Before you install the Java Runtime Environment, make sure you update the repository with:

sudo apt update

2. Next, type in the following command to download and install Java RE:

sudo apt install default-jre

Type y (yes) and press Enter to confirm the installation.

Install Java Runtime Enviorment on Ubuntu.

Install Oracle Java 11

To download the official Oracle JDK, you first need to download a third-party repository.

We include instructions for installations from 2 (two) different package repositories. You can decide from which one you prefer to download.

Option 1: Download Oracle Java from Webupd8 PPA

1. First, add the required package repository by typing:

sudo add-apt-repository ppa:webupd8team/java

Hit Enter when prompted.

2. Make sure to update your system before initiating any installation:

sudo apt update

3. Now, you can install Java 11, the latest LTS version:

sudo apt install oracle-java11-installer

4. Optionally, you can set this Java version as the default with the following command:

sudo apt install oracle-java11-set-default

Option 2: Download Oracle Java from Linux Uprising PPA

1. Before adding the new repository, install the required packages if you do not have them on your system yet:

sudo apt install software-properties-common

2. Next, add the repository with the following command:

sudo add-apt-repository ppa:linuxuprising/java

3. Update the package list before installing any new software with:

sudo apt update

4. Then, download and install the latest version of Oracle Java (version number 11):

sudo apt install oracle-java11-installer

Verify Java is Installed on Your System

Now verify whether the software is part of your system.

To check the version of Java running, use the command:

java –version

The successful installation of Java on Ubuntu will output details, as in the image below.

Confirm the version of Java thatis currently installed.

If there is no Java version, the system will say it is unable to find the software.

Image shows that no version of Java was found.

Install Specific Version of Java

If for some reason you do not wish to install the default or latest version of Java, you can specify the version number you prefer.

Install Specific Version of OpenJDK

You may decide to use Open JDK 8, instead of the default OpenJDK 11.

To do so, open the terminal and type in the following command:

sudo apt install openjdk-8-jdk
Image shows hoe to install specific version of JDK.

Verify the version of java installed with the command:

java –version
How to check the version of JDK installed.

Install Specific Version of Oracle Java

When you download the Oracle Java packages from a third-party repository, you have to type out the version number as part of the code.

Therefore, if you want other versions of Java Oracle on your system, change that number accordingly.

The command for installing Oracle JDK is the following (the symbol # representing the Java version):

sudo apt install oracle-java#-installer

For instance, if you want to install Java 10, use the command:

sudo apt install oracle-java10-installer
Command to install Java 10 on Ubuntu.

How to Set Default Java Version

As you can have multiple versions of Java installed on your system, you can decide which one is the default one.

First, run a command that shows all the installed versions on your computer:

sudo update-alternatives --config java
How to select the default Java version.

The image above shows that there are two alternatives on this system. These choices are represented by numbers 1 (Java 11) and 2 (Java 8), while the 0 refers to the current default version.

As the output instructs, you can change the default version if you type its associated number (in this case, 1 or 2) and press Enter.

How to Set JAVA_HOME Environment Variable

The JAVA_HOME environment variable determines the location of your Java installation. The variable helps other applications access Java’s installation path easily.

1. To set up the JAVA_HOME variable, you first need to find where Java is installed. Use the following command to locate it:

sudo update-alternatives --config java
Set up Java Home Varialbe Enviornment.

The Path section shows the locations, which are in this case:

  • /usr/lib/jvm/java-11-openjdk-amd64/bin/java (where OpenJDK 11 is located)
  • /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java (where OpenJDK 8 is located)

2. Once you see all the paths, copy one of your preferred Java version.

3. Then, open the file /etc/environment with any text editor. In this example, we use Nano:

nano /etc/environment

4. At the end of the file, add a line which specifies the location of JAVA_HOME in the following manner:

JAVA_HOME=”/your/installation/path/”

For example, if we copied the installation path for Java 11 in the previous step, the added line in the text code editor would be:

Installation path for Java 11.

How to Uninstall Java on Ubuntu

In case you need to remove any of the Java packages installed, use the apt remove command.

To remove Open JDK 11, run the command:

sudo apt remove default-jdk

To uninstall OpenJDK 8:

sudo apt remove openjdk-8-jdk

Once you run the command, confirm the continuation of the process by pressing y (yes) and the packages will be removed.

Note: Refer to our comprehensive breakdown of Best Java IDEs to find the best one for your needs.

Conclusion

This guide has shown you how to install Java on Ubuntu 18.04. It also instructed you on how to change the default version and set the home environment variable.

Now, you are ready to use the powerful Java platform!

For more tutorials on how to install Java on other systems, make sure to read our article on how to install Java on Windows.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Enable SSH on Ubuntu
April 23, 2019

When establishing a remote connection between a client and a server, a primary concern is ensuring a secure ...
Read more
How to Install Yarn on Ubuntu 18.04
December 8, 2022

Yarn is a tool for managing, updating, and sharing your Java code. It works through node.js, and it helps to ...
Read more
How to Install Java 11 & 12 on CentOS 7
June 23, 2019

As a highly popular programming language, Java is used for developing anything from lightweight mobile to ...
Read more
How to Install Apache Tomcat 9 on Ubuntu 18.04
July 2, 2019

Apache Tomcat is a free, open-source, lightweight application server used for Java-based web applications.
Read more