Java is one of the most popular programming languages used to build various types of applications and systems. Java runs on all operating systems and devices. We can find applications developed in Java on your laptop, cell phone and game console.
In this guide, we will learn how to install Java on Ubuntu 20.04.
Before starting
There are several different Java implementations. OpenJDK and Oracle Java are the two main implementations of Java, with almost no difference between them except that Oracle Java has some additional commercial features. The license Oracle Java only permits non-commercial use of the software, such as personal use and development use.
The Ubuntu 20.04 default repository includes two OpenJDK packages, the Java Runtime Environment (JRE) and the Java Development Kit (JDK). A JRE consists of a Java virtual machine (JVM), classes, and binaries that allow running Java programs. The JDK includes the JRE and the development / debugging tools and libraries needed to build Java applications.
If you are not sure which Java package to install, a general recommendation is to install the default version of Java, which is the OpenJDK version (JDK 11). Some Java-based applications may require a specific version of Java, so as a user, it is mandatory to read the application documentation.
Install Java OpenJDK 11 di Ubuntu 20.04
At the time of writing, Java 11 is the long-term supported (LTS) version of Java. It is also the default Java development and runtime on Ubuntu 20.04.
Run the following command as a user with sudo or root privileges to update the packages index and install the OpenJDK 11 package:
sudo apt update
sudo apt install openjdk-11-jdk
The JRE is included in the JDK package. If you only need the JRE, install the package openjdk-11-jre
. For minimal Java runtime, install the package openjdk-11-jdk-headless
.
After the installation is complete, verify it by checking the Java version:
java -version
The output will look like this:
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)
At this point, the Java installation on the Ubuntu system was successful.
Install Java OpenJDK 8 di Ubuntu 20.04
Java 8, the previous version of Java for LTS, is still widely used. If your application runs on Java 8, you can install it by typing the following command:
sudo apt update
sudo apt install openjdk-8-jdk
Verify the installation by checking the Java version:
java -version
The output will look like this:
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1ubuntu1-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
Set Default Java Version on Ubuntu 20.04
If you have multiple versions of Java installed on the system, To check the default Java version you can use the following command:
java -version
To be able to change the standard version, use the tool update-alternatives
as shown below:
sudo update-alternatives --config java
The output will look like the following
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-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:
You will be presented with a list of all installed Java versions. Enter the version number you want to use as default and press Enter
.
Set JAVA_HOME Environment Variable
Some applications written in Java use environment variables JAVA_HOME
to specify the Java installation location.
To set the JAVA_HOME environment variable, use the command update-alternatives
to find where Java is installed:
sudo update-alternatives --config java
In our case, the installation path will be as follows:
- OpenJDK 11 is situated on site
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
- OpenJDK 8 is situated on site
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Copy the installation path of your preferred installation. Next, open the file /etc/environment
:
sudo nano /etc/environment
Add the following line, at the end of the file:
/etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Make sure to replace the path with the path to the Java version of your choice.
You can either log out and then log back in, or run the source command following to apply changes to the current session:
source /etc/environment
To verify that the environment variable JAVA_HOME
properly set, run the following echo command:
echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64
/etc/environment
is the system configuration file, which is used by all users. If you want to set a variable JAVA_HOME
on a per user basis, add a line to .bashrc
or any other configuration file that loads when the user logs in.
How to Uninstall Java on Ubuntu 20.04
If for some reason you want to uninstall a Java package, you can uninstall it just like any other package it is installed with apt
.
For example, if you want to delete a package default-jdk
just run the command:
sudo apt remove openjdk-11-jdk
Conclusion
OpenJDK 11 and OpenJDK 8 are available in the default Ubuntu 20.04 repositories and can be installed using apt
package manager.