How To Install OpenJDK on AlmaLinux 9
AlmaLinux 9 has emerged as a leading choice for developers seeking a stable and secure operating system for their applications. It is a community-driven, open-source platform that offers a robust environment to run Java applications. One of the essential tools for Java development is OpenJDK, the open-source implementation of the Java Platform, Standard Edition, which provides all the necessary libraries and tools to develop, test, and run Java applications.
This guide will take you through the step-by-step process of installing OpenJDK on AlmaLinux 9. Whether you are a beginner looking to learn Java or an experienced developer needing to set up your environment, this article will equip you with the knowledge required for a successful installation. We will cover everything from system prerequisites to verifying your installation, ensuring that you are well-prepared to get started with Java development on AlmaLinux 9.
Prerequisites
Before diving into the installation process, it’s crucial to ensure that your system meets certain prerequisites. Here are the requirements for installing OpenJDK on AlmaLinux 9:
- Operating System: A server running AlmaLinux 9. Ensure that your system is up to date and compatible with the latest version of OpenJDK.
- User Permissions: You need a non-root user account with sudo privileges. This allows you to install software packages and make necessary system changes. If you don’t have a non-root user, refer to online resources on how to create one.
- Internet Connection: A stable internet connection is necessary as you will be downloading the OpenJDK packages from the repository during the installation process.
Having these prerequisites in place will ensure that the installation process goes smoothly and without any hiccups.
Step 1: Update System Packages
Before installing OpenJDK, it is vital to update your system packages to their latest versions. This ensures that you have the most recent security patches and performance improvements. Updating your system is a straightforward process. Here are the steps to follow:
- Open Terminal: Access the terminal on your AlmaLinux 9 system. You can do this through SSH if you are accessing a remote server.
- Run System Update Command: Execute the following command to update the package index and upgrade all installed packages to their latest versions:
sudo dnf update
- Review Updates: After running the command, DNF will display a list of available package updates. Review the list to ensure all necessary updates are included.
- Proceed with Updates: If you agree to the updates, confirm the process when prompted by typing ‘y’ (yes) and hitting Enter. This will initiate the update process.
- Reboot System (If Necessary): Some updates, especially kernel updates, may require a system reboot. You can reboot your system using the following command:
sudo reboot
By keeping your system updated, you minimize the risk of conflicts or issues during the installation of OpenJDK, paving the way for a smoother setup experience.
Step 2: Install OpenJDK 11
Installing OpenJDK 11 on AlmaLinux 9 is a crucial step for developers looking to run Java applications. OpenJDK 11 is widely used and is a long-term support (LTS) version, making it a solid choice for application development. Follow these detailed steps to install OpenJDK 11 on your AlmaLinux system:
- Open Terminal: Start by opening your terminal if you haven’t already.
- Install OpenJDK 11: Use the following command to install OpenJDK 11 along with the development kit which includes the necessary tools:
This command instructs DNF to download and install OpenJDK 11 from the AlmaLinux repositories.sudo dnf install java-11-openjdk java-11-openjdk-devel
- Verify the Installation: Once the installation completes, verify it by checking the installed version with this command:
You should see output detailing the version of OpenJDK 11 that has been installed, confirming a successful installation.java --version
- Check Java Compiler Version: In addition, confirm that you can use the Java compiler (javac) by executing:
This ensures that the development tools are also correctly installed.javac --version
If you encounter any errors during installation, ensure that your system repository settings are correctly configured and that you have internet access for downloading the packages. If a specific package cannot be found, you may need to enable additional repositories or check for repository updates.
Step 3: Install OpenJDK 17
Transitioning to OpenJDK 17 signifies adopting the latest features and enhancements available to Java developers. OpenJDK 17, officially released as a long-term support (LTS) version, brings performance improvements and new language features. Here’s how to install it on AlmaLinux 9:
- Open Terminal: Launch your terminal window to begin the installation.
- Install OpenJDK 17: Execute the following command to install both OpenJDK 17 and the development tools necessary for compiling and running Java applications:
This command fetches the necessary packages from the AlmaLinux repositories and installs them on your system.sudo dnf install java-17-openjdk java-17-openjdk-devel
- Verify Successful Installation: After the installation concludes, check the installed version to confirm it was successful:
The output should reflect OpenJDK 17, indicating that it’s installed correctly.java --version
- Check the Java Compiler Version: Ensure that the Java compiler is also installed by running:
Seeing the version number of javac will confirm that you’ve everything set up correctly.javac --version
If any errors occur during this process, check your package manager settings and ensure you have a stable internet connection. Included relevant repository settings can be verified by referencing documentation specific to AlmaLinux.
Step 4: Set Default Java Version
If you have installed multiple versions of Java, such as OpenJDK 11 and OpenJDK 17, managing your Java environment is essential. The alternatives
command in AlmaLinux provides a user-friendly way to set the default Java version that the system uses. Follow these steps to set your preferred Java version:
- Open Terminal: Access your terminal to start configuration.
- View Installed Java Versions: First, check which Java versions are currently installed by running the command:
This will display a list of installed Java versions, along with their corresponding selection numbers.sudo alternatives --config java
- Select Default Version: Type the number of the Java version you want to use as the default and press Enter. For example, if you want to set OpenJDK 17 as your default version, input its corresponding number.
- Verify Default Version: To confirm the default version has been set, use the command:
The output should reflect the version you just set as default.java --version
- Repeat for javac: If you need to set the default compiler version as well, run the same command for javac:
Again, select the desired version and confirm by runningsudo alternatives --config javac
javac --version
.
Setting the appropriate Java version is crucial for the successful execution of Java applications, avoiding compatibility issues and ensuring that the environment is correctly configured for development.
Step 5: Configure JAVA_HOME Environment Variable
Configuring the JAVA_HOME environment variable is a crucial step in ensuring that Java applications run correctly on AlmaLinux. This variable points to the location of your Java installation and is used by various applications and frameworks to locate Java libraries and executables. Follow these steps to set the JAVA_HOME variable on AlmaLinux 9:
- Open Terminal: Access your terminal to begin this configuration.
- Determine Java Installation Path: First, find the installation path of your Java version. This can typically be found at: “
/usr/lib/jvm/java-11-openjdk
” for OpenJDK 11 or “/usr/lib/jvm/java-17-openjdk
” for OpenJDK 17. To verify, run:
This command will show you the full path of the Java executable, and from there, you can deduce your installation path.readlink -f $(which java)
- Create a Configuration File: Next, create a new file for the JAVA_HOME variable, which will be loaded at startup. Use the following command:
This opens the Nano text editor.sudo nano /etc/profile.d/java.sh
- Set JAVA_HOME: In the editor, add the following line (adjusting the path if you are using a different version):
Save your changes by pressingexport JAVA_HOME=/usr/lib/jvm/java-17-openjdk
CTRL + O
and then exit withCTRL + X
. - Make the Script Executable: Ensure the script is executable by running:
sudo chmod +x /etc/profile.d/java.sh
- Load the New Configuration: To apply the changes you made, load the new profile by running:
source /etc/profile.d/java.sh
- Verify JAVA_HOME: Finally, verify that the JAVA_HOME variable has been set correctly by using:
You should see the path that you defined, confirming that the configuration was successful.echo $JAVA_HOME
Setting the JAVA_HOME variable ensures that Java applications run properly, allowing easy integration with build tools like Maven and Gradle, and improving overall environment configuration for development.
Troubleshooting Common Issues
While installing OpenJDK on AlmaLinux 9 is generally straightforward, you may encounter some issues. Here are some common problems and their solutions:
- Package Not Found: If you receive a “package not found” error, ensure that your system’s repositories are up to date. Run
sudo dnf update
before attempting the installation again. - Dependency Conflicts: In case of dependency conflicts, try installing OpenJDK with the
--allowerasing
option:sudo dnf install java-17-openjdk java-17-openjdk-devel --allowerasing
. This allows DNF to remove conflicting packages. - Java Command Not Found: If the
java
command is not recognized after installation, verify that the PATH environment variable includes the Java bin directory. You can add it temporarily with:export PATH=$PATH:$JAVA_HOME/bin
- Incorrect Java Version: If the wrong version of Java is being used, double-check your
alternatives
configuration and JAVA_HOME setting.
Congratulations! You have successfully installed OpenJDK. Thanks for using this tutorial for installing OpenJDK on your AlmaLinux system. For additional or useful information, we recommend you check the official OpenJDK website.