Posted in

How to Install Java (OpenJDK) on Ubuntu 24.04

How to Install Java (OpenJDK) on Ubuntu 24.04
How to Install Java (OpenJDK) on Ubuntu 24.04

Java, a cornerstone for countless applications and enterprise systems, remains a vital programming language. For developers and system administrators working with Ubuntu 24.04, setting up a Java Development Kit (JDK) is often one of the first steps. This guide will walk you through installing OpenJDK, the open-source implementation of the Java Platform, Standard Edition, on your Ubuntu 24.04 system.

Before proceeding with any installation, it’s always a good practice to update your package list to ensure you’re working with the latest available information. Open your terminal and execute:
“`bash
sudo apt update
sudo apt upgrade -y
“`

Ubuntu 24.04 repositories typically contain several versions of OpenJDK. To see which versions are available, you can use the following command:
“`bash
sudo apt search openjdk
“`
This will list packages like `openjdk-11-jdk`, `openjdk-17-jdk`, `openjdk-21-jdk`, etc. For modern development, OpenJDK 17 (LTS) or OpenJDK 21 (LTS) are popular choices. Let’s assume you want to install OpenJDK 17.

To install OpenJDK 17, use the following command:
“`bash
sudo apt install openjdk-17-jdk -y
“`
If you prefer OpenJDK 21, simply replace `openjdk-17-jdk` with `openjdk-21-jdk`.

After the installation completes, you can verify the Java version installed on your system:
“`bash
java -version
“`
You should see output similar to:
“`
openjdk version “17.0.x” 202x-xx-xx
OpenJDK Runtime Environment (build 17.0.x+x-Ubuntu-1ubuntu2)
OpenJDK 64-Bit Server VM (build 17.0.x+x-Ubuntu-1ubuntu2, mixed mode, sharing)
“`

If you have multiple Java versions installed, you might want to set a default one. Ubuntu provides a mechanism for this using the `update-alternatives` command. First, list the installed Java alternatives:
“`bash
sudo update-alternatives –config java
“`
You will see a list of available Java installations with a number next to each. Enter the number corresponding to the version you wish to set as default and press Enter.

To set the `JAVA_HOME` environment variable, which is often required by Java applications, you need to find the installation path of your chosen JDK. For OpenJDK, it’s typically located in `/usr/lib/jvm/`. You can find the exact path using:
“`bash
sudo update-alternatives –query java
“`
Look for the “Value” line. For example, it might be `/usr/lib/jvm/java-17-openjdk-amd64/bin/java`. The `JAVA_HOME` path would be `/usr/lib/jvm/java-17-openjdk-amd64`.

Now, open your shell’s profile file (e.g., `~/.bashrc` or `~/.profile` for Bash, `~/.zshrc` for Zsh) with a text editor:
“`bash
nano ~/.bashrc
“`
Add the following lines to the end of the file, replacing the path with your actual `JAVA_HOME` path:
“`bash
export JAVA_HOME=”/usr/lib/jvm/java-17-openjdk-amd64″
export PATH=$PATH:$JAVA_HOME/bin
“`
Save the file (Ctrl+O, Enter) and exit (Ctrl+X). Then, apply the changes:
“`bash
source ~/.bashrc
“`
You can verify `JAVA_HOME` is set correctly:
“`bash
echo $JAVA_HOME
“`
With Java (OpenJDK) successfully installed and configured, your Ubuntu 24.04 system is ready to run and develop Java applications.

Zac Morgan is a DevOps engineer and system administrator with over a decade of hands-on experience managing Linux and Windows infrastructure. Passionate about automation, cloud technologies, and sharing knowledge with the tech community. When not writing tutorials or configuring servers, you can find Zac exploring new tools, contributing to open-source projects, or helping others solve complex technical challenges.

Leave a Reply

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