Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
How to Install Java and JDK on Linux Systems: A Complete Guide for Beginners to Advanced Users

Java is a powerful programming language used in the development of many applications ranging from web and desktop to enterprise-level systems. In this guide, You will learn installing, managing, and configuring Java into Ubuntu, Rocky Linux, and AlmaLinux.

1. What Is Java, and How Does It Integrate with Linux Systems?

Open-source nature, stability, and strong command-line interface are the qualities developers appreciate in Linux distributions like Ubuntu, Rocky Linux, and Alma Linux. It means that Linux systems are easily available to Java because it is a very prominent choice of programming for software development because of its cross-platform capability and robustness. With the Java installation on these distributions a reliable environment is provided to developers for creating, testing, and deploying different projects. In this guide, we cover installing, configuring, and managing Java to get the most out of development experience in Linux.

2. Preparing Your System for Installing Java

To set up your system, Java shall proceed, only after which is the right time actually to install the Java. It is a process where you have to update the package index, fetch the latest package information, and then check if there are any existing Java installations.

Steps to Prepare Your System:

  • Ubuntu:
sudo apt update
Run sudo apt update on Ubuntu to prepare for Java installation
  • Rocky Linux and AlmaLinux:
sudo dnf check-update
  • Check for existing Java versions:
java -version
Run 'java -version' to check installed Java versions
  • Install useful tools (e.g., curl or wget):
sudo apt install curl  # Ubuntu
Install curl with 'sudo apt install curl' on Ubuntu
sudo dnf install curl  # Rocky Linux/AlmaLinux

Ensuring these preparations helps prevent conflicts and smoothens the installation process.

3. Installing Java on Ubuntu

Ubuntu’s robust package management system makes Java installation simple. This section covers installing both OpenJDK and Oracle Java.

a) Using apt for OpenJDK

OpenJDK is the default open-source implementation of Java and is simple to install using apt.

Install OpenJDK 11 (LTS version):

To install OpenJDK 11, which is the long-term support (LTS) version of Java, you can use the apt package manager on Ubuntu-based systems. This easy approach makes sure you are using the open-source version of Java. Begin by updating your system’s package index with sudo apt update to verify all repositories are current. Then, install OpenJDK 11 using the command:

sudo apt install openjdk-11-jdk
Install OpenJDK 11 with 'sudo apt install openjdk-11-jdk' on Ubuntu

Install a different version (e.g., OpenJDK 8 or 17):

If you are installing a different version of OpenJDK other than the default, say OpenJDK 8 or 17, you need to update the installation command for OpenJDK to point to the version that you would like to use. For example, to install OpenJDK 17, you might type sudo apt install openjdk-17-jdk, and it will show you that OpenJDK 17 and any needed dependencies are installed on your machine.

If you are more into legacy app compatibility, you can use sudo apt install openjdk-8-jdk to get something more similar to OpenJDK 8. It means you can choose the one that will best suit your project’s needs. After this, you will run the Java version command to verify the installation. This check makes sure the installation was successful and that our chosen Java version is setup properly.

sudo apt install openjdk-17-jdk  # or openjdk-17-jdk
Install OpenJDK 17 with 'sudo apt install openjdk-17-jdk' on Ubuntu

Verify the installation:

You should check if the proper version of Java were set up correctly and installed. When the installation is completed, in your terminal, run the command java -version. With this command, we get information about the installed Java and how it was installed as this command outputs detailed information about the installed Java version, and it confirms that it is installed. In the output, it would display the version number and build details and environment info such as ‘OpenJDK version ‘17.0.12’ ‘, anything you install and like. Confirming the Java version is used not only to help detect any discrepancies but also to make doubly sure that the development surrounding your Java-based applications, in general, is configured with Java correctly.

java -version

b) Installing Oracle Java

To install Oracle Java, which may offer improved performance and long-term support features compared to OpenJDK, you first need to add the Oracle PPA to your system’s repository list. Such an approach enables your package manager to access the Oracle Java packages directly. Start by running the following command:

Add the Oracle PPA:

sudo add-apt-repository ppa:linuxuprising/java
Add Oracle PPA with sudo add-apt-repository ppa:linuxuprising/java

This step sets up the repository that has the Oracle Java binaries in it, making the installation a little cleaner. This PPA adds this so your system knows to recognize Oracle Java packages when you install them. Then, add this PPA to your sources and update your package list (including the new repository) before actually installing Oracle Java.

  • Update the package list:

Once the Oracle PPA has been added to your system, update your package list by running.

sudo apt update
  • Install Oracle Java:

After updating, proceed to install Oracle Java with the command.

sudo apt install oracle-java17-installer

(or replace java17 with your desired version). During the installation, a configuration screen will appear, prompting you to accept the Oracle license agreement. It is required to follow these on-screen prompts carefully and select “Yes” to agree to the terms and continue. This step confirms that the Oracle Java installation completes without interruptions and is properly configured on your system.

Note: Follow the on-screen prompts to accept the Oracle license agreement during installation.

Install Oracle Java 17 with 'sudo apt install oracle-java17-installer' and accept the license agreement

4. Installing Java on Rocky Linux and AlmaLinux

Both Rocky Linux and AlmaLinux use dnf as their package manager. This section explains how to install OpenJDK and Oracle Java.

a) Using dnf for OpenJDK

Install OpenJDK 11:

sudo dnf install java-11-openjdk-devel

Install other versions:

sudo dnf install java-8-openjdk-devel  # or java-17-openjdk-devel

Verify the installation:

java -version

b) Installing Oracle Java Manually

Oracle Java can be installed manually by downloading the RPM package from Oracle’s official website.

Steps to Install Oracle Java:

Download the RPM package:

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
  1. Install the package:
sudo dnf localinstall jdk-17_linux-x64_bin.rpm
  1. Verify the installation:
java -version

5. Managing Java Versions on Linux

If multiple versions of Java are installed, the proper thing to do is to manage which Java version your system uses by default. The update-alternatives or alternatives can be used to do such a task.

a) Setting the Default Java Version

On Ubuntu:

When multiple versions of Java are installed on your system, it is important to manage and specify which version should be used by default. Such a configuration can be achieved using the update-alternatives command, which allows you to prioritize one Java version over others. To set the default Java version on Ubuntu, use the following command:

sudo update-alternatives --config java

They will show a list of all your installed Java versions with their priority levels. You can then enter the selection number for the version you want to use as the default. It will verify that your Java environment is compatible with your project requirements, so if you choose to use Java programming, you’ll have a smooth Java application development.

Set default Java version with 'sudo update-alternatives --config java' and select the desired version

On Rocky Linux and AlmaLinux:

sudo alternatives --config java

Select the desired version by choosing the corresponding number.

b) Switching Between Multiple Versions

You can change the active Java version by re-running the update-alternatives or alternatives command and selecting the version you need.

6. Configuring Environment Variables

Configuring JAVA_HOME and updating the PATH is required for some development tools and projects.

a) Setting Up JAVA_HOME

Configuring the JAVA_HOME environment variable and updating the PATH is important for proper functionality in development tools and Java projects. To set up JAVA_HOME, begin by opening your shell configuration file, such as .bashrc or .bash_profile, using a Text editor with the following command:

nano ~/.bashrc
  1. Add the JAVA_HOME variable:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk

This line sets JAVA_HOME to the path where your Java installation is located. If you’re using a different Java version, replace java-11-openjdk with the appropriate directory path. Save and Exit the editor, then run source ~/.bashrc to apply the changes immediately. This setup confirms that Java applications recognize the environment variable and function seamlessly.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
  1. Save the file and reload the shell:
source ~/.bashrc

b) Updating the System’s PATH

After setting the JAVA_HOME variable, save the configuration file and reload the shell by running source ~/.bashrc. This command checks that the changes take effect without needing to log out and back in. Next, to include the Java bin directory in your system’s PATH, which allows you to run Java commands from any terminal session, add the following line to your shell configuration file:

export PATH=$JAVA_HOME/bin:$PATH

Such a setup enables Java commands to be executed from any terminal session.

7. Testing Your Java Installation

Check the runtime environment:

To check if Java is correctly installed and functioning, testing your installation is important. Start by checking the Java runtime environment with the command:

java -version

This command should output the installed Java version, confirming that it is set up correctly, for example, “java version ‘17.0.6’” along with details like the build and runtime environment.

Verify Java installation by running 'java -version' to display the installed version and environment details.

Verify the Java compiler:

javac -version

Such a command verifies the Java Development Kit (JDK) installation and returns the compiler version, such as “javac 17.0.6”. If both commands return the expected outputs without any errors, your Java setup is complete and ready for use. This verification step checks your development environment is correctly configured and avoids runtime issues in Java-based applications.

Java version test output showing java version '17.0.6'

If these commands return the expected versions without errors, your Java setup is complete.

8. Installing the Java Development Kit (JDK)

While the JDK is often included during Java installation, developers may need to install it separately to access tools like Javac.

Install the JDK:

​​In some cases, installing Java does not automatically include the Java Development Kit (JDK), which provides many development tools like Javac for compiling Java code. To install the JDK on Ubuntu, use the following command:

  • Ubuntu:
sudo apt install default-jdk
Command to install Java Development Kit JDK on Ubuntu with output confirmation

This command installs the default JDK version available in Ubuntu’s repository, which includes the necessary development tools and libraries. During the installation, the package manager will download and configure the JDK, ensuring it is ready for use. After installation, you can verify the setup by running the Javac -version, which should output the installed JDK version, confirming that your development environment is fully prepared for Java programming tasks.

  • Rocky Linux/AlmaLinux:
sudo dnf install java-17-openjdk-devel

9. Troubleshooting Common Issues

Common Problems and Solutions

  • Repository Issues: Check if your repositories are updated (sudo apt update or sudo dnf check-update).
  • Permission Errors: Run commands with sudo for administrative privileges.
  • Path Errors: Verify that JAVA_HOME and PATH are set correctly in your shell configuration file.

Debugging Steps

  • Check if Java binaries are in the correct path.

Confirm if multiple versions conflict by running:

which java

11. FAQs about Java on Linux Systems

Q: What is the difference between JDK and JRE?
A: The JRE (Java Runtime Environment) is used to run Java applications, while the JDK (Java Development Kit) includes the JRE and additional tools for developing Java programs.

Q: How do I remove Java from my system?

  • Ubuntu:
sudo apt remove openjdk-11-jdk
Remove Java openjdk-11-jdk on Ubuntu with confirmation output.
  • Rocky Linux/AlmaLinux:
sudo dnf remove java-11-openjdk-devel

Q: Which version of Java should I install?
A: Choose an LTS version (e.g., Java 11 or 17) for stability, or use Java 8 for legacy systems. New projects may benefit from the latest LTS release.

Q: Can I install both OpenJDK and Oracle Java?
A
: Yes, you can install and manage both versions using update alternatives or alternatives to switch between them as needed.

About the writer

Vinayak Baranwal Article Author

Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.

Leave a Reply

Your email address will not be published. Required fields are marked *

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers