Java is a popular programming language used to develop mobile and desktop applications. Java does not come pre-installed on Ubuntu. However, you can install Java on Ubuntu Linux manually through the default repository or directly from the Oracle platform. Java has three core components, JDK (Java Development Kit), JRE (Java Runtime Environment), and JVM (Java Virtual Machine). JDK is used to develop applications, JRE is used to run Java-based applications, and JVM is used to execute Java programs. 

How to Install Java on Ubuntu 24.04

For installing Java on Ubuntu, you can use these methods:

  • Default Repository
  • Deb Package
  • App Center

Method 1: Install Java on Ubuntu from the Default Apt Repository

Ubuntu includes Java in its apt repository, which makes it easier for you to install Java on Ubuntu easily. Installing Java on Ubuntu from the default repository guarantees the stable version. To install Java via the apt package manager, check the following steps.

Step 1: Update System Packages

Ensure your system’s local repositories are up to date and are upgraded to the latest version, by running this command:

sudo apt update && sudo apt upgrade

Step 2: Install Java on Ubuntu

Java includes two components, JRE and JVM which you can install by simply installing JDK. JDK contains all the necessary components to run Java on Ubuntu. To install JDK i.e., Java on Ubuntu, run this command:

sudo apt install -y default-jdk

Optionally, Install JRE (Java Runtime Environment) Only

Even though installing JDK on Ubuntu Linux, installs JRE, however, if you want to only install and use JRE, then run this command to install Java Runtime Environment (JRE) on Ubuntu 24.04:

sudo apt install -y default-jre

Note: Installing JRE only will not install the Java development packages:

Step 3: Check the Java Version

Verify the Java installation by checking the Java version, for that, run this command:

java --version

This command will display all the Java components and their versions:

To only check the JRE (Java Runtime Environment) installation on Ubuntu, run this command:

javac --version

Method 2: Install Java on Ubuntu using the Oracle Deb Package

You can also install Java by downloading its latest deb package from its official website. To install Java from the deb package, you must have the wget utility installed, and your system architecture must be 64-bit. To install Java through the deb package, follow the given steps.

Step 1: Download the Java Deb Package Using the Terminal

Browse the official Java website, locate the Linux x64 Debian Package, copy the link, and provide the wget command to download the deb package of Java:

wget https://download.oracle.com/java/21/archive/jdk-21.0.2_linux-x64_bin.deb

The wget command downloads the deb packages directly into the Home directory:

Step 2: Install Java on Ubuntu 24.04

Once the Java deb packages are downloaded, then, provide the deb package path to the apt install command to install Java on Ubuntu 24.04:

sudo apt install ./jdk-21.0.2_linux-x64_bin.deb

Note: If you have downloaded the Java deb package from the browser, then, it will be saved into the Downloads folder. Then, to install Java on Ubuntu, you need to run this command:

sudo apt install ./Downloads/jdk-21.0.2_linux-x64_bin.deb

Method 3: Install Java on Ubuntu using App Center (Software Center)

Alternatively, you can install Java through App Center (formerly Software Center) using the deb package. Check the steps provided below to install Java through App Center using the deb package.

Step 1: Download Java Deb Package Using Web Browser

Navigate to the Java website and download the Java x64 Linux Debian Package:

Step 2: Install Java through the App Center Application

Open the Downloads folder, and locate the Java (jdk) deb package:

Once the Java deb package is located, open its context menu, then, open it with the App Center store:

Once the Java deb package is launched via App Center, then, click the Install button:

Enter the user credentials to authenticate the Java deb package installation through the App Center:

Note: If you have downloaded the deb package through Terminal then check the Home directory, or check the Downloads folder, if you have downloaded it through a web browser.

How to Install the Specific Java Version on Ubuntu 24.04

If the latest Java version is not compatible with your system specifications or it is not working well. Then, you can install any other Java version that is compatible with your system. To install the specific version of Java, follow the given steps.

Step 1: Choose the Specific Java Version to Install on Ubuntu

First, decide which version of Java you want to install on Ubuntu from the list of Java versions. To get the list of installable Java versions through Terminal, run this command:

sudo apt search "OpenJDK"

This command will search all the versions of Java, allowing you to pick and install any version of Java:

Step 2: Install the Specific Java Version on Ubuntu

Once chosen the version of Java to install on Ubuntu, run this command:

sudo apt install -y openjdk-21-jdk

Remember: Replace the “openjdk-21-jdk” with your respective Java version:

How to Set the Default Java Version on Ubuntu 24.04

If you have installed different versions of Java on Ubuntu, then, you can also switch between them using Terminal. To set the default Java version on Ubuntu, check the below instructions.

Step 1: Check the Default Java Version on Ubuntu

First, check which version of Java is configured as the default version on Ubuntu:

java --version

Step 2: Change the Default Java Version on Ubuntu

The default Java version can be switched using the “update-alternatives –config java” command in Terminal, as shown below:

sudo update-alternatives --config java

Executing this command will list down the installed Java versions. Each version will be assigned with a number. Enter the number associated with that version and press Enter to set it as the default Java version on Ubuntu:

Step 3: Verify the Change

To verify whether Ubuntu was configured to the new default Java version, run this command:

java --version

How to Uninstall Java from Ubuntu 24.04

You can uninstall Java from Ubuntu by executing the given command. However, this command will uninstall the default Java version on which Ubuntu was configured.

sudo apt remove -y default-jdk

This command will remove both JDK and JRE from Ubuntu:

To remove the specific version of Java from Ubuntu, run this command:

sudo apt remove -y {java version}

To completely uninstall Java from Ubuntu, run this command:

sudo apt autoremove java* -y

Conclusion

Java can be installed on Ubuntu through the official repository and deb package. Installing Java from the official repository is a simple and secure method, but it does not install the latest version. On the other hand, installation of Java through deb packages can be performed using Terminal and App Center. Installing Java using the deb package allows you to install the latest version.