How to Install Java 8 on Ubuntu 18.04

install java ubuntu 18.04

In this tutorial, we'll focus on how to install Java 8 on Ubuntu 18.04 from the terminal. By default, Ubuntu 18.04 comes with Java 11.

Java is an application that is a prerequisite in a wide range of software such as Tomcat Web server, Netbeans IDE, Glassfish server to mention just but a few.

The Oracle JDK License has changed for releases starting April 16, 2019, most commonly used PPA are DISCONTINUED.

How to install OpenJDK 8

By default Ubuntu 18.04 comes with Java 11. Many applications still use Java 8.

To install OpenJDK 8, run the following command:

$ apt update
$ apt install openjdk-8-jdk

To verify, run,

$ java -version

Output

openjdk version "1.8.0_282"
 OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~18.04-b08)
 OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)

How to Install default JDK/JRE (Java 11)

This is the easiest option when installing Java since it comes packaged with the Ubuntu system. This will install OpenJDK 11 which is the recommended version.

First off, let's update the system. Log in as root user and run the command below

$ apt update

Next, Install Java Runtime Environment

$ apt-get install default-jre

On the other hand, there exists a default Java installation popularly known as JDK, short for Java Development Kit. This is commonly used with IDE's such as Eclipse and Netbeans for compiling Java programs.

The default-jdk package comes with JRE, so there's no need to install JRE once you've installed JDK. At the time of writing, the default JDK installs Java OpenJDK 11 version.

To install default JDK, run

$ apt-get install default-jdk

Checking Java version

To verify that we have Java installed and check out the version, run the command below.

$ java -version

How to manage Java

As seen above we have a few Java installation in our system, and as good practice, It's advisable to set the default Java version. To achieve this, e have to run the command below

$ update-alternatives --config java

Sample Output

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      1111      auto mode
 1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      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:

Scroll using the cursor keys and Press Enter on your preferred java installation.

Finally, we need to set the JAVA_HOME environment variable.

How to set up the JAVA_HOME environment variable

To do so, Open /etc/environment

 $ nano /etc/environment

Add the path to your preferred java installation

JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre/bin/java "

Press 'CTRL + O' to Save and CTRL + X' to exit nano editor.

Reload by running the source command:

$ source /etc/environment

To verify that all went well run the following command.

$ echo $JAVA_HOME

This should display the path specified

Sample Output

/usr/lib/jvm/java-8-oracle/jre/bin/java

Related Read:

In this tutorial, we have outlined the steps on how you can install Java 8 on Ubuntu 18.04. Java is crucial for a number of software applications as mentioned earlier such as Tomcat server. Feel free to try these steps out. Stay tuned for more insightful articles from us.

Leave a Comment