How to install Java on Ubuntu 18.04

Filed Under: Java

Java is a general purpose, object-oriented and high-level programming language developed by Sun Microsystems way back in 1995. Today Java is commonly used for developing and delivering content on the web apart from powering many popular applications. In this tutorial, we will explore how to install Java on Ubuntu 18.04.

Before we dive in details about installing Java in Ubuntu 18.04, let us quickly understand a few terminologies associated with Java.

JRE: If you are not a programmer and intends to run Java based applications only then you need Java Runtime Environment.

JDK: Java Development Kit is needed if you want to do some sort of programming or develop applications using Java in addition to running Java applications. JDK contains few more tools and components than standalone JRE those are needed for developing Java applications.

OpenJDK: OpenJDK is free and open-source implementation of the Java SE Platform Edition(7) and was first released in 2007.

OracleJDK: Oracle’s JDK is the official version of Java Development Kit from Oracle and is targeted more towards an enterprise audience due to its stability and commercial support. The long term support (LTS) releases of OracleJDK are planned in every three years.

Prerequisites

  • You can open a SSH session in your Ubuntu 18.04 system using root or a sudo enabled user.

Install default Java on Ubuntu

OpenJDK 11 is the default Java package in Ubuntu 18.04 LTS at the time of writing this tutorial. Remember OpenJDK package contains an open-source variant of both JRE and JDK. Let us find out how to install JRE, JDK or both of them one by one from the default OpenJDK package.

To start with, update the package index in your system:


# apt update

Now check if Java is already installed by typing the following command from the terminal:


# java -version

If Java is not installed in your system then you will see the following output in the terminal:

Java Version Command

Java Version

To install default JRE, issue the following command in the terminal:


# apt install default-jre

Now you will be able to run Java applications in your system using JRE. To verify the installation of JRE, find the version of Java.


# java -version
OpenJDK Version on Ubuntu

OpenJDK Version

If you are interested in doing some sort of programming or development using Java then you need to install JDK that contains JRE as well. Run the following command in the terminal to install default JDK.


# apt install default-jdk

To verify the installation of default JDK, find the version Java compiler which is javac.


# javac -version
Javac Version

Javac Version

The installation of JRE/JDK from OpenJDK package is complete. If you are interested in setting up OracleJDK then follow the procedures in the next step.

Install OracleJDK on Ubuntu

To use Oracle Java, you need to buy a commercial license for the production environment. But it’s free for developmental use. Therefore you need to accept terms and conditions for non-commercial use at the time of installing OracleJDK.

Before you begin, update the package list in your system to the latest:


# apt update

To install latest OracleJDK, you need to add LinuxUprising Java PPA repository in your system. The script from this repository downloads and installs latest Java automatically from Oracle’s site.

Add the LinuxUprising PPA to your system but before that, you need to install a dependency (software-properties-common) that will enable you to add the repository in your system. To do that, issue the following commands from the terminal:


# apt install software-properties-common
# add-apt-repository ppa:linuxuprising/java

Now that the repository for OracleJDK has been added, install it by typing following command from the terminal:


# apt install oracle-java11-installer

You will be prompted to accept Oracle’s license/terms and condition twice. Use the arrow key to navigate to ‘Ok’ and ‘Yes’ and press Enter.

Accept Oracle License Aggrement

Accept Oracle License Aggrement

Accept Oracle License Aggrement Again

Accept Oracle License Aggrement Again

Once you have accepted the license, OracleJDK will be installed within a minute. If you have installed OpenJDK previously as well and want to make OracleJDK as the system default then run the following command from the terminal:


# apt install oracle-java11-set-default

To verify the installation process, type the following command in the terminal to find the version of Java:


# java -version
OracleJDK Version

OracleJDK Version

At this point, installation of OracleJDK will be complete in your Ubuntu 18.04 system. Next, proceed with setting home of java by applying the following step.

Seting JAVA_HOME on Ubuntu

It is always a good practice to set Java’s home environment once you have installed it. To do that, you need to find the path of installed Java. It is very much easy to find the path of installed Java by using update-alternatives command. To find the path, run the following command from the terminal:


# update-alternatives --config java
Java Home

Java Home

Copy the marked path (just before bin) from highlighted line and set the JAVA_HOME variable by pointing it to the copied path in the file /etc/profile.


# vi /etc/profile
...
...
export JAVA_HOME=/usr/lib/jvm/java-11-oracle/
...
...

Save the file and reload the system settings defined in /etc/profile by using source command:


# source /etc/profile

To verify the setting of Java home, echo the variable in the terminal:


# echo $JAVA_HOME
/usr/lib/jvm/java-11-oracle
Echo Java Home

Echo Java Home

Manage multiple Java Versions on Ubuntu

If you are running multiple version of Java in your system then it is possible to choose a default version for use by using update-alternatives command. To do that, run the following command from the terminal:


# update-alternatives --config java

The above command will list each installed version of Java along with installation path like the following.

Update Alternative Configure Java

Update Alternative Configure Java

The highlighted selection is the current default version of java in your system. To change the default java, type the selection number and press enter. If you change the default java then change the JAVA_HOME system variable accordingly.

Summary

That’s it! By now you have installed java in your Ubuntu 18.04 system. It is also advisable to set the home for Java since it is mandatory for running an application like Apache Tomcat, Confluence and so on. Hope you have liked the post and Thanks for going through it!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages