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.
In this article, you will learn how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using the default apt package manager on Ubuntu 20.04 and Ubuntu 20.10.
Install Default Java JRE on Ubunu 20.04
As always, first, update and upgrade your APT through the following command.
$ sudo apt update
Next, check for Java installation on the system. By default, Ubuntu 20.04 includes the Java 11 JDK. If Java is not installed on your system then use the following command to install the Java JDK 11.
$ sudo apt install default-jre
[ads1]
Now, you can check the installed version using the following command:
$ java -version
You will get the following output:
java -version openjdk version "11.0.10" 2021-01-19 OpenJDK Runtime Environment (build 11.0.10+9-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.10+9-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
Installing the Default JDK in Ubuntu
Once JRE installed, you might also need the JDK (Java Development Kit) in order to compile and run a Java-based application. To install the JDK, run the following command.
$ sudo apt install default-jdk
After installation, verify the JDK installation by checking the version as shown.
$ javac -version javac 11.0.10
Setting JAVA_HOME Environment Variable
[ads]
The JAVA_HOME
environment variable is used by some Java applications to determine the Java installation location.
To set the JAVA_HOME
environment variable, first, discover where Java is installed by running the following command:
$ readlink -f /usr/bin/java
You will get the following output:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
Then open /etc/environment file using nano text editor:
$ sudo nano /etc/environment
Add the following line at the end of the file, make sure to replace the location of your Java installation path.
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Save the file and reload the file to apply the changes to your current session:
$ source /etc/environment
Verify that the environment variable is set:
$ echo $JAVA_HOME
You will get the following output:
/usr/lib/jvm/java-11-openjdk-amd64
Uninstalling Java
[ads1]
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
Conclusion
In the above guide, you learned how to install Java on Ubuntu 20.04.
If you have any questions, feel free to leave a comment.