How to install Java with `apt` on Ubuntu 18.04

[*]Introduction

Java and JVM (Java Virtual Machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra, and Jenkins.

In this tutorial, you will install various versions of the Java Runtime Environment (JRE) and Java Developer Kit (JDK) using apt. You will also be installing OpenJDK, and the official packages from Oracle. Then you select the version you want to use for your projects. When you’re done, you will be able to use the JDK to develop software, or use the Java Runtime to run software.

Prerequisites

To complete this article, you will need:

Installing the JRE / JDK by default

The easiest option to install Java is to use the version bundled with Ubuntu. By default, Ubuntu 18.04 includes the Open JDK, which is an open source variant of the JRE and JDK.

This package will install either OpenJDK 10 or 11.

  • Until September 2018, this will install OpenJDK 10.
  • After September 2018, will install OpenJDK 11.

Before installing this version, first update the package index:

sudo apt update

Then check which version Java is already installed:

java -version

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

Output

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

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

Run the following command to install OpenJDK:

sudo apt install default-jre

This command will install Java Runtime Environment (JRE). This will allow you to run almost all Java software.

Check the installation:

java -version

You will see the following output:

Output

openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

You may need a Java Development Kit (JDK) in addition to the JRE in order to build and run some custom Java-based software. To install the JDK, run the following command, which will also install the JRE:

sudo apt install default-jdk

Make sure JDK is installed by checking javac version, Java compiler:

javac -version

You will see the following output:

Output

javac 10.0.1

Next, let’s take a look at the directions for which versions of OpenJDK we want to install.

Installing specific versions of OpenJDK

While you can install the default OpenJDK package, you can also install different versions of OpenJDK.

OpenJDK 8

Java 8 is the current long term support release and is still widely supported, although public service ends in January 2019. To install OpenJDK 8, run the following command:

sudo apt install openjdk-8-jdk

Make sure everything is installed:

java -version

You will see the result like this:

Output

openjdk version "1.8.0_162"
OpenJDK Runtime Environment (build 1.8.0_162-8u162-b12-1-b12)
OpenJDK 64-Bit Server VM (build 25.162-b12, mixed mode)

Alternatively, it is possible to install only the JRE by doing the following

sudo apt install openjdk-8-jre

OpenJDK 10/11

The Ubuntu repository contains a package that will install either Java 10 or 11. By September 2018, OpenJDK 10 will be installed. After Java 11 is released, Java 11 will be installed.

To install OpenJDK 10/11, run the following command:

sudo apt install openjdk-11-jdk

To install only the JRE, use the following command:

sudo apt install openjdk-11-jre

Next, let’s take a look at how to install the official Oracle JDK and JRE.

Installing Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you need to add a new package repository for the version you would like to use.

To install Java 8, which is the latest LTS version, first add your package repository:

sudo add-apt-repository ppa:webupd8team/java

When added to the repository, you will see a message like this:

Output

 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Jav
a files in this PPA.

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracl
e-java-7-and-6-installers-no.html

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/j
dk9-downloads-3848520.html

The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04.

More info (and Ubuntu installation instructions):
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html

More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press ENTER to continue. Then update the package list:

sudo apt update

After updating the package list, install Java 8:

sudo apt install oracle-java8-installer

Your system will download the JDK from Oracle and ask you to accept the license agreement. Accept the agreement and install the JDK.

Now let’s take a look at how to choose which version of Java you want to use.

Java Management

You can have multiple Java installations on the same server. You can configure the default version for command line use with the update-alternatives command.

sudo update-alternatives --config java

This is what the output would look like if you installed all Java versions in this article:

Output

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

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

Select the number associated with the Java version to use as the default, or press ENTER to leave the current settings in place.

You can do this for other Java commands like the compiler (javac):

sudo update-alternatives --config javac

Other commands for which this command can be run include, but are not limited to: keytool, javadoc, and jarsigner.

Setting the JAVA_HOME environment variable

Many programs written using Java use the JAVA_HOME environment variable to determine where Java is installed.

To set this environment variable, first determine where Java is installed. Use the update-alternatives command:

sudo update-alternatives --config java

This command shows each Java installation along with its installation path:

Output

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

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

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

In this case, the installation path is as follows:

  1. OpenJDK 11 is located in / usr / lib / jvm / java-11-openjdk-amd64 / bin / java.
  2. OpenJDK 8 is located in / usr / lib / jvm / java-8-openjdk-amd64 / jre / bin / java.
  3. Oracle Java 8 is located in / usr / lib / jvm / java-8-oracle / jre / bin / java.

Copy the path from the privileged installation. Then open / etc / environment using nano or your favorite text editor:

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own:

/ etc / environment

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

Changing this file will set the JAVA_HOME path for all users on the system.

Save the file and exit the editor.

Now download this file to apply the changes to the current session:

source /etc/environment

Make sure to output the environment variable:

echo $JAVA_HOME

You will see the path you set:

Output

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

Other users will need to run source / etc / environment or log out and log back in to apply this setting.

Output

In this tutorial, you have installed multiple versions of Java and learned how to manage them. You can now install software that runs in Java such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.

Sidebar