Install Java on Ubuntu

As a Java developer, you will need to install it on your system one time or the other. Contrary to popular belief, it is pretty simple to install Java on your Ubuntu machine. In this article, we will explain the following three ways to install a stable version of Java on your Ubuntu system:

  • Through OpenJDK binaries
  • Installation Via PPA
  • Through official Oracle Java website

The steps and commands described in this article have been run on an Ubuntu 18.04 LTS system.

Java Installation through OpenJDK binaries

The simplest and quickest way to install Java on your Ubuntu system is through Ubuntu’s repository. This way you can install a stable version of Java through the open source Java runtime binaries called the OpenJDK

Open your Ubuntu Terminal either through the Dash or the Ctrl+alt+T shortcut.

Enter the following command as root in order to install execute of Java OpenJDK version 11:

$ sudo apt install openjdk-11-jdk

Install java OpenJDK with apt

Java will be installed on your system and ready for use. You can confirm installation and check the installed version through the following command:

$ java -version


$ java --version

For execute of Java OpenJDK version 9, you can use the following command:

$ sudo apt install openjdk-9-jdk

Check installed java version

Java Installation Via PPA

You can also install Java on your system by using PPA repositories maintained by Webupd8 Team. Enter this command in your Terminal as sudo to add the PPA repository:

$ sudo add-apt-repository ppa:webupd8team/java

Add Oracle Java PPA repository

Then you need to update your Ubuntu repositories through the following command:

$ sudo apt update

Finally, use the following command in order to install Java through the added repository:

$ sudo apt install oracle-java8-set-default

During installation, you will be required to agree to the license agreement through the following message:

Agree to Java license

Press Enter for selecting Ok after which the following message will appear:

Accept Oracle Binary License

Select Yes and then press Enter to continue installation. After the installation is complete, you can verify the installation and version information as follows:

$ java -version


$ java --version

Java version test

Installation through official Oracle Java website

Another way to install Java is by downloading a Java install package from the official Java website:

From the list of available versions, you can browse to the version you need and then click the Download button:

Download Oracle Java

I am interested in Java SE Development Kit 11.

Java SE Development Kit 11

Select the Accept License Agreement option and then download the dk-11_linux-x64_bin.tar.gz package.

Save downloaded Java Archive

Select the Save File option after which the .tar.gz file will be saved to the Downloads folder.

Copy this file to your Home folder and then run the following command in order to install through this package:

sudo tar -C /opt/java-jdk -zxf ~/jdk-11_linux-x64_bin.tar.gz

Unpack the archive

The downloaded version of Java will be installed on your system. You can verify the installation through the following command:

$ java -version


$ java --version

Setting a Default Version from all the Installed Java Versions

If you have multiple version of Java installed on your Ubuntu, you can select one as your default one:

First, let us get a list of installed Java versions through the following command:

$ sudo update-alternatives --get-selections | grep java

On my system, the current version of Java is Java 8 and Java 11 is also available for use:

Set default Java version

The following command lets you configure a different version of Java as default:

$ sudo update-alternatives --config java

In the following image, you can see that a Selection number is assigned to each version of Java. The asterisk ‘*’ with selection number 2 shows that Java 8 is the currently configured default option.

Configure Java alternatives setting in Ubuntu

You can enter a different selection number for respective Java version and then press Enter. Your default version of Java will then be updated with the selection you make here. You can verify is by running the ‘java -version’ command.

Through this article, you have learned three ways to install Java through the command line. You can also install multiple versions of Java but set one as a default version through the procedure described in this article.

How to install Java on your Ubuntu System

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn