How to Install Java on Ubuntu 24.04

Java is a popular open-source language for developing web apps, mobile applications, games, and more. It also offers features like object orientation, security, scalability, and strong compatibility. Therefore, installing Java on Ubuntu 24.04 allows you to take advantage of its latest features.

In this article, we use different package managers, such as apt, SDKMAN, etc., to install Java on Ubuntu.

Table of Contents

Hide ▲

How to Install Java on Ubuntu 24.04

For installing Java on the latest Ubuntu distribution, utilize the “apt ” package manager, SDKMAN package, or, deb package. Let’s begin with the apt Package Manager.

Explore HostOnce’s Scalable Linux VDS Hosting!

HostOnce gives you fast NVMe SSD storage with full root access and unlimited bandwidth. It’s ideal for developers and growing websites. Get your’s today!

Method 1: Install Java Using Apt

apt is a command-line tool that can install the Java default version or any other specific Java version. You can have this by installing JDK or JRE. You need to have the JRE package for the execution of Java applications, which consists of the JVM, libraries, and other supporting files for programs. While JDK package contains the JRE package that supports for development of Java applications.

Let’s install Java with the Apt:

Step 1: Update System Repositories

First of all, update the system repository through the following command:

sudo apt update && sudo apt upgrade -y
update system repositories

We can move on to the next stage once the system repositories have been updated and upgraded.

Step 2: Check Java Versions

After that, type below command to list all Java versions installed in the system: 

javac --version

It shows us that Java has not yet been installed, but that there are several versions we can install.

check java version

Install Java by selecting the version you want and executing the command.

Step 3: Install Java Using Apt

Select the desired Java version and then execute the command below to install it in Ubuntu 24.04:

sudo apt install default-jdk -y

After executing this command, we have JDK installed on Ubuntu by default:

install java apt

You can also install a specific Java version by replacing “default-jdk” with the version you wish to install:

sudo apt install java_version

Step 4: Verify Java Installation

To check that Java is installed on Ubuntu, utilize the following command:

java --version

This output verifies that “openjdk 21,0.3” has been successfully installed on our Ubuntu 24.04 system:

Method 2: Install Java Using Deb

You can also install Java on Ubuntu using the .deb package. Follow the step-by-step instructions below for a better understanding.

Step 1: Download the Deb Package

First of all, use the “wget” command to download the Debian file from the official website.

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

Step 2: Install Java Using Deb

Let’s install the Java package through the .deb file:

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

Step 3: Confirm Java Installation

The following command will help you check if Java has been installed on Ubuntu 24.04.

java --version
verify java installation

Method 3: Using SDKMAN

SDKMAN, a command-line utility tool, allows us to manage and install multiple versions of software. It allows us to install different Java JDK versions and switch between them.

Instructions to install Java with SDKMAN are listed below:

Step 1: Install Dependencies for SDKMAN

Install the SDKMAN dependencies by executing the following command:

sudo apt install curl zip -y
install sdkman dependencies

Step 2: Run the SDKMAN Script

The SDKMAN script can be downloaded and executed on Ubuntu 24.04 after installing the dependencies.

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

This command simply downloads the SDKMAN script over HTTP using curl. The following output is displayed on the screen after a successful execution:

download sdkman script

Run the “source” command issued to apply modifications:

source "/home/linuxuser/.sdkman/bin/sdkman-init.sh"
apply modifications

Step 3: Confirm the Installation of SDKMAN

Run the below line to list the Java versions that you are using SDKMAN to install.

sdk version
confirm sdkman installation

Step 4: Check Available Java Versions

Use the following command to verify the Java versions that can be installed with SDKMAN.

sdk list java
available java versions

Select the version you wish to install and press the q button to exit.

Step 5: Install Java Using SDKMAN

Now, you can install Java using the sdk command on Ubuntu 24.04

sdk install java identifier

Run the following command after replacing “identifier” with a Java version, such as “17.0.11”.

sdk install java 17.0.11-amzn
install java using sdk

Step 6: Verify Java Installation 

After that, verify if you have Java installed by typing:

java --version
verify java installation

Switching Default Java Version on Ubuntu 24.04

You can switch between the Java versions installed on your system by executing the following command.

sudo update-alternatives --config java

Finally, hit the “Enter” key to keep the default version or type the selection number to switch to a particular Java version:

switch between java versions

Setting JAVA_HOME Environment Variable on Ubuntu 24.04

First, run the following command to access the /etc/environment file through nano:

sudo nano /etc/environment

To set JAVA_HOME to the specific Java version, input the following line in the /etc/environment file:

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

Finally, you can write your changes and exit the nano text editor using CTRL+S and then CTRL+X.

set java home

Execute the following command to reload “/etc/environment”, and apply the changes to the session currently running:

source /etc/environment
apply changes

Now, define the JAVA_HOME while the command is running:

echo $JAVA_HOME
verify java home

How to Uninstall Java From Ubuntu 24.04

Java can be uninstalled/removed to save space. The method of installation will determine how to uninstall Java.

Method 1: Using Apt to Remove Java

If users installed Java through the Apt, you can remove it completely via the following command:

sudo apt autoremove java* -y
remove java using apt

Method 2: Using Deb

If you install Java using a .deb file, you can remove it from Ubuntu using the following command.

sudo apt autoremove jdk-21 -y
remove java installed using deb

Method 3: Using SDKMAN

Finally, utilize the command below to uninstall Java on Ubuntu installed using sdk.

sdk uninstall --force java 17.0.11-amzn
remove java using sdk

This sums up the installation and uninstallation process of Java on Ubuntu. Discover more useful Linux tips and tutorials in our Knowledge Base.

Conclusion

Java is a well-known programming language that you can install on Ubuntu 24.04 using apt, deb files, or SDKMAN. All these methods let you install the latest Java version, but each works differently. The apt and deb options are simple and quick, while SDKMAN is useful if you want to install and switch between multiple Java versions. Therefore, you can select the installation method that best suits your needs.

FAQs

Install Java using the following command: sudo apt install openjdk-17. After that, run the following command to verify installation: java -version

Installing various Java versions is as simple as using the command sudo apt install java-version. After that, you can use the following command to choose which installed Java version your system should use by default: sudo update-alternatives --config java.

Yes. Oracle Java isn’t in the Ubuntu official repository. However, you can get it from Oracle’s website or a PPA such as linuxuprising.

First, run readlink -f $(which java) to get the actual Java path. Then, remove /bin/java from the end and set your environment variable using export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64". To make it permanent, add this line to your ~/.bashrc or ~/.zshrc file. Finally, reload the file using source ~/.bashrc or source ~/.zshrc, depending on your shell.

Author: Anees Asghar

Anees is a seasoned technical writer and WordPress expert with over 5 years of experience building and optimizing WordPress solutions. He also writes on Windows, Linux, MySQL, Java, and other technologies, creating practical content that helps developers and IT professionals.

Post a Comment

Your email address will not be published. Required fields are marked *