Home » Linux Howto » How to Install Java 8 on Ubuntu 18.04

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.

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

How to install Oracle JDK 8

If you want to be more specific and install Oracle JDK, you'll be required to add Oracle's PPA.

add-apt-repository ppa:webupd8team/java

Next, update the system.

apt update

Install Oracle JDK 8 which is the most stable version by running the command below

apt-get install oracle-java8-installer

The following pop up will be displayed. Press 'Enter'.

install Java on Ubuntu

You'll then be prompted to accept the license agreement. Use the Cursor Keys and Press 'Enter' on 'Yes'.

install Java on Ubuntu

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

  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-oracle/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

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

How to uninstall Oracle Java

To completely remove Oracle Java from your system, first, run the commands below to check which version is installed

dpkg --list | grep -i jdk

Output

ii oracle-java8-installer 8u171-1~webupd8~0 all Oracle Java(TM) Development Kit (JDK) 8
ii oracle-java8-set-default 8u171-1~webupd8~0 all Set Oracle JDK 8 as default Java

To remove Oracle Java 8, run

apt-get purge oracle-java8-installer

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