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.
Prerequisites
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
- 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 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.
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"
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
echo $JAVA_HOME
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