You have installed Ubuntu, but you realized you could not use its full potential without Java. That is understandable, and it is why we are investigating how to install Java on Ubuntu in this article. The important thing to mention is that the tutorial is suitable for all LTS releases of Ubuntu.

What You Need to Know about Installing Java on Ubuntu

The great thing about the process of setting up Java on Ubuntu is that you can use multiple ways and they are all simple. You will, however, need to enter commands as the root user of the system. Alternatively, make sure to add the “sudo” prefix when typing commands. The other things you will need include an SSH client, and an Ubuntu server.

Before we start presenting different methods of installing Java, keep in mind that you should update Ubuntu before starting any of the setups below.

Open up your terminal and type the following command:

apt-get update && apt-get upgrade

Once you are done with that, choose the desired method below and start the installation.

METHOD 1: Instructions to Install Java on Ubuntu with apt-get

The simplest way of installing Java is to utilize the Ubuntu package. However, keep in mind that you cannot choose versions this way. Instead, the system will install the default Java, and also install the Java Runtime Environment.

You only need to execute a single command:

apt-get install default-jdk

The system will complete the installation of the currently recommended Java version. Please take a look at the configure section to learn about configuring Java on your system properly.

METHOD 2: Instructions to Use Oracle JDK to Install Java 11

Using Oracle JDK is the most popular alternative if you do not want to use Ubuntu’s repository for any reason.

We will assume that you have updated the system and installed the necessary package to perform the installation.

Use the following command to add a reliable repository:

add-apt-repository ppa:XXX/java

Replace “XXX” with the desired repository. You can use Linux Uprising or Webupd8 Team.

Run the system update command again to be on the safe side. Finally, install Java 11 on Ubuntu by using this command:

apt-get install oracle-java11-installer

METHOD 3: Instructions to Use Oracle JDK to Install Java 8

Once again, make sure to update Ubuntu and set up necessary packages.

The method is similar to the previous one. You also start by adding the desired repository:

add-apt-repository ppa:XXX/java

Update the system again, and use the following command to set up JDK 8:

apt-get install oracle-java8-installer

Configure Java on Ubuntu

If you are not sure, make sure to identify what version of Java your system is using:

java -version

Linux will display the information about Java on your screen, and you should focus on what is shown in the “build XX” section as the “XX” marks the version.

Pick a Default Installation

You may have different Java versions installed on your Linux, and you want to choose the preferred one. Here is how to do that:

update-alternatives --config java

The above line will order the system to display all Java setups you have installed. Each one will be marked with a separate number. Pick the desired installation and make it default by pressing the number that corresponds it. Alternatively, if you do not want to change the default setup, press Enter.

Setting the JAVA_HOME Variable

It is an important variable because it informs other applications about the Java installation path. In case you do not know the location, use this command to find it:

update-alternatives --config java

Linux will present extensive information on the screen, but you need to find “Path” and copy it.

Use a text editor to open the following file:

nano /etc/environment

Find the last line and add this below it:

JAVA_HOME="folder_path_to_java

Finalize the process by saving the file. If you want to check whether everything was done right, use the following line:

echo $JAVA_HOME

The system should display the folder path of the installed Java.

Wrap Up

We hope that you enjoyed our tutorial on how to install Java on Ubuntu. There is a way to do it manually, but why would you go through the hassle when the command line can accelerate the process. Pick one of the preferred methods above, and you will set up Java on your Linux in a matter of minutes!