
How to Install Apache Maven on Rocky Linux 8: A Step-by-Step Guide
Apache Maven is an indispensable tool for Java developers, providing powerful build automation and dependency management capabilities. By standardizing the build process, Maven simplifies project management, making it easier to compile code, run tests, and package results into deployable formats like JAR or WAR files.
If you’re working on a Rocky Linux 8 system, setting up Maven is a straightforward process. This guide will walk you through every step, from ensuring you have the necessary prerequisites to verifying a successful installation.
Prerequisites: Installing the Java Development Kit (JDK)
Before you can install Maven, your system needs a Java Development Kit (JDK). Maven itself is built in Java and requires a JDK to execute. You can easily check if Java is already installed with a simple command.
Open your terminal and run:
java -version
If you see output detailing the Java version, you’re ready to proceed. If not, or if you receive a “command not found” error, you must install the JDK. We recommend installing OpenJDK 11, which is stable and widely compatible.
To install the JDK on Rocky Linux 8, execute the following command:
sudo dnf install java-11-openjdk-devel
Press y when prompted to confirm the installation. The devel package is important as it includes the compiler and other tools necessary for development. Once the installation is complete, you can verify it again with java -version.
Step 1: Download the Apache Maven Archive
Next, you need to download the latest stable release of Apache Maven. It’s always best practice to get this directly from the official Apache Maven download page to ensure you have an authentic and up-to-date version.
You can download the binary tar.gz archive using the wget command. As of this writing, a recent version is 3.9.6, but be sure to check the official site for the absolute latest release.
Execute this command in your terminal to download the archive into your current directory:
wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
This will download the compressed file containing the Maven application.
Step 2: Extract and Relocate the Maven Files
Once the download is complete, you need to extract the archive. The tar command is perfect for this task.
Run the following command to decompress the file:
tar -xvf apache-maven-3.9.6-bin.tar.gz
This creates a new directory named apache-maven-3.9.6. To keep your system organized and make Maven available system-wide, it’s a good practice to move this directory to the /opt directory. The /opt folder is the standard location for optional or third-party software on Linux systems.
Use the mv command to move the folder and rename it to something simpler, like maven:
sudo mv apache-maven-3.9.6 /opt/maven
Step 3: Configure System-Wide Environment Variables
For your system to recognize the mvn command from any location in the terminal, you must configure environment variables. This involves telling the shell where to find the Maven installation.
The cleanest way to set system-wide variables on Rocky Linux is to create a new script file in the /etc/profile.d/ directory.
Create a new file named maven.sh using vi or your preferred text editor:
sudo vi /etc/profile.d/maven.sh
Inside this file, add the following lines. These lines define the necessary environment variables and add Maven’s bin directory to the system’s PATH.
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save and exit the file. Next, make the script executable so that the system can run it:
sudo chmod +x /etc/profile.d/maven.sh
Finally, load the new environment variables into your current shell session using the source command. This applies the changes immediately without needing to log out and back in.
source /etc/profile.d/maven.sh
Step 4: Verify the Apache Maven Installation
With all the configuration complete, the final step is to verify that Maven is installed correctly and that the system can find it.
Run the following command to check the version:
mvn -version
If the installation was successful, you will see output similar to this, confirming the Apache Maven version, the location of your Maven home directory, and the Java version it’s using.
Apache Maven 3.9.6 (...)
Maven home: /opt/maven
Java version: 11.0.21, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-...
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "...", arch: "x86_64", family: "unix"
Seeing this output confirms that you have successfully installed and configured Apache Maven on your Rocky Linux 8 system. You are now ready to use Maven to build, manage, and deploy your Java projects efficiently.
Source: https://kifarunix.com/install-apache-maven-on-rocky-linux-8/


