How To Install Oracle Java JDK 11 / 8 on Ubuntu 18.04 / Linux Mint 19

Today we will be going through the steps to install Oracle Java JDK 11 / 8 on Ubuntu 18.04. Java SDK is the base for the JAVA developers, and this guide will be more useful for them to build a development environment when they especially use Ubuntu.

Open up a terminal and switch to the root user.

sudo su -

OR

su -

Install Oracle Java JDK 11 / 8 on Ubuntu 18.04

We can install Oracle Java JDK in two ways,

Method 1: Install Oracle Java JDK 11 / 8 on Ubuntu 18.04 using Official Source

Method 2: Install Oracle Java JDK 11 / 8 on Ubuntu 18.04 using PPA

Method 1: Install Oracle Java JDK 11 / 8 on Ubuntu 18.04 using Official Source

You can download the Oracle JDK either using command line or browser. Recommend you to use the browser to download the Oracle JDK.

Download using browser

Oracle Java JDK 11: (Current)

Download Oracle JDK 11

Oracle Java JDK 10: (End of public updates)

Download Oracle JDK 10

Oracle Java JDK 8: (End of Public Updates – Jan 2019)

Download Oracle JDK 8

Oracle Java JDK 9: (End of Life)

Download Oracle JDK 9

Download using terminal

### Oracle Java JDK 11 ###

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/11.0.1+13/90cf5d8f270a4347a95050320eef3fb7/jdk-11.0.1_linux-x64_bin.tar.gz

### Oracle Java JDK 8 ###

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz

Extract the downloaded JDK archive using the tar command to the desired directory (Ex. /usr/)

tar -zxvf jdk-*

mv jdk* /usr/

Once you moved the java to the location you want, then you must run update-alternatives command to install the Java on your system.

### Oracle JDK 11 ### 

update-alternatives --install /usr/bin/java java /usr/jdk-11.*/bin/java 2

### Oracle JDK 8 ###

update-alternatives --install /usr/bin/java java /usr/jdk1.8.*/bin/java 2

Set the default java using the below command.

update-alternatives --config java

The above command would list all Java JDK installed on your system, like below.

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/jdk-11.0.1/bin/java                         2         manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

If you require Oracle Java JDK 11, then choose the 1 and press enter.

Method 2: Install Oracle Java JDK 11 / 8 on Ubuntu 18.04 using PPA

Add the JAVA JDK PPA using the add-apt-repository comamnd.

### Oracle JDK 11 ### 

sudo add-apt-repository ppa:linuxuprising/java

### Oracle JDK 8 ###

sudo add-apt-repository -y ppa:webupd8team/java

Now, install Java JDK using the apt-get command.

### Oracle JDK 11 ###

sudo apt install -y oracle-java11-installer

### Oracle JDK 8 ###

sudo apt install -y oracle-java8-installer

During the installation, you would need to accept the Oracle binary licenses.

To set the default Java, install the below package according to your requirement.

### Oracle JDK 11 ###

sudo apt install -y oracle-java11-set-default

### Oracle JDK 8 ###

sudo apt install -y oracle-java8-set-default

Verify Java

Now, check the java version using the following command.

java -version

Output:

Oracle Java JDK 11:

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

Oracle Java JDK 8:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Setup JAVA Environmental Variables

Some Java application installations require prior configuration of environmental variables like JAVA_HOME , JRE_HOME,etc.

To set JAVA environment variables, create a new file under /etc/profile.d directory.

sudo nano /etc/profile.d/javajdk.sh

Place the variables based on the JDK location and version.

JDK 11:

export PATH=$PATH:/usr/jdk-11.0.1/bin
export JAVA_HOME=/usr/jdk-11.0.1
export J2SDKDIR=/usr/jdk-11.0.1

JDK 8:

export PATH=$PATH:/usr/jdk1.8.0_191/bin
export JAVA_HOME=/usr/jdk1.8.0_191
export JRE_HOME=/usr/jdk1.8.0_191/jre/
export J2SDKDIR=/usr/jdk1.8.0_191
export J2REDIR=/usr/jdk1.8.0_191/jre

Load the environments into the current session.

source /etc/profile.d/javajdk.sh

That’s All.