Best Guide to Install Java with Apt on Debian 12 Bookworm
This tutorial provides a comprehensive guide on how to Install Java with Apt on Debian 12 Bookworm (Default JRE and JDK), manage multiple Java versions, and set up the JAVA_HOME
environment path. Java is a foundational requirement for numerous software applications. Follow these steps on a Debian 12 system to install the Java Runtime Environment (JRE) and the Java Development Kit (JDK) using the apt
package manager. Debian 12 comes pre-configured with Java 17 as the default.
To effectively Install Java with Apt on Debian 12 Bookworm, you’ll need access to your server as a non-root user with sudo
privileges. If you haven’t already, refer to a guide on initial server setup for Debian 12 Bookworm to configure this.
Let’s dive into the steps for Install Java with Apt on Debian 12 Bookworm.
Step 1 – Run Debian 12 System Update
Before installing Java, it’s crucial to update your local package index. This ensures you’re working with the latest package information:
sudo apt update
Next, determine if Java is already installed on your system by checking its version:
java -version
If Java isn’t installed, you’ll receive an output similar to this:
**Output**
-bash: java: command not found
Step 2 – Install Default JRE and JDK on Debian 12 Bookworm
Now, you can use apt
to install the default JRE, which is OpenJDK 17 in this case:
sudo apt install default-jre -y
After the installation completes, verify the installation by checking the Java version again:
java -version
The output should resemble the following:
**Output**
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment (build 17.0.7+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.7+7-Debian-1deb12u1, mixed mode, sharing)
For compiling and running Java-based software, you’ll likely need the JDK. Install the JDK on Debian 12 with this command; this command will also install the JRE if it’s not already present:
sudo apt install default-jdk -y
Confirm the JDK installation by checking the version of javac
, the Java compiler:
javac -version
The expected output is:
**Output**
javac 17.0.7
Step 3 – Manage Multiple Java Installations on Debian 12
If you have multiple Java installations on your system, you can configure which version is used by default. Use the update-alternatives
command:
sudo update-alternatives --config java
The output will show available Java installations:
**Output**
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
Press <enter> to keep the current choice[*], or type selection number:
Select the desired Java version by typing its selection number and pressing Enter, or press Enter to keep the current choice.
Repeat the process for the Java compiler:
sudo update-alternatives --config javac
**Example Output**
There is 1 choice for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/javac 1711 auto mode
1 /usr/lib/jvm/java-17-openjdk-amd64/bin/javac 1711 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Step 4 – Set up Java Home Environment Variable on Debian 12
Many Java programs rely on the JAVA_HOME
environment variable to locate the Java installation. To set this up, first determine the installation path of your Java version. Use the update-alternatives
command again:
sudo update-alternatives --config java
The output will display the path. For example:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
The base path (without /bin/java
) is what you’ll use for JAVA_HOME
.
Open the /etc/environment
file with a text editor (like vi
or nano
):
sudo vi /etc/environment
Add the following line to the end of the file, replacing the path with your actual Java installation path (excluding /bin
):
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Save and close the file.
Reload the file to apply the changes:
source /etc/environment
Verify the JAVA_HOME
environment variable:
echo $JAVA_HOME
**Output**
/usr/lib/jvm/java-17-openjdk-amd64
Note: Other users need to run source /etc/environment
or log out and log back in for the setting to take effect.
You have successfully completed the installation and configuration.
Conclusion
This guide covered how to Install Java with Apt on Debian 12 Bookworm (default JRE and JDK). You also learned how to manage multiple Java versions and set up the JAVA_HOME
environment path.
Alternative Solutions for Installing Java on Debian 12
While apt
is a straightforward method, other options exist for installing Java on Debian 12. Let’s explore two alternatives.
1. Using SDKMAN!
SDKMAN! (Software Development Kit Manager) is a versatile tool for managing multiple software development kits, including Java. It provides a convenient way to install, switch between, and manage different Java versions without modifying system-wide configurations.
Installation:
First, install zip
and unzip
if you don’t already have them:
sudo apt update
sudo apt install zip unzip
Then, install SDKMAN!:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to open a new terminal or source the SDKMAN! initialization script:
source "$HOME/.sdkman/bin/sdkman-init.sh"
Usage:
To list available Java versions:
sdk list java
To install a specific Java version (e.g., version 21):
sdk install java 21-open
To set a specific Java version as the default for your current shell:
sdk use java 21-open
To set a Java version as the default system-wide:
sdk default java 21-open
SDKMAN! isolates Java installations within your user’s home directory, preventing potential conflicts and allowing easy switching between versions for different projects. It also simplifies the process of upgrading and uninstalling Java SDKs.
2. Manual Installation from a Tarball
This method involves downloading a pre-built Java distribution (JDK or JRE) from Oracle or another provider as a .tar.gz
archive and manually extracting it to a directory on your system. This provides greater control over the installation location and allows you to use custom builds or versions not available through apt
or SDKMAN!.
Download:
Download the desired Java version from the official Oracle website or another trusted source. Make sure to choose the Linux x64 .tar.gz
archive.
Installation:
- Create a directory for Java:
sudo mkdir -p /opt/java
- Move the downloaded
.tar.gz
file to/opt/java
:
sudo mv jdk-21_linux-x64_bin.tar.gz /opt/java/
- Extract the archive:
sudo tar -xzf jdk-21_linux-x64_bin.tar.gz -C /opt/java/
- Remove the archive:
sudo rm jdk-21_linux-x64_bin.tar.gz
- Set up environment variables (similar to the
apt
method):
sudo vi /etc/environment
Add the following line, replacing /opt/java/jdk-21
with the actual path to your extracted Java directory:
JAVA_HOME="/opt/java/jdk-21"
Also, add the java path to the system path:
PATH="/opt/java/jdk-21/bin:$PATH"
- Reload the environment:
source /etc/environment
- Verify the installation:
java -version
Important Considerations:
- Security: When downloading from unofficial sources, verify the integrity of the tarball to prevent installing malicious software.
- Updates: Manual installations require manually downloading and installing updates, which can be more cumbersome than using a package manager.
- System Integration: You may need to manually configure system-wide settings, such as associating
.jar
files with Java.
While the manual installation method provides maximum flexibility, it also demands more responsibility for security and maintenance. The SDKMAN! approach offers a balance between convenience and control, making it a good alternative to apt
for managing Java SDKs.