Java is an open-source globally known programming language used to develop web applications, mobile apps, game development, etc. Java is famous for its robust features like object-oriented, security, scalability, etc. One of the well-known features of Java is its multi-platform compatibility. This means we can install and use Java programming language on any platform like Linux, Windows, Mac, etc.
Ubuntu 24.04, codenamed “Noble Numbat”, is the latest LTS release and is available for download on Ubuntu’s official page. We can install Java on Ubuntu 24.04 and benefit from its latest features. For this purpose, we can use different package managers like apt, SDKMAN, etc.
How to Install Java on Ubuntu 24.04
To install Java on Ubuntu 24.04, we can use the “Apt” package manager, “Deb” package, or “SDKMAN”. Let’s start with the apt package manager.
Method 1: Installing Java on Ubuntu 24.04 Using Apt
apt is a well-known command-line tool that can be used to install the default or any specific Java version. This can be done by installing JDK or JRE. The JRE package installs only the components needed to run Java programs, while the JDK package enables both running and developing Java programs. Therefore, it is recommended to install JDK instead of JRE on Ubuntu.
Follow the given stepwise instructions to install Java on Ubuntu 24.04 using Apt.
Step 1: Update and Upgrade System Repositories
Before installing Java, first, execute the provided command to update and upgrade the repository:
sudo apt update && sudo apt upgrade -y
Once the system repositories are updated and upgraded, we can proceed to the next step.
Step 2: Check Available Java Versions
To check all available Java versions, execute the following command:
javac --version
The output snippet shows that Java isn’t yet installed on our machine, but it shows several versions that we can install on Ubuntu 24.04:
You can pick the desired Java version and execute the corresponding command to install it on your system.
Step 3: Install Java Using Apt
From the available Java versions, select the desired one, and execute the below-given command to install it on Ubuntu 24.04:
sudo apt install default-jdk -y
On successful execution of this command, the default jdk will be installed on Ubuntu, as shown in the following screenshot.
If you want to install any specific Java version, replace “default-jdk” with the specific version(that you want to install), like this:
sudo apt install java_version
Step 4: Verify Java Installation
To confirm Java’s installation on Ubuntu 24.04, run the following command that retrieves the currently installed Java version:
java --version
The output confirms the successful installation of “openjdk 21.0.3” on our Ubuntu 24.04 machine:
Method 2: Installing Java on Ubuntu 24.04 Using Deb
We can download the Java deb package using wget command and install it using the apt command. For better understanding, walk through the below-given stepwise instructions:
Step 1: Download Deb Package
The wget is a command line utility that lets us download files from the web. Execute the below-given “wget” command to download the Java deb package from Oracle’s official page:
sudo wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
Step 2: Install Java Using Deb
Now execute the following command to install Java from the deb package:
sudo apt install ./jdk-21_linux-x64_bin.deb
Step 3: Confirm Java Installation
Finally, we can run the given command to verify the Java’s installation on Ubuntu 24.04:
java --version
Method 3: Installing Java on Ubuntu 24.04 Using SDKMAN
SDKMAN is a command-line utility tool that allows us to install and manage multiple versions of software development kits (SDKs) on a system. We can use it to install different versions of Java JDK and switch between them.
To install Java on Ubuntu 24.04 using SDKMAN, follow the steps below:
Step 1: Install Dependencies for SDKMAN
First, execute the given command to install the essential dependencies for SDKMAN:
sudo apt install curl zip -y
Step 2: Run the SDKMAN Script
After installing the required dependencies, we can run the below-provided command to download and execute the SDKMAN script on our Ubuntu 24.04:
curl -s "https://get.sdkman.io" | bash
This command uses curl to download the SDKMAN script from the URL “get.sdkman.io”, and then pipes it to bash to execute it. On successful execution, the following output appears on the screen:
Now execute the given “source” command to save the changes:
source "/home/linuxuser/.sdkman/bin/sdkman-init.sh"
Step 3: Confirm the Installation of SDKMAN
You can confirm the SDKMAN installation using the following command:
sdk version
Step 4: Check Available Java Versions
Once SKD is successfully installed, use the given command to check the available Java versions (that can be installed using SDKMAN):
sdk list java
Choose the version that you want to install on your system and hit the q button to exit.
Step 5: Install Java Using SDKMAN
Now run the following sdk command to install Java on Ubuntu 24.04:
sdk install java identifier
Replace the “identifier” with a specific Java version like “17.0.11”, and run the command:
sdk install java 17.0.11-amzn
Step 6: Verify Java Installation
Finally, we can check the installed Java version to confirm the installation:
java --version
Switching Default Java Version on Ubuntu 24.04
If we’ve multiple Java versions installed on our system, we can switch between them by executing the following command:
sudo update-alternatives --config java
Press the “Enter” key to keep the default Java version or select the preferred Java version by specifying the corresponding selection number:
Setting JAVA_HOME Environment Variable on Ubuntu 24.04
To set the JAVA_HOME environment variable on Ubuntu, open the “/etc/environment” file in the nano editor by executing the below-given command:
sudo nano /etc/environment
Now specify the path of the selected Java version in the “/etc/environment” file to set the JAVA_HOME environment variable:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64/bin/java"
Hit the “CTRL+S” and “CTRL+X” to save the changes and exit the nano editor:
Now execute the given command to reload the “/etc/environment” file and apply changes to the current session:
source /etc/environment
Now run the given command to ensure the JAVA_HOME is Set:
echo $JAVA_HOME
How to Uninstall Java From Ubuntu 24.04
If Java is no longer needed, we can uninstall/remove it to free up some space. However, uninstallation of Java depends on the installation method.
Method 1: Uninstalling Java From Ubuntu 24.04 Using Apt
If we install Java on our Ubuntu 24.04 using Apt, we can completely uninstall it using the following command:
sudo apt autoremove java* -y
Method 2: Uninstalling Java From Ubuntu 24.04 Using Deb
If Java is installed on Ubuntu using the deb package, we can remove it from the system by executing the following command:
sudo apt autoremove jdk-21 -y
Method 3: Removing Java From Ubuntu Using SDKMAN
Execute the below-given command to uninstall Java installed via SDKMAN on Ubuntu:
sdk uninstall --force java 17.0.11-amzn
This sums up the installation and uninstallation of Java on Ubuntu 24.04.
Final Thoughts
Java is a versatile programming language that can be installed on Ubuntu 24.04 using “apt”, “deb”, or “SDKMAN”. All the mentioned methods let us install the latest version of Java on Ubuntu 24.04. However, each method has its pros and cons, you can choose an installation method according to your requirements. For instance, Java installation using apt and deb is faster and easier while SDKMAN helps us install multiple Java versions and switch between them.