How to Install Oracle JDK in Ubuntu 18 in 2020
Tags: java
Published: 2019-11-05 14:02:32

In this short guide, I'll show you the new way of installation Oracle JDK: 8, 11 or 13 on Ubuntu 18 or Linux Mint 19 (and above). There are many articles describing the old steps which are not working anymore due to changes from Oracle. Below you can find the whole process - step by step.

Note 1: If you need OpedJDK then you can install it with command like: `sudo apt-get install openjdk-8-jdk` or by using [SDKMAN](https://sdkman.io/) - I prefer to use SDKMAN which gives many benefits like working with different versions and easy updates.

Note 2: Be sure that you read the [Important Oracle JDK License Update](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) before using Oracle JDK.

## Step 1. Create Oracle account and download Oracle JDK

First in order to avoid confusion there are two Java downloads:
* [Java Downloads for Linux](https://www.java.com/en/download/linux_manual.jsp)
* [Java SE Downloads](https://www.oracle.com/technetwork/java/javase/downloads/index.html)

The second one is the one which has JDK and can be used for development. It's hosted on the Oracle website and needs Oracle account in order to download the JDK. Once the download is complete you can chose the Java version that you want to download - i.e. (or get recent from the link above):
* [Java SE Development Kit 8 Downloads](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
* [Java SE Development Kit 11 Downloads](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
* [Java SE Development Kit 13 Downloads](https://www.oracle.com/technetwork/java/javase/downloads/jdk13-downloads-5672538.html)

For Ubuntu/Linux Mint you need to download `.tar.gz` file for your architectuer: 32 or 64 Bits.

## Step 2. Install Oracle JDK in /usr/lib/jvm

I check huge amount of articles trying to find what is the best place to install oracle JDK on Ubuntu. There are different choices so I decided to select:

    /usr/lib/jvm
    
which was the previous installation place for Oracle JDK 8.

Once the download is complete you can move the `.tar.gz` file to the folder of installation and extract it there:

```bash
cd Downloads
sudo mv jdk-8u231-linux-x64.tar.gz /usr/lib/jvm/
sudo tar xzvf jdk-8u231-linux-x64.tar.gz
```

This will install the JDK in folder - `jdk1.8.0_231`:
    
    /usr/lib/jvm/jdk1.8.0_231
    
## Step 3. Configure Java for update-alternatives (optional)

This step is needed in order to make newly installed Java visible for `update-alternatives`. What you need to do is:

```bash
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_231/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_231/bin/javac 1
```

Now if there are more than one Java version you can manage then easily by:

```bash
sudo update-alternatives --config java
```

and then select the best Java for your machine(in the example below Oracle JDK is not installed):

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

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

Press <enter> to keep the current choice[*], or type selection number: 
```

## Step 4. Set up variable JAVA_HOME (optional)

Many programs depends on environmental variable JAVA_HOME. So it's a good idea to set it up in order to avoid problems later. The setup of this variable for Debian based systems can be done by opening user profile settings:

```bash
sudo nano ~/.bashrc
```

Then you need to add somewhere near the bottom of the file:

    export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_231
    export PATH=$JAVA_HOME/bin:$PATH


Then you need to reset the terminal (by command `source ~/.bashrc`) or open new one and test the variable by `echo`:

```bash
source ~/.bashrc
echo $JAVA_HOME
```

With this last step the installation of Oracle JDK 8 on Ubuntu/Linux Mint/Debian is complete.

## Bonus: The scripts for installation of Oracle JDK 11 or 13

Download **Oracle JDK 11** (the link is above) from the site and then execute:

```bash
sudo mv jdk-11.0.5_linux-x64_bin.tar.gz /usr/lib/jvm/
tar xzvf jdk-11.0.5_linux-x64_bin.tar.gz
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.5/bin/java 11
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.0.5/bin/javac 11
```


Download **Oracle JDK 13** (the link is above) from the site and then execute:

```bash
sudo mv jdk-13.0.1_linux-x64_bin.tar.gz /usr/lib/jvm/
tar xzvf jdk-13.0.1_linux-x64_bin.tar.gz
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-13.0.1/bin/java 13
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-13.0.1/bin/javac 13
```

Note: if you wonder what are the numbers at the end of this command then this is the explanation:

> a priority number. The highest the number, the higher the priority. That means that the alternative with the highest number will be executed by default, unless we manually set which is the default.


## Resources

* [Java Donwloads](https://www.oracle.com/technetwork/java/javase/downloads/index.html)
* [Linux 64-bit installation instructions for Java](https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)