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. These instructions will work for Ubuntu 16.04, 18.04, Ubuntu 20.04, and any other LTS release, or even other Ubuntu-based distros like Xubuntu.

This should work for any Ubuntu-based distro, even non-LTS versions like 20.10, but they were tested on Ubuntu 18.04 and Ubuntu 20.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 latest Java using the default (Ubuntu’s) JDK using apt-get (use this if you don’t know what to choose)
  3. Installing Java 11 or Java 13 using Oracle’s JDK with 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. Depending on when you’re reading this, the command will install Java 11 or a newer version. You can now move on to step 3 and configure your Java.


How to install Java 17 using the Oracle JDK

If you don’t want to use Ubuntu’s default open source JDK, you can use the official Oracle JDK through a 3-rd party repository.

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 Linux Uprising, but you can use any other repository:

add-apt-repository ppa:linuxuprising/java

And then update your package list again:

apt-get update

Step 3: Install Java 17

So to install the JDK 17th (LTS), latest version, run the following command:

apt-get install oracle-java17-installer

The command is pretty similar for other versions too (if you want to install another version, replace the XX)

apt-get install oracle-javaXX-installer

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


No longer recommended: How to install Java 8 using the Oracle JDK

To install Java 8 (which will reach its end of life January 2019!), follow these instructions:

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

So to install the JDK 8th, outdated 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 17 for the purpose of this tutorial. Replace the version numbers if you’re going to use a different version. Java 17 is only available for 64-bit systems, so if your Ubuntu is 32-bit, you must use a different version.

Update 2019: Now that you need an Oracle.com account to download the file below, you’ll have to manually download the file and continue the process after the next step. Alternatively, you can just use the Ubuntu repositories.

Download the .tar.gz file:

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

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-17_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 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

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

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:

JAVA_HOME="/your/java/installation-path"

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

JAVA_HOME="/usr/lib/jvm/java-17-oracle"

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:

echo $JAVA_HOME

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.

This post was last modified on January 17, 2022 3:57 pm

Categories: Knowledgebase
Tags: javaUbuntu

View Comments (34)

Related Post