How to Install Java on Ubuntu 24.04


Java is an open-source, robust programming language used worldwide for developing several applications. Java is widely known for write once and run it anywhere capability and includes features like automatic memory management, strong type checking, and exception handling. The main advantage of Java is its versatility, which means applications developed through Java can be run on multiple platforms including Windows, Mac, and Linux. This portability makes Java one of the preferred languages for cross-platform development.

If you have recently installed Ubuntu 24.04, and are interested in developing or running Java applications on the system, you must install Java.

In this guide, you will see:

How to Install Java on Ubuntu 24.04

You can install Java on Ubuntu 24.04 from:

How to Install Java on Ubuntu 24.04 from Apt Package Manager

The Ubuntu 24.04 official repository includes different versions of Java packages that can be installed on the system from the apt package manager. These Java packages can be installed by installing Java Development Kit (JDK) or Java Runtime Environment (JRE) on the system.

The JRE package will install only the necessary components needed to run Java programs on the system; it will not allow you to create new Java programs. However, if you install JDK on Ubuntu, you will be able to install JRE and execute and develop Java programs on your system. Thus, you should consider installing JDK instead of JRE on the Ubuntu system.

Before installing Java on Ubuntu, ensure the repository is updated, if it’s not, use the below-given command:

sudo apt update && sudo apt upgrade -y

Then check for the different Java JDK versions that can be installed on Ubuntu using the following command:

javac --version

From the output, you will see different JDK versions that can be installed on Ubuntu. If you want to install the default JDK version on Ubuntu, use the following command, that will install Java 21 on your system.

sudo apt install default-jdk -y

Note: You can change the JDK version according to your choice in the above command.

To check whether the correct Java version is installed on Ubuntu, use the below-given command:

java --version

How to Install Java on Ubuntu 24.04 from Deb Package

Besides installing Java from the apt package manager, you can also download the Java deb package and install it from the apt command. The step-by-step guide for installing Java through the deb package is provided below:

Step 1: Download the Java Deb Package

First, visit the Java official website and download the Java deb package on Ubuntu using the wget command provided below:

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

Note: Ensure installing the latest Java deb package on Ubuntu using the wget command.

Step 2: Install Java from the Deb Package

Then install the Java from the deb package on Ubuntu from the following apt command:

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

Step 3: Check Java Version

To ensure the correct version of Java is installed on Ubuntu, run the below-given command:

java --version

How to Install Java on Ubuntu 24.04 from SDKMAN

SDKMAN is a powerful command-line utility used to install and manage different Java JDK versions on your system. It makes it easy for system users to quickly install and switch to different Java versions on their systems.

To install Java on Ubuntu 24.04 from SDKMAN, follow the below-given steps:

Step 1: Install Dependencies for SDKMAN

First, run the following command to ensure installing the required dependencies for SDKMAN installation on Ubuntu:

sudo apt install curl zip -y

Step 2: Download and Execute SDKMAN Script

Now, download and execute the SDKMAN script on Ubuntu from the following command:

curl -s "https://get.sdkman.io" | bash

Step 3: Save the Changes

To ensure changes are successfully applied, run the below-given command by replacing the username:

source "/home/username/.sdkman/bin/sdkman-init.sh"

Step 4: Confirm SDKMAN Installation

To confirm whether SDKMAN is successfully installed on Ubuntu, run the following command:

sdk version

Step 5: Check Available Java Version

To check for the availability of different versions of Java that can be installed through SDKMAN on Ubuntu, use the following command:

sdk list java

When you run the command, you will find the Vendor name, version number, distribution, and Identifier name of the Java version you want to install. Note down the Identifier for the Java version.

Then use the sdk install command, followed by the java argument and the Identifier of the specific Java version you want to install on Ubuntu:

sdk install java Identifier

Here, I am installing Java version 21.0.2 from SDKMAN on Ubuntu using the following command:

sdk install java 21.0.2-amzn

Step 6: Check Java Version

To check the correct version of Java is installed on Ubuntu through SDKMAN, use the following command:

java --version

How to Remove Java from Ubuntu 24.04

Removing Java from Ubuntu 22.04 will depend on the method you choose for the Java installation. If you have installed Java from the apt package manager, you can completely remove it from the system using:

sudo apt autoremove java* -y

If you have chosen the deb package for Java installation on Ubuntu, you can remove it from the system using:

sudo apt autoremove jdk-21 -y

To remove Java installed through SDKMAN on Ubuntu, use the following command:

sdk uninstall --force java Identifier

Replace Identifier with the Java version Identifier retrieved from the list java command.

 

How to Switch to a Specific Java Version on Ubuntu 24.04

If you have installed multiple Java versions on your Ubuntu 24.04 system, you can switch to a specific Java version by running the following command:

sudo update-alternatives --config java

There, enter the Java version position number you want to use on Ubuntu:

Once done, the default Java version will switch to the specific Java version you have selected using the above-given command. You can verify the Java version currently used by the system using:

java --version

How to Set JAVA_HOME Environment Variable in Ubuntu 24.04

By default, most Java-based applications and tools use the JAVA_HOME variable for locating the JDK installation directory. Thus, setting this environment variable on Ubuntu may also play a crucial role in running those applications and tools on the system. When you set the JAVA_HOME variable, you are telling the system to point to the JDK installation directory and use this location to run the applications on the system.

To set the “JAVA_HOME” environment variable in Ubuntu 24.04, first, find the path of the Java version you want to use for JAVA_HOME variable using the following command:

sudo update-alternatives --config java

Then select the Java version and make it the default one on your system.

Copy the path of the selected Java version and add this path to the environment variable, which you can open from the following command:

sudo nano /etc/environment

Here, we are using the Java 21 location, which we set as the default Java version in the previous step:

JAVA_HOME= "/usr/lib/jvm/java-21-openjdk-amd64/bin/java"

After you add your required Java path, save your changes by using the following command:

source /etc/environment

Once done, you can verify the Java path is successfully added on Ubuntu by using:

echo $JAVA_HOME

Conclusion

Java is a robust and versatile programming language used to create and run Java applications on the system. You can install Java on Ubuntu 24.04 through the apt package manager, deb package, or SDKMAN. All these methods will help you install the Java latest version on Ubuntu. Installing Java through an apt package manager or deb package is faster and easier to use. However, using SDKMAN to install Java will allow you to install several Java versions on the system and switch to the specific version through a single command. Further, you can also add the Java version to the JAVA_HOME variable to point to the specific Java installation directory on Ubuntu.

Print Friendly, PDF & Email
Categories