How-to

How to Install Java OpenJDK on Ubuntu 20.04

What is Java?

Java is one of the most popular programming languages developed by Sun Microsystems. It allows you to build different types of applications for different systems.

There are two different Java implementation, the Oracle Java and the OpenJDK. The Oracle Java has additional and commercial features, while the OpenJDK is an open-source implementation of Java Platform.

What we will do?

In this tutorial, you will learn how to install Java on the Ubuntu 20.04 System. You will learn how to install multiple Java versions (Java OpenJDK 11 LTS and Java OpenJDK 14 Non-LTS) on the Ubuntu system, setup the default Java version for your application, and set up the ‘JAVA_HOME’ environment variable for a specific user or system-wide.

Prerequisite

For this tutorial, make sure you have got an Ubuntu 20.04 server with root privileges.

Below is the tutorial about the Ubuntu 20.04 server installation.

Install Ubuntu 20.04 Server

Before going any further, log in to your Ubuntu server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Install Java on Ubuntu 20.04

First, you will install Java OpenJDK 11 and 14 to the Ubuntu system. It’s available by default on the official Ubuntu repository.

Find all available Java OpenJDK packages using the following command.

apt search openjdk

As can be seen, there are 4 Java OpenJDK versions available on the Ubuntu 20.04 Focal Fossa repository, including the Java OpenJDK 8, 11 as the LTS (Long Term Support) version, and the OpenJDK 13 and 14 as the Latest version Non-LTS.

And for this guide, we will install the OpenJDK 11 as the latest LTS version, and the OpenJDK 14 as the latest version of Java.

– Install Java OpenJDK LTS Version

It’s highly recommend to install and use the Java OpenJDK LTS version on the production environment.

To install the Java OpenJDK 11, run the apt command below.

apt install openjdk-11-jdk openjdk-11-jdk-headless

For the Java OpenJDK 8, run the following command.

apt install openjdk-8-jdk openjdk-8-jdk-headless

Once the installation is complete, check your java version using the following command.

java -version

Now you will get the response as below.

Install Java OpenJDK 11

As a result, you’ve installed Java OpenJDK 11 to the Ubuntu 20.04 System.

– Install Java OpenJDK Non-LTS Version

The Ubuntu 20.04 Focal Fossa provided 2 latest versions Java Non-LTS, the Java OpenJDK 13 and 14.

To install the Java OpenJDK 14, run the command below.

apt install openjdk-14-jdk openjdk-14-jdk-headless

For the Java OpenJDK 13, use the following command.

apt install openjdk-13-jdk openjdk-13-jdk-headless

Now you can check the Java version using the following command.

java -version

below is the result you will get.

Install Java OpenJDK 14

As a result, you’ve installed the Java OpenJDK 11 and 14 to the Ubuntu 20.04 System, and you’ve two versions on Java OpenJDK on your system.

Step 2 – Setup Default Java Version

After installing two Java versions on the Ubuntu system, you will set up the default Java version for your application. You will setup the default Java OpenJDK and JRE version for your application, on the Ubuntu system.

First, check all available Java version on the Ubuntu system using the command below.

update-java-alternatives --list

Below is the response you will be shown.

java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.14.0-openjdk-amd64      1411       /usr/lib/jvm/java-1.14.0-openjdk-amd64

As can be seen, you’ve two Java versions on your system. The first column is the name of the Java installation, the second column is the priority number, and the third column is the path of the Java installation directory.

Below is the detailed result you’ve got.

  • The Java OpenJDK 11 has comes with the name ‘java-1.11.0-openjdk-amd64’ and the priority number ‘1111’. It’s installed on the ‘/usr/lib/jvm/java-1.11.0-openjdk-amd64’ directory.
  • The Java OpenJDK 14 has comes with the name ‘java-1.14.0-openjdk-amd64’ and the priority number ‘1411’. It’s installed on the ‘/usr/lib/jvm/java-1.14.0-openjdk-amd64’ directory.

And to set up the default Java version on the Ubuntu system, you need the ‘name’ of Java installation.

– Setup Java OpenJDK 11 as Default

To configure the Java OpenJDK 11 as default, run the following command.

update-java-alternatives --set java-1.11.0-openjdk-amd64

After that, check the java, javac, and jar command as below.

java -version
javac -version
jar --version

As can be seen, the default Java version is now completely changed to Java OpenJDK 11.

Setup Java OpenJDK 11 as default

And you’ve successfully changed the default Java version on the Ubuntu system.

– Setup Java OpenJDK 14 as Default

To configure the Java OpenJDK 14 as default, run the following command.

update-java-alternatives --set java-1.14.0-openjdk-amd64

After that, check the java and javac command as below.

java -version
javac -version
jar --version

As a result, the default Java version has been changed to Java OpenJDK 14, changed completely.

Setup Java OpenJDK 14 as default

And you’ve learned how set up the default Java version on Ubuntu 20.04 System.

Step 3 – Setup JAVA_HOME Environment Variable

The ‘JAVA_HOME‘ is an environment variable that will be used by Java applications to determine which Java installation directory and Java version will be used by the application.

In this step, you will set up the JAVA_HOME environment variable for the specific user through the ‘~/.bashrc‘ file, and set up it system-wide by creating a new script on the ‘/etc/profile.d‘ directory.

– Setup JAVA_HOME for Specific User

For the specific user, log in to your user and edit the ‘~/.bashrc‘ file.

su - user
vim ~/.bashrc

Change the Java path installation and paste the following configuration into it.

# For Java 11
export JAVA_HOME="/usr/lib/jvm/java-1.11.0-openjdk-amd64"

Save and close.

Now reload the ‘~/.bashrc‘ configure to apply new configuration.

source ~/.bashrc

Check it using the following command.

echo $JAVA_HOME

As a result, you’ve set up the ‘JAVA_HOME’ for the specific user.

Configure JAVA_HOME for specific user

– Setup JAVA_HOME for System-Wide

This set up will affect every user on the Ubuntu system.

Go to the ‘/etc/profile.d/‘ directory and create a new bash script ‘java.sh‘.

cd /etc/profile.d/
vim java.sh

Change the Java path installation and paste the following configuration into it.

# For Java 11
export JAVA_HOME="/usr/lib/jvm/java-1.11.0-openjdk-amd64"

Save and close.

Next, make the script executable and load it to the system.

chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh

After that, check the ‘JAVA_HOME‘ variable using the command below.

echo $JAVA_HOME

And you will get the result as below.

Setup JAVA_HOME environment variable system wide

Testing JAVA_HOME environment variable system wide

As can be seen, you’ve set up the ‘JAVA_HOME‘ environment variable on the Ubuntu system.

As a result, you’ve successfully installed Java on the Ubuntu 20.04 System.

Write A Comment