In this guide, we will describe how to install Java on Ubuntu 20.04.
Java is one of the most popular programming languages in the world, and is used to build various applications and systems. Java runs on all major operating systems and devices. You can find applications developed with Java in your laptop, phone and game.
There are many different Java implementations. OpenJDK and Oracle Java are the two most important Java implementations. They are basically the same except that Oracle Java has very few additional features. The Oracle Java license is only allowed for use as non-commercial software, such as personal use and development use.
The default Ubuntu 20.04 source repository contains two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE mainly includes Java Virtual Machine (JVM), classes and binary packages that allow you to run Java programs. JDK contains JRE and development/debugging tools and library files for building Java applications.
If you are not sure which version of Java to install, we usually recommend installing the OpenJDK (JDK 11) version. Some Java-based applications may need to run under a specified Java version, you should consult the application documentation.
At the time of writing, Java 11 is a long-term support version (LTS) of Java. It is also the default Java development and runtime environment for Ubuntu 20.04.
Run the following command as root or another sudo user to update the package index and install the OpenJDK 11 JDK package:
sudo apt update
sudo apt install openjdk-11-jdk
Once the installation is complete, you can verify it by checking the Java version:
java -version
The output is similar to the following:
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)
that's it! At this point, you have successfully installed Java on your Ubuntu system.
JRE is included in the JDK package. If you only need JRE, install the openjdk-11-jre
package. Minimal Java runtime environment, install openjdk-11-jdk-headless
package.
Java 8, the previous version of Java LTS, is still widely used. If your application is running on Java 8, you can install it by typing the following command:
sudo apt update
sudo apt install openjdk-8-jdk
Verify the installation process 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)
If you have installed multiple versions of Java on your Ubuntu system, you can enter the following command to check which version is set as the default:
java -version
To modify the default version, use the update-alternatives
command:
sudo update-alternatives --config java
The output looks like this:
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:
All installed Java versions will be listed. Enter the serial number you want to set as the default value, and press "Enter".
In some Java applications, the environment variable JAVA_HOME
is used to indicate the location of the Java installation.
To set the JAVA_HOME
variable, first use update-alternatives
to find the Java installation path:
sudo update-alternatives --config java
In this example, the installation path is as follows:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Once you find your preferred Java installation path, open the /etc/environment
file:
sudo nano /etc/environment
Suppose you want to set JAVA_HOME
to specify OpenJDK 11. At the end of the file, add the following line:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
To make the changes take effect in the current shell, you can log out and log in to the system again, or run the following command:
source /etc/environment
Verify that the JAVA_HOME
environment variable is set correctly:
echo $JAVA_HOME
You should see the Java installation path:
/usr/lib/jvm/java-11-openjdk-amd64
You can uninstall Java using apt, just like uninstalling any software package.
For example, to uninstall the default-jdk
package, enter:
sudo apt remove openjdk-11-jdk
Both OpenJDK 11 and OpenJDK 8 are in the default Ubuntu 20.04 software source repository and can be installed using the apt
package management tool.
Recommended Posts