1080*80 ad

Install Java on Debian 11: Java 11, 17, and 18

A Complete Guide to Installing Java on Debian 11

Whether you’re a developer building applications or a system administrator managing server infrastructure, having the right Java environment is crucial. This guide provides a straightforward, step-by-step process for installing various versions of OpenJDK (Java Development Kit) on Debian 11 “Bullseye.”

We will cover the installation of two Long-Term Support (LTS) versions, Java 11 and Java 17, which are recommended for most production environments due to their stability and extended support cycles.

Before You Begin: Prerequisites

To follow this guide, you will need:

  • A system running Debian 11 Bullseye.
  • Access to a user account with sudo privileges or the root user.

Step 1: Update Your System’s Package Index

Before installing new software, it’s always a best practice to update your system’s package list. This ensures you are fetching the latest and most secure versions of the software available in the repositories.

Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade

This refreshes your local package cache and applies any pending updates to your system.

Step 2: Installing the Default OpenJDK (Java 11)

Debian 11 includes OpenJDK 11 in its default repositories, making it the easiest version to install. As an LTS release, it’s a stable and reliable choice for many applications.

To install the full Java Development Kit (JDK), which includes the compiler and other development tools, use this command:

sudo apt install default-jdk

The default-jdk package is a convenient meta-package that points to the current default JDK for the Debian release, which is OpenJDK 11 for Bullseye.

If you only need to run Java applications and not compile them, you can install the Java Runtime Environment (JRE) instead. The JRE is a lighter installation that includes the Java Virtual Machine (JVM) but not the development tools.

sudo apt install default-jre

Step 3: Installing a Newer LTS Version (OpenJDK 17)

For new projects, starting with a more recent LTS version like OpenJDK 17 is often recommended. It offers performance improvements, new language features, and a long support window. OpenJDK 17 is also available directly from the Debian 11 repositories.

To install OpenJDK 17, run the following command:

sudo apt install openjdk-17-jdk

You can now have both Java 11 and Java 17 installed on the same system. In the next steps, we’ll cover how to manage them.

Step 4: Verify Your Java Installation

After the installation is complete, you should verify that Java is correctly installed and check its version. Run this command in your terminal:

java -version

If you installed OpenJDK 17 last, you will likely see output similar to this, confirming the active version:

openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Debian-1)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Debian-1, mixed mode, sharing)

Step 5: Managing Multiple Java Installations

Many developers and administrators need to switch between different Java versions for different projects. Debian uses a powerful tool called update-alternatives to manage multiple versions of a command-line tool.

To see all the installed Java versions and choose which one is the system default, run the following interactive command:

sudo update-alternatives --config java

The system will present a list of available Java installations.

There are 2 choices 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-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Simply type the selection number of the version you want to set as the default and press Enter.

Important: You should also do this for the Java compiler, javac, if you have multiple JDKs installed.

sudo update-alternatives --config javac

Step 6: Setting the JAVA_HOME Environment Variable

Many Java-based applications, including build tools like Maven and Gradle, rely on the JAVA_HOME environment variable to locate the JDK installation directory. It’s essential to configure this for system-wide compatibility.

First, you need to find the installation paths of your Java versions. You can use the update-alternatives command from the previous step to see the paths.

Let’s assume you want to set JAVA_HOME for OpenJDK 11. The path is typically /usr/lib/jvm/java-11-openjdk-amd64.

To set this variable for all users on the system, edit the /etc/environment file:

sudo nano /etc/environment

Add the following line to the end of the file:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Save the file and exit the editor. To apply the changes immediately without rebooting, run the following source command:

source /etc/environment

You can verify that the variable is set correctly by running:

echo $JAVA_HOME

The output should be the path you just set.

Security and Maintenance Best Practices

  • Stick to LTS Versions: For production servers and critical applications, always prefer Long-Term Support (LTS) releases like Java 11 and 17. They receive security updates for a much longer period than non-LTS versions.
  • Keep Your System Updated: Regularly run sudo apt update && sudo apt upgrade to ensure your Java installation receives important security patches.
  • Install Only What You Need: If you only need to run Java applications, install the JRE instead of the full JDK. This reduces the system’s attack surface by not installing development tools that are unnecessary for runtime operations.

Source: https://kifarunix.com/install-java-11java-17java-18-on-debian-11/

900*80 ad

      1080*80 ad