How to Install Java on Ubuntu

It’s a pretty straightforward and easy process, and we’ll show you step-by-step instructions on how to install Java on Ubuntu. The instructions would work for Ubuntu 14.04, 16.04, 18.04, and any other LTS release.

This should work for any Ubuntu-based distro, even non-LTS versions like 17.10, but they were tested on Ubuntu 16.04. They should work on both servers and desktops.

In a hurry? Click here and follow the instructions.

Before we begin installing Java

Some things you’ll need before we start installing Java:

  • An Ubuntu server. You can get one from Vultr or any other cloud hosting provider
  • A sudo/root user. All commands below are executed by the root user. If you’re using a non-root user then you’ll need to append ‘sudo’ to most commands.
  • An SSH client like MobaXterm (if you use Windows), or SSH enabled (if you use Ubuntu)
  • Make sure you don’t have Java already installed. If you do, remove it, or if you want to use multiple Java versions/installs, then keep it and configure them later.

So that’s pretty much it.

There are multiple ways of installing Java

We’ll order them based on difficulty (easiest first):

  1. Contact us and we’ll install and configure Java for you (any version, any OS)
  2. Installing the default (Ubuntu’s) JDK using apt-get (use this if you don’t know what to choose)
  3. Installing Java using Oracle’s JDK using apt-get
  4. Installing Java manually using Oracle’s JDK (not recommended for beginners)

How to install Java (the default JDK) on Ubuntu using apt-get

We’ll start with our instructions on how to install the latest recommended version of Java (JDK) using the Ubuntu package. By installing the JDK (Java Development Kit), you’ll also install the JRE (Java Runtime Environment). This is the OpenJDK package which is an open source implementation of Java.

Step 1: Update Ubuntu

The first thing you should always do is update your system. You can do so by running the following commands:

apt-get update && apt-get upgrade

Step 2: Install the default JDK

Run the following command:

apt-get install default-jdk

And that’s it. You can now move on to step 3 and configure your Java.

How to install Java using the Oracle JDK

If you don’t want to use Ubuntu’s default open source JDK, you can use the official Oracle JDK.

Step 1: Update Ubuntu

Again, you should always update your system first before you do anything else. Run the following commands:

apt-get update && apt-get upgrade

And install the required package if you don’t have it already installed:

apt-get install software-properties-common

Step 2: Add the Java repository

The first thing you need to do is add a 3-rd party repository to get the Oracle JDK. We’ll use the one from WebUpd8, but you can use any other repository:

add-apt-repository ppa:webupd8team/java

And then update your package list again:

apt-get update

Step 3: Install Java

Currently, Java 9 is considered stable, though you’ll find many other outdated tutorials saying 9 is still a “developer/beta/preview” release, it’s actually stable. So you should install Java 9. Java 10 should be released soon (March 2018).

So to install the JDK 9th (stable) version, run the following command:

apt-get install oracle-java9-installer

If, for any reason, you need the 8th version, run the following command:

apt-get install oracle-java8-installer

And that’s it. You can now move on to step 4 and configure your Java.

How to install Java manually using Oracle’s JDK on Ubuntu

This step is not recommended for beginners. You won’t get updates from Ubuntu’s repositories, you’ll have to manually update your Java each time there’s a new release. If you want to install a custom version of Oracle’s JDK that’s not available via a package, you can follow the steps below.

Step 1: Update

As always. Update first:

apt-get update && apt-get upgrade

Step 2: Download the JDK

Go to Oracle’s downloads page and select the version you want to download. We’ll use Java’s Standard Edition 9.0.1 for the purpose of this tutorial. Replace the version numbers if you’re going to use a different version. Java 9 is only available for 64-bit systems, so if your Ubuntu is 32-bit, you must use a different version.

Download the .tar.gz file:

wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie"

The Java version and this link often change, so please go to the download page yourself and copy the link yourself.

Create a directory for your Java installation:

mkdir /opt/java

This is where we’ll install Java. You can use a different directory if you want to install it elsewhere like /usr/local

Step 3: Install Java

Now extract the .tar.gz (tarball) file to the directory you previously created

tar -zxf jdk-9.0.4_linux-x64_bin.tar.gz -C /opt/java

And that’s it. move on to step 4 and configure your Java.

Setting up Java on Ubuntu

Now it’s time to configure your Java.

Check what Java version you’re using

First, make sure you’ve installed Java on your system and check what version you have:

java -version

The output should be something like:

Java(TM) SE Runtime Environment (build 9.0.4+11) 
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

So based on this, we’re using version 9.

Set a default if you have multiple Java installations

If you have multiple Java installations, you can set a default one by using the following command:

update-alternatives --config java

You can also use this command to check if you have multiple installations.

You’ll get an output with a list of installed Javas. Press enter to keep the default one without any changes or enter a number to select a different default Java.

Set the JAVA_HOME variable

You’ll most likely need to set the JAVA_HOME variable so other applications can find the location of your Java installation. To find the Java installation path, run the previous command again:

update-alternatives --config java

And copy the installation path – second column – under “Path”.

Next, open the file “/etc/environment” with a text editor

nano /etc/environment

And add the following line at the end of the file:


Of course, make sure you update the path with the one you previously copied, example:


Save the file and then reload it:

source /etc/environment

To test if everything’s done right, you can check your JAVA_HOME variable using:


And the output should be your Java installation path.

There are multiple ways of setting up the JAVA_HOME variable, and multiple other variables that you may need to set, depending on what you need and what you’re going to use.

If you need any help or have any questions, leave a comment.

Leave a comment

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

15 thoughts on “How to Install Java on Ubuntu