Objective

The objective of this tutorial is to install Java on Ubuntu. We will be installing the latest version of Oracle Java SE Development Kit (JDK) on Ubuntu 18.04 Bionic Beaver Linux. This will be performed in three ways: Installing Java using the Ubuntu Open JDK binaries, installing Java via PPA and installing Java using the official Oracle Java binaries.

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver
  • Software: - Java(TM) SE Runtime Environment 8,9,10 or 11

Requirements

Privileged access to to your Ubuntu 18.04 Bionic Beaver Linux system is required to perform this installation.

Difficulty

EASY

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Instructions

Install Java using the Ubuntu Open JDK binaries

In most cases you do not need to look further to install Java on Ubuntu than Ubuntu's repository which contains an opensource version of Java runtime binaries called Open JDK.
To install Ubuntu Java Open JDK version 11 execute:
$ sudo apt install openjdk-11-jdk
and for Java Open JDK 8 run:
$ sudo apt install openjdk-8-jdk

Install Java on Ubuntu via PPA

Add PPA Repository

Using Webupd8 Team's PPA repository we can install Java on Ubuntu automatically using the apt command. Webupd8 Team currently maintains Oracle Java 8 PPA repositories for Ubuntu 18.04 Bionic Beaver.

Let's start by adding a PPA repository:

Java version 8

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

Java version 10

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

Install Java on Ubuntu

After adding PPA repository we can move to installing java on Ubuntu. Executing apt search oracle-java command should now show multiple java versions available for install.

Namely they are java8 and java10.

To install Java 8 execute:
$ sudo apt install oracle-java8-set-default
To install Java 10 execute :
$ sudo apt install oracle-java10-set-default


The above commands will automatically install selected java version and set all necessary java environment variables.

$ java --version
java 10.0.2 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

Set default Java Version Manually

In case you need to manually switch between installed Java version start by listing your current java environment variable settings:

$ sudo update-alternatives --get-selections | grep ^java


For more verbose version of the above command execute sudo update-alternatives --get-selections | grep java.

To set java to eg. Java 10 executable run:

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-10-oracle/bin/java      1091      auto mode
  1            /usr/lib/jvm/java-10-oracle/bin/java      1091      manual mode
* 2            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode

Press  to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/lib/jvm/java-10-oracle/bin/java to provide /usr/bin/java (java) in auto mode
Confirm your selection:

$ sudo update-alternatives --get-selections | grep ^java
java                           auto     /usr/lib/jvm/java-10-oracle/bin/java
javac                          manual   /usr/lib/jvm/java-8-oracle/bin/javac
javadoc                        manual   /usr/lib/jvm/java-8-oracle/bin/javadoc
javafxpackager                 manual   /usr/lib/jvm/java-8-oracle/bin/javafxpackager
javah                          manual   /usr/lib/jvm/java-8-oracle/bin/javah
javap                          manual   /usr/lib/jvm/java-8-oracle/bin/javap
javapackager                   manual   /usr/lib/jvm/java-8-oracle/bin/javapackager
javaws                         manual   /usr/lib/jvm/java-8-oracle/jre/bin/javaws
javaws.real                    auto     /usr/lib/jvm/java-10-oracle/bin/javaws.real

Use the update-alternatives --config JAVA-EXECUTABLE-HERE to change the environmental path to any other java executable binaries as required.

Install Java using the Official Oracle binaries

The following section will describe a manual Oracle Java installation on Ubuntu 18.04.

Java Download

Navigate your browser to the official Oracle java download page and download the latest binaries.

We are interested in eg. jdk-10.0.1_linux-x64_bin.tar.gz file.

Download java file and save it into your home directory:

$ ls ~/jdk-10.0.1_linux-x64_bin.tar.gz 
/home/linuxconfig/jdk-10.0.1_linux-x64_bin.tar.gz

Install Java on Ubuntu 18.04

Now, that your java download is completed and you have obtained the Oracle JDK binaries, execute the following linux commands to perform the java ubuntu install into a /opt/java-jdk directory:

$ sudo mkdir /opt/java-jdk
$ sudo tar -C /opt/java-jdk -zxf ~/jdk-10.0.1_linux-x64_bin.tar.gz

Set Defaults

The following linux commands will set Oracle JDK as system wide default. Amend the below commands to suit your installed version:

$ sudo update-alternatives --install /usr/bin/java java /opt/java-jdk/jdk-10.0.1/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java-jdk/jdk-10.0.1/bin/javac 1

Confirm Java Installation

What remains is to check for installed java version:

$ java --version
java 10.0.1 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$ javac --version
javac 10.0.1
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.