Install java on ubuntu

In this tutorial we will learn how to install java and setup required java variables on ubuntu.

Java is one of the famous programming languages which allow developers to compile programs once and then run anywhere.

In this tutorials commands are provided to install java on ubuntu 16.04.

Installing java using default packages.

First of all we will update all existing packages on OS to latest version.

apt-get update && apt-get upgrade

Once OS’s packages updated install latest version of Java Runtime Environment (JRE) by executing following command:

apt-get install default-jre

You can install jdk instead of jre which is required to compile java programs. JDK is recommended as it includes jre as well.

apt-get install default-jdk

That’s all java is ready to use.

java -version
javac -version

Now setting up java installation path as JAVA_HOME variable. To find location execute command

update-alternatives --config java

Once you get path set it as JAVA_HOME by adding it to file /etc/environment like


To activate this variable for current session execute following command


Verify it is activated by executing command


That’s it.

Leave a Reply

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