Installing Java in Ubuntu

This post shows how to install Orcale JDK using apt-get or by downloading from the Oracle site on Ubuntu. It also shows how to install OpenJDK.

Installing Oracle JDK using apt-get

For installing Java in Ubuntu first you need to add Oracle’s PPA (Personal Package Archive) so that Ubuntu knows where to look for updates.

sudo add-apt-repository ppa:webupd8team/java

Here note that this repository is not maintained by Oracle. You can refer this URL– But the WebUpd8 Oracle Java PPA doesn’t include any Java binaries, just a script that automatically downloads and install Oracle Java 8.

Then you need to update your package repository. For that use the following command.

sudo apt-get update

After that install Oracle JDK. For that use the following code.

sudo apt-get install oracle-java8-installer

Installing Oracle JDK by downloading manually

If you want to manually download and install Oracle JDK then follow the given steps. You may opt for this way of installing Java in Ubuntu if you are behind a proxy/firewall that is creating problem with connecting to repository.

Go to – and download the required version of Oracle JDK.

Create a directory /usr/java for your Java installation-

installing java

By default gzip file will be downloaded in Downloads directory so copy it from there to the directory /usr/java which you have created.

java installation

Then untar the installation.

java installation

Setting JAVA_HOME environment variable and Java path

In order to find where Java is installed applications refer to JAVA_HOME environment variable. So you need to set up the environment variable and also append the path to Java /bin directory to the PATH. For that you need to edit /etc/environment file.

sudo gedit /etc/environment

append path to bin in existing PATH- :/usr/java/jdk1.8.0_151/bin

Add JAVA_HOME environment variable at the end of the file.


Make sure to use installation path as per your Java installation.

After that reload the variables by running this command

source /etc/environment

You can verify that the environment variable is set correctly or not by running the following command-


Also check the Java installation by running the following command –

java -version

You should get the output for the version you have installed which should look something like this.

java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Installing OpenJDK

If you want to install OpenJDK

sudo apt-get install openjdk-8-jdk

If you want to install just the Java Runtime Environment

sudo apt-get install openjdk-8-jre

Want to write your first Java program after installing Java, have a look at this post- Writing First Java Program – Hello World

Related Posts

That’s all for the topic Installing Java in Ubuntu. If something is missing or you have something to share about the topic please write a comment.

You may also like

Share this post:

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.