How to Install Java on Ubuntu Server 24.04

Java is an essential runtime for many applications. Installing Java on Ubuntu is a streamlined process but requires some configuration. In this guide, we’ll walk through each step, including which versions to install, how to configure environment variables, and how to test your installation.

How to Install Java on Ubuntu Server 24.04

Introduction

Java is an essential runtime for many applications, from web servers to development environments. On an Ubuntu Server, installing Java is a streamlined process but requires a bit of configuration to get it running smoothly. In this guide, we’ll walk through each step, including which versions to install, how to configure environment variables, and how to test your installation.

1. Update the System

Before we begin, ensure your server packages are up-to-date. Updating the system helps avoid compatibility issues and prepares your server for installing new software.

sudo apt update && sudo apt upgrade -y

2. Check Java Versions Available in the Repository

Ubuntu’s default package manager (APT) can install either OpenJDK or Oracle’s JDK. To see the available Java packages, use the command:

sudo apt search openjdk

This command lists all Java Development Kit (JDK) and Java Runtime Environment (JRE) versions available in the repositories. Typically, OpenJDK is preferred because it’s open-source, widely compatible, and maintained by the community.

openjdk-11-demo/noble-updates,noble-security 11.0.24+8-1ubuntu3~24.04.1 amd64
  Java runtime based on OpenJDK (demos and examples)

openjdk-11-doc/noble-updates,noble-security 11.0.24+8-1ubuntu3~24.04.1 all
  OpenJDK Development Kit (JDK) documentation

openjdk-11-jdk/noble-updates,noble-security 11.0.24+8-1ubuntu3~24.04.1 amd64
  OpenJDK Development Kit (JDK)

openjdk-11-jdk-headless/noble-updates,noble-security 11.0.24+8-1ubuntu3~24.04.1 amd64
  OpenJDK Development Kit (JDK) (headless)
...
openjdk-17-jdk/noble-updates,noble-security 17.0.12+7-1ubuntu2~24.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-17-jdk-headless/noble-updates,noble-security 17.0.12+7-1ubuntu2~24.04 amd64
  OpenJDK Development Kit (JDK) (headless)

openjdk-17-jre/noble-updates,noble-security 17.0.12+7-1ubuntu2~24.04 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-17-jre-headless/noble-updates,noble-security 17.0.12+7-1ubuntu2~24.04 amd64
  OpenJDK Java runtime, using Hotspot JIT (headless)
...
openjdk-21-jdk/noble-updates,noble-security 21.0.4+7-1ubuntu2~24.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-21-jdk-headless/noble-updates,noble-security 21.0.4+7-1ubuntu2~24.04 amd64
  OpenJDK Development Kit (JDK) (headless)

openjdk-21-jre/noble-updates,noble-security 21.0.4+7-1ubuntu2~24.04 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-21-jre-headless/noble-updates,noble-security 21.0.4+7-1ubuntu2~24.04 amd64
  OpenJDK Java runtime, using Hotspot JIT (headless)
...
openjdk-8-jdk/noble-updates,noble-security 8u422-b05-1~24.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-8-jdk-headless/noble-updates,noble-security 8u422-b05-1~24.04 amd64
  OpenJDK Development Kit (JDK) (headless)

openjdk-8-jre/noble-updates,noble-security 8u422-b05-1~24.04 amd64
  OpenJDK Java runtime, using Hotspot JIT

I've shorted down the output for clarity to highlight just the relevant part of the command output in this example. You can run the full command to see everything in detail.

The OpenJDK version available in the Ubuntu repositories is typically a stable release. For general purposes, installing the latest long-term support (LTS) version (e.g., OpenJDK 17) is recommended. Install it with:

sudo apt install openjdk-17-jdk -y

If you specifically need an older or newer version, replace openjdk-17-jdk with the desired version from the list generated in Step 2. Like this one for OpenJDK 21:

sudo apt install openjdk-21-jdk

