How To Install Java on Ubuntu 18.04, 20.04 & 22.04 using Apt-Get

Java on Ubuntu

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.

See Also: Experience Our for Free VPS Hosting: Enjoy a 30-Day Trial with Risk-Free Servers

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.

See Also: Experience Our for Free VPS Hosting: Enjoy a 30-Day Trial with Risk-Free Servers

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:

java -version

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:

JAVA_HOME="YOUR_PATH" 

(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:

source /etc/environment 

To verify that everything worked, use this command to test it:

echo $JAVA_HOME 

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.

See Also: Experience Our for Free VPS Hosting: Enjoy a 30-Day Trial with Risk-Free Servers

COMPLETE DIGITAL SERVER SOLUTIONS FOR ALL

Bare Metal Dedicated Servers

A single tenant, physical server allowing you full access to its resources

Read More

Cloud VPS

The cheapest way to get your own independent computing instance.
Read More

Cloud VDS

Virtualized server platform hosted on enterprise-grade physical servers

Read More

10 Gbps Unmetered Servers

Zumiv offers high bandwidth dedicated servers up to 20Gbps.
Read More

ZUMIV NEWSLETTER

Receive the latest news, updates and offers. You can unsubscribe at any time.

ZUMIV NEWSLETTER

Receive the latest news, updates and offers. You can unsubscribe at any time.

zumiv logo white final 1 1

Cloud VPS and Bare Metal Servers for Less

Registered Office:
124 City Road, London
EC1V 2NX, United Kingdom.
44-7-441-399-305
Support Hours: 24x7x365
Sale Office Hours: M-F, 7AM-5PM EST

We accept the following:

payment option

PRODUCTS

SERVICES

© 2023 Webcom Ltd. (DBA zumiv.com) Company,  CR: 14252964, ® All Rights Reserved

certifications

ZUMIV NEWSLETTER

Receive the latest news, and offers. You can unsubscribe at any time.

CALL US 44.7.441.399.305

  • PRODUCTS
  • LOCATIONS
  • SOLUTIONS
  • COMPANY
This is a staging enviroment

Please tell us more about yourself.

Complete the form below and one of our experts will contact you within 24 hours or less. For immediate assistance contact us.

In order to finalize your application, please read and accept our Terms and Conditions*.

CUSTOM QUOTE REQUEST

Complete the form below and one of our experts will contact you within 24 hours or less. For immediate assistance contact us.

We promise not to sell, trade or use your email for spam. View our Privacy Policy.