Google Cloud Compute Engine

How to Install Java on Google Cloud Ubuntu 18.04 LTS

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

How to Install Java on Google Cloud Ubuntu 18.04 LTS. In this guide you are going to learn how to install Java Runtime Environment (JRE), Java Development Kit (JDK) and Open JDK.


A running Compute Engine, see the Setting up Compute Engine Instance.

Install JRE/JDK

You can install Open JDK which is packaged with Ubuntu 18.04 LTS by default.

sudo apt update
sudo apt install default-jre

Once Java Runtime Environment is installed you can check the Java version with the command as follows.

java -version

You will see the output resembling this.

openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)

Next you can install the default JDK to compile and run some specific software packages.

sudo apt install default-jdk

Once Java Development Kit is installed you can check the version with the javac compiler command as follows.

javac -version

You will get the version of JDK installed currently.

Install Specific Java versions

Java 11 is the current version with Long Term support until 2023 and extended support until 2026.

Ubuntu repositories will have either Java 10 or 11. Once Java 11 is available by default, it will install Java 11.

How to Install OpenJDK 8

Java 8 is also a Long Term supported version which has support until 2022 and widely supported. To install OpenJDK 8, execute the following command.

sudo apt install openjdk-8-jdk

Verify the installed version of Java.

java -version

Managing Java versions

You can configure which Java version to use as default.

sudo update-alternatives --config java

This is the output with all versions installed in your instance.

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 
Press <enter> to keep the current choice[*], or type selection number: 

Select the number associated with the Java version you need to use and hit Enter

Set up Java Environment Variable

Use the same update-alternatives command to get the installation path of your default Java version.

sudo update-alternatives --config java

Now we have 2 Java versions installed, their paths are

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

Copy the installation path of your default version and add it in the JAVA_HOME environment variable.

sudo nano /etc/environment

At the end of this file, add the following line with your installation path. To use the official Java 8 by Oracle the variable will be as follows.


Hit Ctrl+X followed by Y and Enter to save and exit the nano editor.

Now JAVA_HOME environment variable is set and available for all users.

Reload to apply changes.

source /etc/environment

To verify the environment variable of Java


You will get the installation path you just set.

Now Java is successfully installed and you can install all softwares which runs on Java like Elasticsearch, Jenkins, etc

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.