The APT package manager is designed to install not only the specific package you provide as a parameter but also all the necessary dependencies it needs to function properly. This ensures that when you install a package, all required components are automatically included, saving time and preventing compatibility issues.

4. Verify Java Installation

Once the installation completes, verify that Java has been successfully installed by checking the version:

java -version

You should see output showing the installed Java version (e.g., openjdk version "17.0.1"). This confirms that Java is installed and ready to use.

java -version
openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

5. Set JAVA_HOME Environment Variable

Many applications require the JAVA_HOME environment variable to locate the Java installation directory. Setting this variable is optional but recommended, especially for server configurations. First, locate the Java installation path:

sudo update-alternatives --config java
[sudo] password for dolpa:
There is 1 choice for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

This command will list the available Java paths. Copy the path you see, usually something like /usr/lib/jvm/java-17-openjdk-amd64/.

Next, open your profile file for editing:

sudo vim /etc/profile.d/java.sh

Add the following lines at the end of the file, replacing path_to_java with the path you copied earlier:

# Add Java binary to the PATH and set JAVA_HOME
export JAVA_HOME=path_to_java
export PATH=$JAVA_HOME/bin:$PATH

To save and exit Vim, press Esc, type :wq, and hit Enter. To quit without saving, type :q! instead.

To apply the changes, reload the profile:

source /etc/profile

6. Verify JAVA_HOME Variable

To confirm that JAVA_HOME is set correctly, run:

echo $JAVA_HOME

You should see the Java path you specified in the environment file. This step ensures that the variable is recognized system-wide.

echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-amd64

7. Install Oracle JDK (Alternative Method)

If you prefer Oracle JDK instead of OpenJDK, you need to download it from Oracle’s website, as it’s not included in the default Ubuntu repository. Here’s how to install it manually:

  1. Download Oracle JDK: Visit the Oracle JDK download page to get the latest .tar.gz file for Linux.
  2. Set Up Environment Variables: Follow the steps in Section 5 above, updating the JAVA_HOME variable to point to /opt/jdk-version.

Extract the Archive:

sudo tar -xvzf jdk-version_linux-x64_bin.tar.gz -C /opt

This command extracts the files to the /opt directory, which is a common location for manually installed software.

Upload or Download the Archive to Your Server: If you downloaded it on a different machine, use scp to upload it to your server. Otherwise, use wget to download it directly on the server. Example:

wget https://download.oracle.com/java/version/jdk-version_linux-x64_bin.tar.gz

8. Configure Default Java Version (Optional)

If you have multiple versions of Java installed, you can set the default using update-alternatives. This tool lets you switch between different installed versions easily:

sudo update-alternatives --config java

The output will display a list of installed Java versions. Enter the number of the version you want to set as default and press Enter.

Screenshot Placeholder: update-alternatives Java Selection Menu

9. Test Java Installation with a Sample Program

To ensure your Java installation is fully functional, create a sample “Hello World” Java program. Create a new file named HelloWorld.java:

vim HelloWorld.java

Paste the following code into the file:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save and close the file, then compile and run the program:

javac HelloWorld.java
java HelloWorld

The first command compiles the Java code into a .class file, which will appear in your working directory. The second command executes the code, displaying the output in your console. If everything is set up correctly, you should see the output:

Hello, World!

10. Troubleshooting Common Issues

a) JAVA_HOME Not Recognized

If $JAVA_HOME doesn’t output correctly, make sure you spelled it exactly as configured in /etc/profile and that you reloaded your profile with source /etc/profile.

b) Error in Compiling Java Programs

If you encounter errors while compiling Java programs, verify that javac (the Java compiler) is in your PATH by running javac -version. If it’s not, revisit the PATH configuration in Section 5.

Conclusion

Installing Java on an Ubuntu Server is a straightforward process, but setting up the environment variables and verifying the installation ensures everything is configured correctly. By following this guide, you’re now ready to run Java applications, whether on OpenJDK or Oracle JDK, with a fully set up environment for development or production purposes.

Read next