There are a lot of Java applications that you may come across and use in your daily work. That’s why you need JVM (Java’s virtual machine) as the run-time environment to run Java applications.
This tutorial will show you how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04 needed by many Java-based applications.
Installing the Default JRE in Ubuntu
We will install OpenJDK 11, which is an open-source alternative to the JRE and JDK.
First, we update the system with:
$ sudo apt update && sudo apt upgrade
Now we will check if the Java Run-time Environment is installed on the system:
$ java -version
If Java is not installed on your system you will get the command not found error:
Installing Java Runtime Environment (JRE)
Now we will install OpenJDK 11, which will provide Java Runtime Environment (JRE). Type the following command to install the packages:
$ sudo apt install default-jre
Type ‘Y’ to continue.
After the packages are installed, type the java -version command again to verify the operation was successful:
$ java -version
Installing JDK (Java Development Kit)
After we have successfully installed Java Run-time Environment, we can now install Java Development Kit (JDK).
Type the following command to install it:
$ sudo apt install default-jdk
We can now verify the Java Development Kit installation by typing the following:
$ javac -version
If everything went ok, we should get the installed JDK version:
Next, we need to set up the JAVA_HOME environment variable.
Setting the JAVA_HOME Environment Variable
The JAVA_HOME environment variable is used to discover the Java installation directory by Java-based applications.
First, we must check in which directory the Java is installed:
$ readlink -f /usr/bin/java
Now open /etc/environment file using your favorite text editor:
$ sudo nano /etc/environment
Now add the following line to the end of the file, after replacing the path with the installation directory path on your system that you’ve got by running the previous command readlink, but omit /bin/java at the end:
Save the file, then reload the configuration to your current session:
$ source /etc/environment
Now verify the environment path is updated by typing the following command:
$ echo $JAVA_HOME
You should see the java path in the terminal output like in the example above.
In this tutorial, we have learned how to install Java Run-time Environment (JRE) and the Java Developer Kit (JDK) on Ubuntu 20.04 and the same steps are applicable on other Ubuntu versions as well.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.