How to Install Java on Ubuntu and Linux Mint

Learn to install Java on Ubuntu. Go with the default version or install a specific one.
Warp Terminal

Installing Java is certainly one of the first things to do after installing Ubuntu.

Even if you are not a Java developer, you might have to install Java if you have to use tools like Android Studio or play Minecraft.

If you are not particular about the Java versions, install it using this command:

sudo apt install default-jdk

But there is more to installing Java on Ubuntu and I would like to provide you complete details in this tutorial.

The thing is that you can get Java on Ubuntu via multiple packages. There is JRE, OpenJDK and Oracle JDK. But what is the difference between these packages, and which one should you install? Let me explain that in the next section.

JRE vs. JDK vs. OpenJDK vs. Oracle JDK: What's the difference

Before we go on seeing the process to install Java, let’s quickly understand the difference between JRE, OpenJDK and Oracle JDK.

  • JRE (Java Runtime Environment): This is needed to run a Java-based application on Ubuntu. This is all you require if you are not a programmer.
  • JDK (Java Development Kit): This is what you need to if you want to program in Java.
  • OpenJDK vs Oracle JDK: It is the open-source implementation of Java Development Kit, while Oracle JDK is the official Oracle version of Java Development Kit. While OpenJDK is sufficient for most of the cases, some programs such Android Studio suggest using Oracle JDK to avoid UI/performance issues.

Now that you know the difference between JRE, JDK, OpenJDK and Oracle JDK, you should make a decision to install one of these packages.

Once you do that, it’s time to see how to install these packages and get Java on your Ubuntu Linux.

Check if Java is already installed in Ubuntu

Before you see how to install Java, you should verify if Java is already installed. This is also helpful in knowing which version of Java is installed on your system.

Open a terminal and use the following command:

java -version

If you have Java installed, you should see an output like this:

When checked the version of Java using the --version option, version info are displayed. Here, you can see that Java installed as OpenJDK in Ubuntu.
Java installed as OpenJDK

As you can see in the above screenshot, the system has Java 21 installed via OpenJDK.

If you don’t have Java installed on your system, you’ll see an output like this:

Java is not available. So, when you try to check the version of Java, it will show an error message along with some default steps to get Java in Ubuntu.
Java Unavailable

Basically, Ubuntu is giving you a hint about the various packages you can install to get Java. And as you can see, there are multiple Java versions available to install.

💡
Each Ubuntu release offers multiple Java versions to install. You can see the available versions with java -version command (if Java is not installed or with apt search jre --names-only (if Java is already installed).

We have seen the difference between various types of ‘Java’, let’s see how to install them.

Install JRE (for running Java applications)

JRE is required for running Jar files on Ubuntu. Jar files are basically precompiled Java files. As an end user, if you just want to use Java applications, you’ll need JRE.

Open a terminal and use the following command to install JRE:

sudo apt install default-jre

This will install the latest JRE available from Ubuntu. But if you want to install a specific version, you can use openjdk-21-jre, openjdk-17-jre or openjdk-11-jre instead of default-jre in the above command.

Search for available Java versions on your system:

apt search jre --names-only

This will have a huge output with various packages that contain the term jre in their name. Look for packages like openjdk-XX-jre and install them.

Let's say you want to install openjdk-11-jre which is available for your Linux system. Install it in this fashion:

sudo apt install openjdk-11-jre

Install OpenJDK (for compiling and running Java programs)

As mentioned previously, if you intend to compile and run Java programs on Ubuntu, you need the JDK. It contains the Java Compiler (javac) package.

In a terminal, use the following command to install OpenJDK Java Development Kit:

sudo apt install default-jdk

This will install the latest JDK available from Ubuntu. But if you want to install a specific version, you can use openjdk-21-jdk, openjdk-17-jdk or openjdk-11-jdk instead of default-jdk in the above command.

Search for available Java versions on your system:

apt search -jdk --names-only

This will have a huge output with various packages that contain the term -jdk in their name. Look for packages like openjdk-XX-jdk and install them.

Let's say you want to install openjdk-11-jdk which is available for your Linux system. Install it in this fashion:

🗒️
If you are looking for a specific Java version that is not available for your Ubuntu version, you can get it from the official Java website. The next section provides the details.

Install specific Java version in Ubuntu (only if you must)

Can't find the Java version you are looking for in Ubuntu repositories? Don't worry. Oracle provides a couple of installation files for Debian and Ubuntu-based systems. Let's take a look at those.

Using Official DEB File

If you are on a Debian-based system, you can use the official DEB file to install Java on your system.

Let's see how to install Oracle JDK in Ubuntu and Linux Mint, using the DEB file.

First, go to the official Oracle Java downloads page and download the deb installer file.

Download the DEB Files for Java version
Download DEB File

You may need to accept license when prompted.

Now, got to the downloaded directory and install the file using the command:

sudo dpkg -i <downloaded-file.deb>
Install the downloaded debian package
Install DEB File

You can now check the installed java version using the command:

java --version
Installed Java version is displayed using the --version option.
Installed Java version

Using Official Archive Files

🚧
Before continuing, you must note that, this is not a fully automated installation. You will face scenarios where you need to manually configure some features.

There is a TAR file for specific Java versions, that can be used to install Java on various Linux distributions. Here, you can install it in both a privileged directory (require root permission) like /opt or any directory in $HOME of the user.

📋
What is it for?
With the archive files, you can install a private version of the JDK into any location, without affecting other JDK installations.

First, download the TAR file for Java from the official website.

Download the compressed archive according to the architecture requirement.
Download the Archive File

You may need to accept license when prompted.

Now, I am going to install Java to the /opt directory. Here, I need root permission. First, move the file from the downloaded directory to the required installation directory.

sudo mv /home/team/Downloads/jdk-22_linux-x64_bin.tar.gz /opt

Now, go inside the directory and extract the tarball.

sudo tar zxvf jdk-22_linux-x64_bin.tar.gz

This will extract the files to the /opt directory. To use the Java components, you can use:

/opt/jdk-22.0.2/bin/java --version
🚧
Caution
Keep care when you add this directory to your path. It may impact your other Java installations. Do it only if you know what you are doing.

Wrapping Up

If you are into Java, C++, or PHP programming, you may need to install Eclipse IDE.

How to Install Eclipse on Ubuntu and Other Linux
A guide to installing the latest version of Eclipse IDE for Java, C++ and PHP on Ubuntu and other Linux distributions.

Running Java programs in an IDE? Here's how you can set JAVA_HOME variable correctly.

How to Set JAVA_HOME Variable in Ubuntu Linux Correctly
If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you’ll need to set JAVA_HOME to your path. Otherwise, your system will complain that “java_home environment variable is not set”. In this beginner’s tutorial, I’ll show the steps to correctly set

Now that you learned to install Java on Ubuntu, you may want to improve your Java skills. I suggest trying the interactive Java Tutorial by CodeGym. It’s a fun and engaging way to learn Java.

Any questions or suggestions are always welcome.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.