How to Install Java in Ubuntu

Java has been ranked among the most powerful programming languages, and millions of devices run on Java. Knowing how to install Java on Ubuntu is essential for someone who is looking to learn Java or run the Java-based applications in Ubuntu. This post will guide you on installing Java in Ubuntu. The steps that are detailed here will work on Ubuntu 18.04, 20.04, 22.04, and newer versions.

Step-by-Step Guide on Installing Java in Ubuntu

Installing Java in Ubuntu is easy. You only need a user account with sudo privileges and a stable internet connection. There are different Java platforms that you can use including Java Standard Edition, JavaFX, etc. For our case, we will focus on the Java Standard Edition which offers the Java Development Kit (JDK) and the Java Runtime Environment (JRE).

When installing Java, you need JDK and JRE. JDK lets you seamlessly develop the Java applications on Ubuntu, while JRE offers an environment to run the Java-based applications. The two platforms are free and open-source for anyone to install.

That being said, here are the steps to install Java in Ubuntu.

Step 1: Refresh Apt

When you want to get the latest packages from the Ubuntu repositories, you should use the “apt” command to update the repositories.

$ sudo apt update

Next, let’s confirm if Java is available in our Ubuntu. For that, check the Java version. If Java is installed, you will see the JDK version displayed on the terminal. Otherwise, you will get an output that is similar to the one in the following which shows that Java is not installed.

Step 2: Install JRE

By default, when you install JDK, it should automatically install the equivalent JRE package. However, if JRE doesn’t work as required or you only want to run the Java-based applications without any development, you can install it as follows:

$ sudo apt install default-jre

Press “y” when prompted. Then, let the installation proceed to completion. All the required packages and dependencies will get installed and you will have JRE available on your Ubuntu.

You will see the “done” message that confirms the installation’s success at the last line on your terminal.

Step 3: Install JDK

Ubuntu supports Java. From the official Ubuntu repository, you can install OpenJDK 11. If you choose to run the following command, OpenJDK 11 will automatically get installed:

$ sudo apt install default-jdk

However, you have room to specify a different JDK version. Your project may require the latest JDK versions such as OpenJDK 18. Luckily, when installing JDK, you can specify any version that you want to install.

For instance, let us install OpenJDK 18. For that, we will change our command as follows:

$ sudo apt install openjdk-18-jdk

Once you confirm the installation, OpenJDK will install and you will now have Java available on your system and can start writing your code and build it successfully.

At this point, you can check the available JDK version on your Ubuntu.

The given output confirms that we installed OpenJDK 18 on our Ubuntu. That’s how you can comfortably install Java in Ubuntu. These steps will work on the earlier-mentioned Ubuntu versions. Besides, you can apply the same on other distros such as Linux Mint.


Java is a powerful programming language that you can use for different projects including desktop and mobile applications. To build the Java projects on Ubuntu, you must install JDK and JRE. Moreover, if you want to run the Java-based applications, you must have JRE on your Ubuntu. The steps that are covered in today’s post will guide you on what steps you should take to install Java in Ubuntu. Have fun!

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.