Java is a widely used programming language that has become an essential tool for developing and running applications on various platforms. If you are a developer or a system administrator using Ubuntu 20.04, you may need to install Java on your machine to run Java-based applications or develop Java programs. Fortunately, Ubuntu 20.04 provides an easy and convenient way to install Java using the Apt-Get package manager. In this article, we will walk you through the step-by-step process of installing Java on Ubuntu 20.04 using Apt-Get, so you can get started with your Java projects right away. The process is the same for Ubuntu 18.04 and Ubuntu 22.04.
Installation of the Default JRE/JDK
If you are planning to use the current version of Java, which at the time of this writing is Openjdk 11.0.18 you can use the following two commands to either install the default JRE which stands for Java Runtime Envoirement or the JDK which stands for Java Developement Kit and is usually needed to compile Java applications.
For the default JRE the command is:
apt-get install default-jre
And for the default JDK the command is:
apt-get install default-jdk
Pro-Tip: The JDK always contains the JRE, so you do nothing wrong when always going with the JDK apart from its larger file size.
Installing other Java Versions
It may happen that you need older versions of Java for your project. Because the commands shown above only install the latest version of either the JDK or the JRE, you need to specifically install the older versions. But no worries! We got you covered!
Installing Java 8 (JDK & JRE)
To install Java 8 JDK use this command:
apt-get install openjdk-8-jdk –y
And to install Java 8 JRE use this command:
apt-get install openjdk-8-jre –y
Installing Java 16 (JDK & JRE)
Installing the Java 16 JDK or JRE are again very easy and can be done with one single command.
For Java 16 JDK this command would be:
apt-get install openjdk-16-jdk –y
And for Java 16 JRE it would be:
apt-get install openjdk-16-jre –y
Installing Java 17 (JDK & JRE)
Ok… now it’s getting repetitive but you may have guessed it: Java 17 is installed in the same way, but with “17” as the version number.
So to install Java 17 JDK use this command:
apt-get install openjdk-17-jdk –y
And for Java 17 JRE use this command:
apt-get install openjdk-17-jre –y
Switching between different Java Versions
It is possible to have several Java versions installed at the same time, but only one Java version is selected or active at a time. This is especially important when you want to set up your own Minecraft Server and you are often switching between Minecraft versions. If you want to learn more about how to run your own Minecraft Server, read our beginner-friendly Minecraft-Server guide here.
You can find out which version is currently active with this command:
As you can see, although I’ve installed multiple versions of Java, Java 17 is currently active. To switch between the active versions use this command:
sudo update-alternatives --config java
If you want to switch to a different version of java, just type in the “Selection”-Number of the corresponding version of Java (e.g. “4” for Java 8) and hit enter.
How to set the “JAVA_HOME” environment Variable
The JAVA_HOME variable is an environment variable that points to the root directory of the Java installation on your system. It is used by Java-based applications to locate the Java installation and execute Java commands.
Setting the JAVA_HOME variable is important for development environments or when running applications that require a specific Java version. For example, if you have multiple Java versions installed on your system, setting the JAVA_HOME variable to the desired version can ensure that the correct Java version is used when running an application.
To set the JAVA_HOME variable, follow these steps:
Run this command to get an overview of every installed version and their path:
sudo update-alternatives --config java
Here you can see every installed Java version and the corresponding path, e.g. the Path for the Java 17 JDK is “/usr/lib/jvm/java-17-openjdk-amd64/bin/java”
If you don’t want to change anything here and just wanted to find the path to copy it, press [Enter] to close the menu again.
Now open the file, which will contain the variable with this command:
sudo nano /etc/environment
And add the following line:
(Of course, replace “YOUR PATH” with the actual path of the java version)
Save the file with [CTRL]+O and close the editor with [CTRL]+X.
For the changes to take effect, reload the config with this command:
To verify that everything worked, use this command to test it:
It should return the path you’ve just entered.
Congrats! Now you know how to install different versions of Java and how to switch between them.