Effortless Installation of Java on Ubuntu 24.04: Install, Configure, and Run the Latest Version
Here’s a complete guide to Install Java on Ubuntu 24.04. In this article, we’ll walk you through installing the latest version of Java on Ubuntu 24.04, using both OpenJDK and Oracle JDK options. This guide also includes managing multiple installations, configuring environment variables, and verifying your setup to ensure compatibility with Java-based applications.
With Ubuntu 24.04, installing Java is straightforward, thanks to enhanced package management and updated repositories. Follow along to install Java efficiently for development or production environments.
Table of Contents
Introduction to Installing the Latest Java on Ubuntu 24.04
Java is a widely used programming language essential for various applications, from enterprise software to mobile apps. With the release of Ubuntu 24.04, users now have better access to the latest versions of Java through its repositories and package manager. This guide covers all you need to install and configure the latest Java version on Ubuntu 24.04, including the most recent Long Term Support (LTS) version, OpenJDK 21, as well as Oracle’s proprietary JDK.
Why Java Installation is Important for Ubuntu 24.04 Users
Ubuntu 24.04 is a powerful choice for developers and system administrators alike, and Java’s versatility and platform independence make it a necessary component in many development environments. Installing Java on Ubuntu allows you to run applications that depend on Java runtime environments (JRE), set up development tools, and deploy Java-based software on servers.
Step 1: Check if Java is Already Installed on Ubuntu 24.04
Before installing Java, let’s check if it’s already present. Open your terminal and run:
java -version
If Java is installed, this command will display the version number (e.g., openjdk version "21.0.0"
). If Java isn’t installed, you’ll see an error message stating the command was not found.
For additional confirmation, you can also check the Java compiler:
javac -version
This checks if the Java Development Kit (JDK) is installed, as the compiler (javac
) is included only in JDK packages, not JRE packages.
If Java isn’t installed, or you want to update to the latest version, follow the steps below.
Step 2: Installing OpenJDK 21 on Ubuntu 24.04
OpenJDK is the open-source implementation of the Java Platform, widely used due to its stability and compatibility. With Ubuntu 24.04, you can install OpenJDK 21, the latest LTS version, through the apt package manager.
Installing OpenJDK 21 Using Apt
- Update the package list to ensure you’re installing the latest packages:
sudo apt update
- Install OpenJDK 21 by running:
sudo apt install openjdk-21-jdk
- After the installation completes, verify that Java is installed:
java -version
You should see an output confirming that OpenJDK 21 is installed.
Why Use OpenJDK 21?
OpenJDK 21 is the most recent LTS version, offering enhanced performance, updated security patches, and long-term support. It is also fully compatible with Java applications and meets the requirements of most Java-dependent tools, making it ideal for development and production environments.
Step 3: Installing Oracle JDK on Ubuntu 24.04
Oracle JDK offers additional performance optimizations and features that can benefit enterprise environments. However, due to licensing restrictions, Oracle JDK is not available in Ubuntu’s default repositories. You can install it by adding a third-party PPA or downloading it directly from Oracle.
Installing Oracle JDK via a PPA
To install Oracle JDK (e.g., Oracle JDK 21) on Ubuntu 24.04, follow these steps:
- Add the Linux Uprising PPA:
sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
- Install Oracle JDK 21 by running:
sudo apt install oracle-java21-installer
- During installation, follow the prompts to accept Oracle’s license agreement.
Confirming Oracle JDK Installation
After installation, confirm that Oracle JDK is installed:
java -version
This output should display the Oracle JDK version, confirming a successful installation.
Direct Download Option
Alternatively, you can download the Oracle JDK package directly from Oracle’s Java SE Downloads, extract it to a directory like /usr/local
, and configure it manually. This option is useful if you need a custom installation path or specific build.
Step 4: Managing Multiple Java Versions
Ubuntu 24.04 allows you to install and manage multiple Java versions, which is helpful if you need different versions for various projects. The update-alternatives
command lets you switch between Java versions.
- To list all installed Java versions, run:
sudo update-alternatives --config java
- You’ll see a list of installed versions. Each version has a selection number. Type the number of the version you want to set as the default and press Enter.
- Verify the default Java version by running:
java -version
Using update-alternatives
gives you the flexibility to switch Java versions as needed for different applications.
Step 5: Setting the JAVA_HOME Environment Variable
The JAVA_HOME
variable is essential for many Java-based applications as it points to the directory where Java is installed. Configuring this variable is a standard step in setting up Java.
Configuring JAVA_HOME
- Find the Java installation path by running:
sudo update-alternatives --config java
The path will look something like /usr/lib/jvm/java-21-openjdk-amd64
.
- Open the environment file in a text editor:
sudo nano /etc/environment
- Add the following line at the end of the file, replacing the path with your Java installation directory:
JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
- Save and close the file, then reload the environment variables:
source /etc/environment
- To verify, check the
JAVA_HOME
variable:
echo $JAVA_HOME
If the output shows the correct path, the JAVA_HOME
variable is set correctly.
Step 6: Testing Your Java Installation
After installing and configuring Java, it’s good practice to run a simple Java program to confirm the setup.
- Create a new Java file:
nano HelloWorld.java
- Add the following code to the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Compile the program:
javac HelloWorld.java
- Run the compiled Java program:
java HelloWorld
If the program prints “Hello, World!” to the terminal, your Java installation is successful and ready for development.
Frequently Asked Questions
How do I uninstall Java on Ubuntu 24.04?
To remove OpenJDK, use sudo apt remove openjdk-*. For Oracle JDK, use sudo apt remove oracle-java21-installer. Run sudo apt autoremove afterward to clear any leftover dependencies.
Can I install both OpenJDK and Oracle JDK on Ubuntu 24.04?
Yes, you can install multiple JDKs and switch between them using update alternatives. This flexibility is especially helpful for developers who need different Java versions for various projects.
Which version of Java is recommended for Ubuntu 24.04?
OpenJDK 21, the latest LTS version, is sufficient for most users and supported for years to come. Oracle JDK is suitable if you require specific Oracle optimizations.
How can I switch between Java versions?
Use sudo update-alternatives –config java and select the version you want to use.
Do I need to set JAVA_HOME for all users?
Setting JAVA_HOME in /etc/environment makes it globally available. For individual users, you can set it in ~/.bashrc or ~/.profile.
With these steps, Java is now installed and configured on Ubuntu 24.04. You’re ready to start building, running, and deploying Java applications!