Easy Steps to Install Java on Ubuntu 20.04

Java is one of the ubiquitous programming languages that exit in software programming today and is also quite relevant to many other frameworks that depend on it like the Android programming framework. It was created to have the least dependencies possible and runs in something called a virtual machine that enables it to run on a variety of device platforms without the need to check for compatibility. Java is used to build software that can be anything from a desktop program to a web-based application to a distributed system. With this guide we show you how to install Java on Ubuntu.

Ubuntu is a Linux distribution that enjoys one of the largest user-base online It’s easy to set up, learn and use. In fact, if you have a VPS or Dedicate server you’ll be able to choose your Ubuntu operating system. We will focus on Java on Ubuntu 20.04. These steps will work on any system that runs the latest version of Ubuntu and also the LTS stream of Ubuntu. We will be installing Java SE 11 LTS.

What is JDK and JRE?

Anyone who wants to install Java on a machine is faced with the dilemma of figuring out what is needed to be installed, JDK or JRE. JDK stands for the Java Development Kit, which is a superset of the JRE. JRE stands for Java Runtime Environment. Java programs are run inside virtual machines in the Java Virtual Machine and that can be installed with the JRE.

If your purpose is to just run existing Java programs then the JRE is sufficient. The JDK is a Software Development Kit (SDK) that gives you everything that is required to build Java Applications on your machine and is what you need to install if you are going to be developing them.

Install using the Default Packet Manager

The first option you have to install is by using the default packet manager on Linux. This is by using the command “apt-get” .
Before you use that, first update the current packages to the latest version using the following command:

sudo apt-get update && apt-get upgrade

The ‘sudo’ keyword will prompt you for your root access password and is needed for elevated permissions to install programs on a Linux machine. Once that is completed type and run the following command to acquire and install the Java Runtime Environment.

sudo apt-get install default-jre

If you are planning on using the machine for development, you should look to install the Java Development Kit (JDK) instead. If you are planning to compile Java programs you will need this. The JDK includes the JRE, so its no problem if you choose to install the JDK instead. Use the following command to install the JDK

sudo apt-get install default-jdk

Now you can use Java on your Ubuntu system. Check if the installation works properly installed by using the following commands.

java -version
javac -version

The output will look something like this.

root@vps42681194:~# java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu120.04.1, mixed mode, sharing)
root@vps42681194:~# javac -version
javac 11.0.6

Install Java using the Oracle JDK

Another method of installing Java on Ubuntu is by using the Oracle Development kit or Oracle JDK. We first should update Ubuntu’s current packages using the following command.

sudo apt-get update && apt-get upgrade

Next, we will also use a third-party library managed by WebUpd8 . However, we should first install the following package using the following command.

sudo apt-get install software-properties-common

Now we get the Java Uprising PPA with the following command.

sudo add-apt-repository ppa:linuxuprising/java

Use this time to run the update again.

sudo apt-get update && apt-get upgrade

We can now install Java on our Ubuntu system with the following command and then following the on-screen instructions to install it.

apt update; apt-get install oracle-java14-installer

This command installs Java version 14. You can also choose to install another version by replacing the java14 with java13 or java11 if so needed.

Check for proper Java installation with the following commands:

java -version
javac -version

The output will look something like this.

root@vps42681194:~# java -version
java version "14" 2020-03-17
Java(TM) SE Runtime Environment (build 14+36-1461)
Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)
root@vps42681194:~# javac -version
javac 14

Multiple Versions

A single server can have multiple versions of Java installed. You can choose which is the default version by using the following command:

update-alternatives --config java

The following output will appear where you can choose which version of Java must be the default one by entering a number signifying your selection.

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-14-oracle/bin/java 1091 manual mode

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

Configuring the Java Environment variables

After installing and choosing the desired Java version, you should setup your environment variables if you are planning on developing or compiling Java programs on your machine. The variable in question is the JAVA_HOME variable. Most applications will need to know the location of your Java installation and this is the method they use to find it. The previous command can help you identify the location of your Java installation:

update-alternatives --config java

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-14-oracle/bin/java 1091 manual mode

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

You should copy the installation path and the edit the environment file that is located in the etc directory. Use the following command to access that.

nano /etc/environment

Append the following text to the end of the JAVA_HOME environment variable.

JAVA_HOME="/usr/lib/jvm/java-14-oracle/bin/java"

Then finish the edit and save the file by using CTRL+X. Apply the changes by typing and executing the following command.

source /etc/environment

Confirm that it is working by entering the following command.

echo $JAVA_HOME