How To Install Java 8 On Ubuntu

By bhagwatchouhan
How To Install Java 8 On Ubuntu

In this post, we will discuss all the steps required to install Java 8 distributed by Oracle on the popular Linux distribution i.e. Ubuntu. It includes the steps for Ubuntu 18.04 LTS. The steps should be similar to other releases of Java, Ubuntu and other flavors of Linux.

Notes: Make sure that you have gone through the License Agreement for JDK 8 released after April 16, 2019.

 

Download JDK

Open the browser and search for Download JDK 8. It will show the JDK download page with the change in the licensing model as shown in Fig 1 and Fig 2.

Oracle JDK License Changes

Fig 1

Oracle JDK Download

Fig 2

Click on the download link as highlighted in Fig 2. It will ask to accept the License Agreement as shown in Fig 3.

Oracle JDK License Agrement

Fig 3

 

Install Java

Open the terminal and make directory /usr/java/oracle to keep java installations distributed by Oracle. We can install multiple versions of Java in this directory. Now copy the downloaded file to this location and extract it as shown below:

>sudo mkdir -p /usr/java/oracle
>cd /usr/java/oracle
>sudo cp /data/setups/jdk-8u241-linux-x64.tar.gz jdk-8u241-linux-x64.tar.gz
>sudo tar -xzvf jdk-8u241-linux-x64.tar.gz

The above steps will install JDK to the path /usr/java/oracle/jdk1.8.0_241.

 

Set Environment Variables

In this step, we will configure the environment variable to use the JDK installed by us via the command line.

>sudo nano /etc/profile

Scroll down by pressing Page Down button and add at the end of this file:

# Java 8
JAVA_HOME=/usr/java/oracle/jdk1.8.0_241
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Now press Ctrl + O and hit Enter to write our change. Press Ctrl + X to exit the nano editor. The nano editor should look like the one shown in Fig. 4.

Oracle JDK Path

Fig 4

 

Configure Java Commands

We can configure the Java commands to use the newly installed JDK by default. We can check the installed Java before and after executing these commands as shown below:

# Check version
java -version
# Configure Java Alternatives sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/oracle/jdk1.8.0_241/bin/java" 1
# Configure Javac Alternatives sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/oracle/jdk1.8.0_241/bin/javac" 1
# Configure Java WS Alternatives sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/oracle/jdk1.8.0_241/bin/javaws" 1
# Check version java -version
# Use only in case of multiple JDKs installed # Configure Java sudo update-alternatives --config java
# Configure Java Compiler sudo update-alternatives --config javac

The output of these commands is displayed in Fig. 5. You might be required to switch among the installed JDKs in case of multiple installations.

Oracle JDK Install

Fig 5

This is how we can install Java 8 distribution from Oracle on Ubuntu 18.04 (Bionic Beaver).

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan