Java is one of the most popular object-oriented programming languages in the world. It is used by millions of companies in the world to develop different types of cross-platform applications, including desktop apps, websites, mobile apps, smart cards, IoT, and much more.

Java is platform-independent which means you just write the Java program once and then run it everywhere.

In this article, you will learn to install different versions of the Java Runtime Environment (JRE) and the Java Development Kit (JDK) on Ubuntu 18.04. You will install both OpenJDK as well as official Oracle Java binaries. The same instructions apply for Ubuntu 16.04 and other Ubuntu-based distributions like Kubuntu, Linux Mint and Elementary OS.

Prerequisites

To complete this article successfully:

  1. You need a Ubuntu 18.04 machine (get one from DigitalOcean).
  2. You must be logged in as a user with sudo privileges.

Java Editions and Variants

There are three main Java editions: Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). The Standard Edition is used for building desktop applications. Java EE is for web applications and Java ME is for mobile applications — both of these are built on top of Java SE. This article covers the steps to install Java SE (Standard Edition).

There are two main implementations of Java: OpenJDK and Oracle Java. Although both are almost same in terms of functionality, the Oracle Java cannot be used in production without a commercial license. There are no such licensing restrictions for OpenJDK.

There are two types of Java packages available: Java Runtime Environment (JRE), and the Java Development Kit (JDK).

If you want to run a compiled Java program, then you only need JRE which includes Java Virtual Machine (JVM), the Java class library, and other files. For Java development, you need to install JDK. It includes JRE, tools for development and debugging the program, and other libraries.

If you are not sure which Java version or implementation is right for you, just install the default OpenJDK version available on Ubuntu 18.04.

Installing the Default OpenJDK (Java 11)

The simplest way for installing Java is to use the default binaries available in Ubuntu repositories. By default, Ubuntu 18.04 includes the OpenJDK packages. Follow the instructions below to install the current version of OpenJDK (Java 11) on Ubuntu 18.04.

First, update the apt package index:

$ sudo apt update

Now, check if Java is already installed:

$ java -version

If Java is not already installed, you will see the following output:

Command 'java' not found, but can be installed with:

sudo apt install default-jre            
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless

Execute the following command to install the default version of OpenJDK:

$ sudo apt install default-jdk

Verify the installation by typing the following command:

$ java -version

You will see the following output:

openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)

That's it. You should have successfully installed OpenJDK 11 (the latest LTS release) on your Ubuntu 18.04 machine.

Note: By default, JRE is included in the JDK release. If you only need JRE, install the default-jre package instead.

Installing OpenJDK 8

In previous section, we learned to install the default OpenJDK version. But it is not necessary. You can also install any other version of OpenJDK.

Java 8 is still the widely-used version of Java, though the public maintenance has ended in January 2019. If your application needs Java 8, you can install the OpenJDK 8 by executing the following command:

$ sudo apt install openjdk-8-jdk

Verify the installation by typing:

$ java -version

You should see the following output:

openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

It is also possible to install only JRE 8 by running the following command:

$ sudo apt install openjdk-8-jre

Installing Oracle Java 11

If you want to install the official version of Oracle JDK, you need to add a new package repository Linux Uprising PPA.

To install Oracle Java 11, which is the latest LTS version, first install the dependencies required to add a new repository:

$ sudo apt install software-properties-common

Enable the Linux Uprising PPA by typing:

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

You will be asked to press ENTER to confirm the addition of the new repository.

Once the repository is added, update the package index by typing:

$ sudo apt update

Note: Since the Oracle Java 11 can no longer be downloaded directly from Oracle website, you need to do an additional step. Go to Oracle website, create an account and download the Oracle Java 11 *.tar.gz archive, and place the archive in /var/cache/oracle-jdk11-installer-local/.

Now you can install the Oracle Java 11 by running the following command:

$ sudo apt install oracle-java11-installer-local

Verify the installation by typing:

$ java -version

If the Oracle Java was installed successfully, you will see the details printed on the console.

Before installing Oracle Java, make sure you read and understand the Oracle JDK License. The license only permits non-commercial use of the software, such as personal/educational use and development use.

Setting the Default Java Version

You can have multiple Java versions installed on one machine. To configure which version should be used as a default on the command line, use the update-alternatives command like below:

$ sudo update-alternatives --config java

You will see the following output if you have installed multiple Java versions:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 

To change the default Java version, just type the number in the Selection column and press ENTER.

You can do the same for other Java commands such as javac (Java compiler), keytool, and javadoc:

$ sudo update-alternatives --config javac

Check out How to change the default Java version on Ubuntu guide to find out more configuration options.

Setting the JAVA_HOME Environment Variable

Some applications developed using Java use the JAVA_HOME environment variable to determine the Java installation location.

You can easily find the current Java version installation path by using the update-alternatives command:

$ sudo update-alternatives --config java

This command will list all installations of Java along with their installation paths. In our case, the installation paths are as follows:

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Copy the installation path of your preferred installation. Next, open the /etc/environment file by typing the following command:

$ sudo nano /etc/environment

At the end of this file, add the following line:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Don't forget to replace the path with your own copied preferred Java version path.

After saving the file, execute the following command to reload the changes to your current session:

$ source /etc/environment

To verify that JAVA_HOME environment variable is set correctly, run the following command:

$ echo $JAVA_HOME

You will see the following path you just set:

/usr/lib/jvm/java-11-openjdk-amd64/bin/

The above method sets the JAVA_HOME for all users as /etc/environment is a system-wide configuration file. Other users will need to either execute the source command above (source /etc/environment) or log out and log in to see the changes.

Uninstalling Java

If you decide to uninstall the Java packages for any reason, you can do it just like any other package installed with apt package manager.

For example, you should execute the following commands to completely remove the default OpenJDK:

$ sudo apt remove default-jdk
$ sudo apt autoremove

For OpenJDK 8 uninstallation, the following commands should work:

$ sudo apt remove openjdk-8-jdk
$ sudo apt autoremove

Conclusion

That's all folks for how to install Java on Ubuntu 18.04 machine. In this tutorial, you learned to install and manage different Java versions on your Ubuntu server.

You can use the same instructions for Ubuntu 16.04 and other Ubuntu-based distributions like Kubuntu, Linux Mint and Elementary OS.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.