How to install java in Ubuntu 16.04 using the terminal

In this article, I am going to describe how to install java in Ubuntu 16.04 using the terminal.


First I am going to add Oracle’s PPA(Personal Package Archive) using terminal command.


You can open terminal easily by pressing Ctrl + Alt + t


Then type and enter below code.


         sudo add-apt-repository ppa:webupd8team/java            


After that update your package repository using this terminal command.


        sudo apt-get update

Java 8 is the latest stable version of Java and the recommended version to install. You can install java8 using following terminal command.


         sudo apt-get install oracle-java8-installer
Many programs use the JAVA_HOME environment variable to determine the Java installation location. To set this environment variable, first need to find out where Java is installed. You can find the java location by executing below terminal command.
        sudo update-alternatives –config java
Example from my terminal


Selection    Path                                                 Priority        Status
0           /usr/lib/jvm/java-8-oracle/jre/bin/java    1081         auto mode
* 1        /usr/lib/jvm/java-8-oracle/jre/bin/java     1081         manual
You should select the asterisk marked path as your java installation location.In my installation, java has installed in  /usr/lib/jvm/java-8-oracle/jre/bin/java  directory. So I have to add that directory as the JAVA_HOME variable.


Now we have to add JAVA_HOME variable to /etc/environment file. To do this you can open that file using gedit text editor, running following terminal command.


      sudo gedit /etc/environment
At the end of /etc/environment file, you have to add the following line according to the output given by sudo update-alternatives –config java terminal command.


In my installation, I had to add following line.


You should add your relevant java installation path accordingly.


Then save and close the file. You can reload that file using following terminal command.


     source /etc/environment
You can test whether the environment variable has been set correctly by executing the following terminal command. This command will display which directory is recorded as $JAVA_HOME


     echo $JAVA_HOME


You can check the Java version after installing the package using following terminal command.


     javac -version
If you like this post, spread by sharing it on social media. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by

Up ↑

%d bloggers like this: