How to Install Java on Debian-based Linux
Java is a widely-used programming language and platform that powers numerous applications and systems worldwide. Installing Java on Debian-based Linux distributions like Ubuntu, Debian, and their derivatives is a straightforward process, but it involves a few essential steps. In this guide, I’ll walk you through the process of installing Java on your Debian-based Linux system.
Step 1: Checking Java Version. Before installing Java, it’s essential to check if Java is already installed on your system and determine its version. You can do this by running the following command in your terminal:
java -version
This command will display the installed Java version, if any.
Step 2: Find Available Java Versions. When navigating the available OpenJDK and OpenJRE versions on Debian-based Linux distributions, it’s important to note that both OpenJDK and OpenJRE are accessible options for Java installation. OpenJDK constitutes the entire Java Development Kit (JDK), encompassing both the runtime environment (JRE) and development tools, while OpenJRE solely pertains to the Java Runtime Environment.
To explore the spectrum of available OpenJDK and OpenJRE versions, execute the following command:
apt-cache search openjdk
This command generates a list of available OpenJDK and OpenJRE packages, presenting you with a selection to install either the JDK or JRE. While the JRE suffices for executing Java applications, opting for the JDK is advisable for development purposes or scenarios where access to development tools might be necessary.
If you are unsure about precise requirements or anticipate future development needs, installing the JDK offers a more comprehensive solution.
Step 3: Installing Default JDK (Java Development Kit) To install the default Java Development Kit, execute the following command. To install the default Java Runtime Environment replace “jdk” in the command below with “jre“:
sudo apt install default-jdk
This command installs the default JDK package. You can confirm successful installation of java by running the “java -version” command in your terminal.
Step 4: Installing Specific version of JDK or JRE. To install a specific version, such as OpenJDK 17, use the following command. Same as above, replace “jdk” in the command below with “jre” if you prefer to install OpenJRE 17 instead:
sudo apt install openjdk-17-jdk
Step 5: Configuring Java Versions After installing multiple Java versions. You may need to configure which version should be used as the default. Use the following command to configure Java versions:
sudo update-alternatives --config java
This command allows you to select the default Java Runtime Environment.
Step 6: Configuring Java Compiler. Similarly, if you have installed multiple versions of JDK you can configure the default Java compiler using the following command:
sudo update-alternatives --config javac
Step 7: Downloading and Installing Oracle JDK (Optional)
In instances where the specific version of Java Development Kit (JDK) you require isn’t available in the default repository, you can obtain it directly from Oracle’s website and install it manually. Here’s how you can do it:
wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb
sudo dpkg -i jdk-22_linux-x64_bin.deb
sudo apt -f install
These commands download the Oracle JDK package and install it using dpkg.
Step 8: Setting JAVA_HOME (Optional) Setting the JAVA_HOME environment variable is optional but can be useful for specifying the Java installation directory. You can set JAVA_HOME in your ~/.bashrc (for user specific setup) file or /etc/environment (for system wide setup) file by opening either file:
nano ~/.bashrc
or,
nano /etc/environment
And adding the following line. You can get the path for the desired Java version by running the “sudo update-alternatives –config java” command.
export JAVA_HOME=/path/to/java
Save the file and reload for the changes to take effect.
source ~/.bashrc
or,
source /etc/environment
Conclusion: By following the steps outlined in this guide, you can successfully install Java on your Debian-based Linux system using various methods. Whether you opt for OpenJDK or Oracle JDK, you now have the knowledge to set up Java according to your preferences and requirements. Java’s versatility and wide range of applications make it an essential tool for developers and users alike on Linux platforms.