How to install Java on Ubuntu 20.04
In this guide, we will describe how to install Java on Ubuntu 20.04.
Java is one of the most popular programming languages used to build different kinds of applications and systems. Java runs on all major operating systems and devices. You can find applications developed in Java on your laptop, phone, and game console.
Before You Begin
There are multiple different implementations of Java. OpenJDK and Oracle Java are the two main implementations of Java, with almost no differences between them except that Oracle Java has a few additional commercial features. Oracle Java License permits only non-commercial use of the software, such as personal use and development use.
The default Ubuntu 20.04 repositories include two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). The JRE consists of the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. The JDK includes the JRE and development/debugging tools and libraries necessary to build Java applications.
If you are not sure which Java package to install, the general recommendation is to install to the default OpenJDK (JDK 11) version. Some Java-based applications may require a specific version of Java, so you should consult the application documentation.
Installing OpenJDK 11
At the time of writing, Java 11 is the latest long-term supported (LTS) version of Java. It is also the default Java development and runtime in Ubuntu 20.04.
Run the following commands as a user with sudo privileges or root to update the packages index and install the OpenJDK 11 JDK package:
sudo apt updatesudo apt install openjdk-11-jdk
Once the installation is complete, you can verify it by checking the Java version:
java -version
The output should look something like this:
openjdk version "11.0.7" 2020-11-10
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)
That’s it! At this point, you have successfully installed Java on your Ubuntu system.
JRE is included in the JDK package. If you need only JRE, install the openjdk-11-jre
package. For minimal Java runtime, install the openjdk-11-jdk-headless
package.
Installing OpenJDK 8
Java 8, the previous Java LTS version, is still widely used. If your application runs on Java 8, you can install it by typing the following commands:
sudo apt updatesudo apt install openjdk-8-jdk
Verify the installation by checking the Java version:
java -version
The output should look something 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)
Setting the Default Version
If you have multiple Java versions installed on your Ubuntu system you can check which version is set as the default one by typing:
java -version
To change the default version, use the update-alternatives
command:
sudo update-alternatives --config java
The output will look something like below:
Output :
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 modePress <enter> to keep the current choice[*], or type selection number:
You will be presented with a list of all installed Java versions. Enter the number of the version you want to be used as the default and press Enter
.
Java_
Home Environment Variable
The JAVA_HOME
environment variable is used by some Java applications to determine the Java installation location.
To set the JAVA_HOME
variable, first find the Java installation path with update-alternatives
:
sudo update-alternatives --config java
In this example, the installation paths are as follows:
- OpenJDK 11 is located at
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
- OpenJDK 8 is located at
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Once you found the path of your preferred Java installation, open the /etc/environment
file:
sudo nano /etc/environment
Assuming you want to set JAVA_HOME
to point to OpenJDK 11, add the following line, at the end of the file:
/etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Copy
For changes to take effect on your current shell you can either log out and log in or run the following source
command:
source /etc/environment
Verify that the JAVA_HOME
environment variable was correctly set:
echo $JAVA_HOME
You should see the path to the Java installation:
/usr/lib/jvm/java-11-openjdk-amd64
/etc/environment
is a system-wide configuration file, which is used by all users. If you want to set the JAVA_HOME
variable on a per-user basis, then add the line to the .bashrc
or any other configuration file which is loaded when the user logs in.
Uninstalling Java
You can uninstall Java like any other package installed with apt
.
For example, to uninstall the default-jdk
package enter:
sudo apt remove openjdk-11-jdk
If you have any questions, feel free to leave a comment.