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 shows you how simple it is to install Java on Ubuntu 18.04.

how to install java on ubuntu


Note: This guide provides instructions for Ubuntu 18.04. But the installation is the same for the 16.04 version and any other Ubuntu-based distribution (including Linux Mint, Kubuntu, and Elementary OS).


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

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.

Make sure this is the version you now have 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 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.

Conclusion

This guide showed you how to install Java on Ubuntu 18.04. It also instructed changing the default version and setting the home environment variable.

Now you are ready to use the powerful Java platform!