Install Java with Apt on Debian 12 Bookworm

This tutorial intends to teach you to Install Java (Default JRE and JDK) with Apt on Debian 12 Bookworm, Manage Multiple Java versions, and set up the Java_Home environment path. Java is required for many kinds of software. You can follow the steps below to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt. Debian 12 ships with the default Java 17.

Install Java with Apt on Debian 12 Bookworm

To complete this guide, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit the Initial Server Setup with Debian 12 Bookworm.

Now follow the steps below to complete your Java installation with APT on Debian 12.

Step 1 – Run Debian 12 System Update

First, you must update your local package index with the command below:

sudo apt update

Then, check that you have Java installed on your server by checking its version:

java -version

If Java is not currently installed, you’ll receive the following output:

Output
-bash: java: command not found

Step 2 – Install Default JRE and JDK on Debian 12 Bookworm

At this point, you can use APT to install the default JRE from OpenJDK 17:

sudo apt install default-jre -y

When your installation is completed, you can verify it by checking its version:

java -version

In your output, you should see:

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)

To compile and run some Java-based software, you may need JDK. To install the JDK, run the following command on Debian 12, which will also install the JRE:

sudo apt install default-jdk -y

Verify that the JDK is installed by checking the version of Javac which is the Java compiler:

javac -version

In your output, you should see:

Output
javac 17.0.7

Step 3 – Manage Multiple Java Installation on Debian 12

At this point, you can configure which Java version is the default for use if you have multiple Java installations on your server. To do this, you can use the following command in your command line:

sudo update-alternatives --config java

In my case, I get the following output:

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:

Here I have 1 choice for Java. If you have multiple choices you can type your selection number or press enter to keep the current choice.

Also, you can do this for your Java compiler, to do this, you can run the command below:

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

As you know, programs that are written in Java will use the Java_Home environment variable to find which Java installation location. To do this, you need to know where your Java is installed. You can check it by using the command below:

sudo update-alternatives --config java
Output
  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

As you can see from the output, OpenJDK 17 is installed under the following path:

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Next, open the /etc/environment file with your favorite text editor like Nano Editor, we use the vi editor:

sudo vi /etc/environment

Note: This file may be blank. At the end of the file, add the following line, making sure to replace the path with your own, and not include the bin/ portion of the path:

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

When you are done, save and close the file.

Reload the file to apply the changes with the following command:

source /etc/environment

Finally, verify your Java Home environment variable path on Debian 12 with the command below:

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

Note: Other users will need to run the source /etc/environment command or log out and log back in to apply this setting.

That’s it. You are done.

Conclusion

At this point, you have learned to Install Java (default JRE and JDK) with Apt on Debian 12 Bookworm. Also, you have learned to set up the Java_Home environment path and manage multiple Java versions.

Hope you enjoy it. You may like these articles on the Debian 12 Tutorials:

Install Lighttpd Web Server on Debian 12

Install Caddy Web Server on Debian 12

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular