How To Install OpenJDK on Ubuntu 24.04 LTS
In this tutorial, we will show you how to install OpenJDK on Ubuntu 24.04 LTS. Java is one of the most widely used programming languages in the world, powering countless applications across various platforms. It offers developers a robust, secure, and platform-independent environment for creating software solutions. OpenJDK, the open-source implementation of the Java Platform, Standard Edition (Java SE), has gained significant popularity among developers and organizations due to its free availability and extensive community support.
In this comprehensive guide, we will walk you through the process of installing OpenJDK on Ubuntu 24.04 LTS, the latest long-term support release of the popular Linux distribution. Whether you are a seasoned Java developer or just starting your journey, this article will provide you with the necessary steps to set up a reliable Java development environment on your Ubuntu system. By the end of this tutorial, you will have OpenJDK up and running, ready to tackle your Java projects with ease.
Prerequisites
- A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- Basic familiarity with the terminal and command-line interface.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
- An Ubuntu 24.04 system with root access or a user with sudo privileges.
Install OpenJDK on Ubuntu 24.04
Step 1. Updating the Package Repository.
The first step in installing OpenJDK is to update your system packages and install the required dependencies. Open your terminal and run the following commands:
sudo apt update sudo apt upgrade
This command will update the package lists and upgrade any existing packages to their latest versions, providing a stable foundation for the OpenJDK installation.
Step 2. Installing OpenJDK.
- Method 1: Using the Default APT Repository
The simplest and most straightforward method to install OpenJDK on Ubuntu 24.04 is by using the default APT package manager. Follow these step-by-step instructions:
sudo apt install default-jdk
Once the installation is complete, verify the installed Java version by running:
java -version
You should see an output similar to the following:
openjdk version "22.0.2" 2024-01-16 OpenJDK Runtime Environment (build 22.0.2+13-Ubuntu-2) OpenJDK 64-Bit Server VM (build 22.0.2+13-Ubuntu-2, mixed mode, sharing)
- Method 2: Using SDKMAN
SDKMAN is a popular tool for managing multiple JDK versions on Linux systems. It provides an easy way to install and switch between different Java versions. Here’s how to install OpenJDK using SDKMAN:
Install the necessary dependencies by running:
sudo apt install curl zip
Install SDKMAN by executing the following command:
curl -s "https://get.sdkman.io" | bash
Open a new terminal window or source the SDKMAN environment by running:
source "$HOME/.sdkman/bin/sdkman-init.sh"
List the available Java versions by executing:
sdk list java
Install the desired OpenJDK version, for example, OpenJDK 21, by running:
sdk install java 22.0.3-open
Verify the installation by checking the Java version:
java -version
Using SDKMAN provides flexibility in managing multiple Java versions on your system, allowing you to easily switch between them based on your project requirements.
Step 3. Configuring Java Environment.
After installing OpenJDK, it’s important to properly configure the Java environment variables to ensure smooth development and execution of Java applications. Here’s how to set up the JAVA_HOME environment variable and update the system PATH:
Locate the Java installation path by running:
ls -l /usr/lib/jvm/
Open the /etc/environment
file using a text editor with sudo privileges:
sudo nano /etc/environment
Add the following line at the end of the file, replacing the path with the actual OpenJDK directory path:
JAVA_HOME="/usr/lib/jvm/java-22-openjdk-amd64"
Save the changes and exit the text editor.
Source the /etc/environment
file to apply the changes:
source /etc/environment
Verify that the JAVA_HOME variable is set correctly by running:
echo $JAVA_HOME
Update the system PATH to include the Java binary directory:
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc
Source the .bashrc
file to apply the changes:
source ~/.bashrc
By setting the JAVA_HOME environment variable and updating the system PATH, you ensure that Java-related commands and tools can be accessed from anywhere in the terminal.
Step 4. Managing Multiple Java Versions.
In some cases, you may need to work with multiple Java versions on the same Ubuntu system. Ubuntu provides the update-alternatives command to manage and switch between different Java installations. Here’s how to use it:
List the available Java versions installed on your system:
sudo update-alternatives --list java
Configure the default Java version by running:
sudo update-alternatives --config java
Verify that the default Java version has been updated by running:
java -version
Using update-alternatives
, you can easily switch between different Java versions based on your project requirements. This flexibility allows you to maintain compatibility with older Java versions while still being able to take advantage of the latest features and improvements in newer releases.
Congratulations! You have successfully installed OpenJDK. Thanks for using this tutorial for installing OpenJDK on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official OpenJDK website.