How to Install Java on Ubuntu 20.04

how to install java on ubuntu 20.04

Let’s talk about how to install Java on Ubuntu 20.04. What is Java? Java is a free, open-source, and most popular language used for developing mobile and desktop applications, and it can run on all major operating systems. There are different implementations of Java, including JRE, OpenJDK, and Oracle JDK. Let’s understand the difference between JRE, OpenJDK, and Oracle JDK.

Oracle JDK: It is the official Oracle version of the Java Development Kit. It has a few additional commercial features and permits only non-commercial use of the software.
JRE: Java Runtime Environment consists of the Java virtual machine and binaries used to run Java programs.
OpenJDK: Java Development Kit includes the JRE and development tools and libraries required to build Java applications.

This tutorial will explain how to install Java 8, Java 11, and Oracle Java 15 on Ubuntu hosting.

Prerequisites

  • An Ubuntu 20.04 VPS (we’ll be using our NVMe 2 VPS plan)
  • Access to the root user account (or access to an admin account with root privileges)

Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number
install oracle java ubuntu 20.04

You will need to replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

apt-get update -y
apt-get upgrade -y

Install Java 8

Java 8 is the previous and widely used Java LTS version. If your application built on Java 8, you can install it with the following command:

apt-get install openjdk-8-jdk -y

Once installed, verify the Java version using the following command:

java -version

You should get the following output:

openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)

Install Java 11

Java 11 is the latest LTS version of Java. BY default, it ships in the Ubuntu 20.04 default repository. You can install it with the following command:

apt-get install openjdk-11-jdk -y

Once Java 11 is installed, you can verify it with the following command:

java -version

You should get the following output:

openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Install Oracle Java 15

By default, Oracle Java is not available in the Ubuntu 20.04 default repository. You will need to add the third-party repository in your system in order to install Oracle Java.

First, install the required dependencies with the following command:

apt-get install gnupg2 software-properties-common -y

Once all the dependencies are installed, add the Java repository with the following command:

add-apt-repository ppa:linuxuprising/java

When the repository is added, you can install it with the following command:

apt-get install oracle-java15-installer -y

Once the installation has been finished, you can verify it with the following command:

java -version

You should get the following output:

java version "15.0.1" 2020-10-20
Java(TM) SE Runtime Environment (build 15.0.1+9-18)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

Switch Between Different Java Versions

If you have multiple versions of Java installed in your system, you can easily set the default version using the update-alternatives command.

To set the Java default version from Oracle Java 15 to Java 8, run the following command:

update-alternatives --config java

You should see a list of all installed Java versions in the following 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      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
* 2            /usr/lib/jvm/java-15-oracle/bin/java             1091      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press  to keep the current choice[*], or type selection number: 3

Type 3 and hit Enter to set Java 8 as the default Java version. You should get the following output:

update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode

Set JAVA_HOME Environment Variable

You will need to set JAVA_HOME environment variable to define the Java path in some Java applications.

First, find the Java installation path with the following command:

update-alternatives --config java

You should see the path of all Java versions in the following 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      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
* 2            /usr/lib/jvm/java-15-oracle/bin/java             1091      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Next, copy your desired Java version path from the above output and paste it in the /etc/environment file:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
nano /etc/environment

Add the following line for Java 8:

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

Next, activate the Java environment variable with the following command:

source /etc/environment

You can now verify the JAVA_HOME environment variable with the following command:

echo $JAVA_HOME

You should get the following output:

/usr/lib/jvm/java-8-openjdk-amd64

In the above guide, you learned how to install Java on Ubuntu 20.04 server.

installing java on ubuntu 20.04

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can ask our expert Linux admins to set it up for you. They are available 24/7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

1 thought on “How to Install Java on Ubuntu 20.04”

Leave a Comment