How to Install Apache Maven on Rocky Linux

Install Apache Maven on Rocky Linux to manage Java builds, dependencies, and documentation through a standardized project structure. Whether you need to compile Java applications, manage library dependencies automatically, or integrate with CI/CD pipelines like Jenkins, Maven provides the foundation for reproducible builds. By the end of this guide, you will have a working Maven installation verified with a test project.

Rocky Linux does not install Maven by default, so you will add it with DNF or a manual tarball depending on how new of a Maven release you need.

Choose Your Apache Maven Installation Method

Before installing, consider your options. Rocky Linux offers Maven through its default repositories, while the Apache project provides the latest release for manual installation. The table below compares both approaches:

MethodChannelVersionUpdatesBest For
DNF Package ManagerRocky AppStream repositoriesDistribution defaultAutomatic via dnf upgradeMost users who prefer distro-tested packages
Manual InstallationApache DownloadsLatest stableManual re-downloadUsers needing the newest Maven features

For most users, the DNF method is recommended because it provides automatic security updates and handles Java dependencies automatically. Only install manually if you specifically need features from the latest Maven release.

Rocky Linux 10 ships Maven 3.9.9, Rocky Linux 9 ships Maven 3.6.3, and Rocky Linux 8 ships Maven 3.5.4. JDK 11, 17, and 21 are available on all three releases, but the Rocky Linux 8 Maven package defaults to Java 8 unless you set JAVA_HOME. If you need a newer Maven release than your Rocky Linux version provides, use the manual installation method below.

Method 1: Install Apache Maven via DNF Package Manager

Step 1: Update Rocky Linux System Packages

First, refresh the package cache so DNF sees the latest Maven and Java packages:

sudo dnf upgrade --refresh

This command updates the package cache and applies any pending security updates before installing new software, ensuring you get the latest available package versions.

Step 2: Install Apache Maven

Next, install Maven and the Java Development Kit (JDK) so you have the compiler needed for builds:

sudo dnf install maven java-21-openjdk-devel

While the Maven package pulls in a Java runtime, the JDK also adds the javac compiler that Maven uses to build projects.

Java 21 is available on all Rocky Linux releases (8, 9, and 10). If your project requires a specific Java version, substitute java-17-openjdk-devel or java-11-openjdk-devel in the command above. On Rocky Linux 8, Maven still defaults to Java 8 until you set JAVA_HOME, so check the troubleshooting section if your version does not change.

Step 3: Verify Maven and the Java Compiler

Once installation completes, verify that Maven is accessible and check the installed version:

mvn -version

Expected output (Rocky Linux 10):

Apache Maven 3.9.9 (Red Hat 3.9.9-3)
Maven home: /usr/share/maven
Java version: 21.0.9, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.6.87.2-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Your output may show different locale and OS version details depending on your system configuration, kernel, and whether you are running in a VM, container, or WSL.

Next, confirm the Java compiler is available:

javac -version

Expected output (Rocky Linux 10):

javac 21.0.9

Your version numbers, Java runtime path, and platform encoding will vary by Rocky Linux release and system locale. Rocky Linux 9 shows Maven 3.6.3 with Java 17.0.x by default. Rocky Linux 8 shows Maven 3.5.4 with Java 1.8.0 unless you set JAVA_HOME to a newer JDK.

Method 2: Install Apache Maven from the Binary Tarball

Alternatively, this method downloads the latest Maven release directly from Apache and installs it under /opt, giving you access to newer features before they appear in Rocky Linux repositories.

Step 1: Install Prerequisites

Maven requires a Java Development Kit (JDK). Minimal Rocky Linux installations may also be missing curl and tar, so install all prerequisites together:

sudo dnf install java-21-openjdk-devel tar curl

The JDK package provides both java and javac commands, while curl lets you fetch the release metadata and tarball from Apache servers. On minimal installations, tar may also be missing.

Once you install the packages, verify that Java is available:

java -version

Expected output (Rocky Linux 10):

openjdk version "21.0.9" 2025-10-21 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10-LTS, mixed mode, sharing)

All Rocky Linux releases (8, 9, and 10) now include Java 21 in the default repositories. If your project requires a specific LTS version, you can install java-17-openjdk-devel or java-11-openjdk-devel instead.

Step 2: Download the Latest Maven Release

Now, use the GitHub API to detect the latest Maven release and download the tarball from Apache. Keep this terminal open because the MAVEN_VERSION variable is reused in the next steps:

cd /tmp
MAVEN_VERSION=$(curl -fsSL https://api.github.com/repos/apache/maven/releases/latest | grep -oP '"tag_name": "maven-\K[^"]+')
if [ -z "$MAVEN_VERSION" ]; then
  echo "Error: Could not determine the latest Maven version."
else
  curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
fi

This sequence queries the Apache Maven GitHub repository for the latest release tag, then downloads the matching archive from the official CDN. The -f flag makes curl fail on HTTP errors, -L follows redirects, --progress-bar shows a compact download indicator, and -o saves the tarball using the versioned filename.

Expected output (download progress example):

######################################################################## 100.0%

Optional: verify the tarball checksum before extracting it:

curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512"
echo "$(cat apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512)  apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c -

Expected output (Rocky Linux 10 example):

apache-maven-3.9.12-bin.tar.gz: OK

The Apache checksum file contains only the hash, so the second line feeds the hash and filename into sha512sum -c for a one-step verification.

If the GitHub API request fails, set MAVEN_VERSION manually from the Apache Maven download page or the GitHub releases page, then re-run the download line.

Step 3: Extract Maven to the Installation Directory

Next, extract the archive to /opt/, which is the standard location for optional software on Linux systems:

sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/

Here, the -C flag specifies the target directory for extraction. If you closed your terminal, re-run the previous step so MAVEN_VERSION is set again.

Step 4: Create a Symbolic Link

After extraction, create a symbolic link to simplify path references and make future upgrades easier:

sudo ln -sfn /opt/apache-maven-${MAVEN_VERSION} /opt/maven

This command creates a /opt/maven symlink pointing to the extracted directory. The -s flag creates a symbolic link, -f replaces any existing one, and -n treats the current symlink as a regular file. When you upgrade to a newer Maven version later, updating the symlink is all that is needed.

Step 5: Configure Environment Variables

Now, create a profile script to set the required environment variables system-wide:

sudo tee /etc/profile.d/maven.sh >/dev/null <<'EOF'
export M2_HOME=/opt/maven
export PATH="${M2_HOME}/bin:${PATH}"
EOF

This heredoc writes /etc/profile.d/maven.sh with two environment variables: M2_HOME points to the Maven installation directory, and PATH includes the Maven binaries. The single-quoted EOF marker keeps the variables literal so they expand correctly in each new shell session.

If you already installed Maven from DNF, this PATH update makes the /opt/maven version take precedence. You can keep both, but remove the DNF package if you only want the manual install.

Step 6: Apply the Environment Changes

System-wide profile scripts load during login sessions. To apply the changes immediately without opening a new terminal, reload /etc/profile:

source /etc/profile

Alternatively, log out and back in, or open a new terminal window for the changes to take effect automatically. The environment variables persist for all future sessions.

Step 7: Verify the Manual Installation

Finally, confirm that Maven is correctly installed and accessible from your PATH:

command -v mvn
mvn -version

Expected output (Rocky Linux 10):

/opt/maven/bin/mvn
Apache Maven 3.9.12 (848fbb4bf2d427b72bdb2471c22fced7ebd9a7a1)
Maven home: /opt/maven
Java version: 21.0.9, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.6.87.2-microsoft-standard-wsl2", arch: "amd64", family: "unix"

The command -v mvn output confirms Maven is in your PATH at the expected location, and Maven home shows /opt/maven (the symlink you created). The Java version line confirms which JDK Maven is using.

Rocky Linux 9 and 8 will show different Java versions and kernel releases in the output, but the Maven home path remains /opt/maven across all releases.

Before creating a project, also confirm the Java compiler is available by running javac -version. Maven uses the compiler to build Java source files.

javac -version

Expected output (Rocky Linux 10):

javac 21.0.9

Create a Test Maven Project

To verify your Maven installation works correctly, try creating and building a simple Java project.

Step 1: Generate a New Project

Maven provides archetypes (project templates) to quickly scaffold new projects. Create a basic Java application:

mvn archetype:generate -DgroupId=com.example.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command creates a new directory called my-app with a standard Maven project structure, including a pom.xml configuration file and sample Java source code. The first run may take a minute as Maven downloads archetype resources from the central repository.

Step 2: Build the Project

Next, navigate into the project directory and compile the application:

cd my-app
mvn package

During this process, Maven downloads required dependencies, compiles the source code, runs tests, and packages the application into a JAR file in the target/ directory. Look for the BUILD SUCCESS message at the end:

[INFO] Building jar: /tmp/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Step 3: Run the Application

Finally, execute the compiled application to confirm everything works:

java -cp target/my-app-1.0-SNAPSHOT.jar com.example.app.App

Expected output:

Hello World!

If you see this output, your Maven installation is fully functional and ready for Java development.

Troubleshooting Common Maven Issues

JAVA_HOME Not Set or Incorrect

If Maven reports that JAVA_HOME is missing or incorrect, it cannot locate the JDK:

The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.

To diagnose, check the current variable and the actual Java path:

printf 'JAVA_HOME=%s\n' "$JAVA_HOME"
readlink -f /usr/bin/java

Expected output when JAVA_HOME is unset (Rocky Linux 10 example):

JAVA_HOME=
/usr/lib/jvm/java-21-openjdk/bin/java

To fix this, if JAVA_HOME is empty or incorrect, set it system-wide and reload the profile:

echo "export JAVA_HOME=/usr/lib/jvm/java-21-openjdk" | sudo tee /etc/profile.d/java-home.sh
source /etc/profile

Alternatively, open a new terminal window or log out and back in so the system profile reloads for your session.

Then, verify Maven can now locate Java:

mvn -version

Adjust the JAVA_HOME path based on your installed JDK version. Use /usr/lib/jvm/java-17-openjdk for Java 17 or /usr/lib/jvm/java-11-openjdk for Java 11. On Rocky Linux 8, the Maven package defaults to Java 8 unless JAVA_HOME is set, so switch the system default if needed with sudo alternatives --config java and then set JAVA_HOME to match.

Maven Command Not Found

If your shell reports that mvn is missing after a manual install, the PATH update has not been applied:

bash: line 1: mvn: command not found

First, check whether the Maven binary exists and whether it is in your PATH:

command -v mvn || echo "mvn not found in PATH"
ls -l /opt/maven/bin/mvn

Expected output when the binary exists but PATH is missing:

mvn not found in PATH
-rwxr-xr-x 1 root root 6184 Dec 13 09:16 /opt/maven/bin/mvn

To fix this, reload the system profile or open a new terminal window:

source /etc/profile

Then, verify the command works:

mvn -version

Rocky Linux 8 Shows Wrong Java Version

On Rocky Linux 8, the DNF Maven package defaults to Java 8 even when newer JDKs are installed. If Maven still reports Java 1.8, set JAVA_HOME to your preferred JDK and then reload your profile so Maven picks it up.

First, check which Java versions are installed and which is currently selected for the java command:

sudo alternatives --config java

You will see a list of installed Java versions with selection numbers. Enter the number corresponding to your preferred version (for example, java-11-openjdk) and press Enter.

After switching, set JAVA_HOME to match the JDK you want Maven to use:

echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk" | sudo tee /etc/profile.d/java-home.sh
source /etc/profile

Verify Maven now uses the correct version:

mvn -version

Expected output showing Java 11 instead of 1.8:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 11.0.25, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.25.0.9-2.el8.x86_64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.6.87.2-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Update Apache Maven

Update DNF-Installed Maven

For DNF installations, update Maven along with other system packages:

sudo dnf upgrade --refresh

If you only want to update Maven, run:

sudo dnf upgrade --refresh maven

Update Manually Installed Maven

For manual installations, you have two options: run the update commands directly, or save a reusable script for future updates.

Option 1: Run commands directly

Download the latest release and refresh the symbolic link:

cd /tmp
MAVEN_VERSION=$(curl -fsSL https://api.github.com/repos/apache/maven/releases/latest | grep -oP '"tag_name": "maven-\K[^"]+')
if [ -z "$MAVEN_VERSION" ]; then
  echo "Error: Could not determine the latest Maven version."
else
  echo "Downloading Maven $MAVEN_VERSION..."
  curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
  curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512"
  echo "$(cat apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512)  apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c -
  sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/
  sudo ln -sfn /opt/apache-maven-${MAVEN_VERSION} /opt/maven
  echo "Update complete."
  mvn -version
fi

Option 2: Create a reusable update script

For easier future updates, save the following script to your home directory. The script performs these checks before updating:

  • Tool check: Confirms curl, tar, grep, and sha512sum are available
  • Version comparison: Shows current and latest versions before proceeding
  • Confirmation prompt: Asks before downloading and installing

Create the script file with a heredoc:

cat <<'EOF' > ~/update-maven.sh
#!/bin/bash
set -e

# Check required tools
for cmd in curl tar grep sha512sum; do
  if ! command -v "$cmd" >/dev/null 2>&1; then
    echo "Error: $cmd is required but not installed."
    echo "Run: sudo dnf install curl tar grep coreutils"
    exit 1
  fi
done

# Get current installed version
echo "Checking for Maven updates..."
CURRENT_VERSION=$(mvn -version 2>/dev/null | grep -oP 'Apache Maven \K[0-9.]+' || echo "not installed")

# Fetch latest version from GitHub API
if ! LATEST_VERSION=$(curl -fsSL https://api.github.com/repos/apache/maven/releases/latest | grep -oP '"tag_name": "maven-\K[^"]+'); then
  echo "Error: Could not fetch the latest version from GitHub."
  echo "Check your internet connection or try again later."
  exit 1
fi

echo "Current installed version: $CURRENT_VERSION"
echo "Latest available version:  $LATEST_VERSION"
echo ""

if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
  echo "Already up to date."
  exit 0
fi

read -p "Continue with update? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  echo "Update cancelled."
  exit 0
fi

echo ""
echo "Downloading Maven $LATEST_VERSION..."
cd /tmp
curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${LATEST_VERSION}/binaries/apache-maven-${LATEST_VERSION}-bin.tar.gz" -o "apache-maven-${LATEST_VERSION}-bin.tar.gz"
curl -fL --progress-bar "https://dlcdn.apache.org/maven/maven-3/${LATEST_VERSION}/binaries/apache-maven-${LATEST_VERSION}-bin.tar.gz.sha512" -o "apache-maven-${LATEST_VERSION}-bin.tar.gz.sha512"
echo "Verifying SHA512 checksum..."
echo "$(cat apache-maven-${LATEST_VERSION}-bin.tar.gz.sha512)  apache-maven-${LATEST_VERSION}-bin.tar.gz" | sha512sum -c -

echo "Extracting to /opt/..."
sudo tar xzf "apache-maven-${LATEST_VERSION}-bin.tar.gz" -C /opt/

echo "Updating symbolic link..."
sudo ln -sfn "/opt/apache-maven-${LATEST_VERSION}" /opt/maven

# Clean up
rm -f "apache-maven-${LATEST_VERSION}-bin.tar.gz" "apache-maven-${LATEST_VERSION}-bin.tar.gz.sha512"

echo ""
echo "Update complete!"
mvn -version
EOF

Make the script executable:

chmod +x ~/update-maven.sh

Run the script whenever you want to check for updates:

~/update-maven.sh

Expected output when already up to date:

Checking for Maven updates...
Current installed version: 3.9.12
Latest available version:  3.9.12

Already up to date.

When an update is available, the script shows both versions, asks for confirmation, then downloads, extracts, and updates the symlink automatically.

Do not automate Maven upgrades with cron. Review release notes and test your builds before updating production environments.

Remove Apache Maven

Remove DNF-Installed Maven

To uninstall Maven installed via DNF:

sudo dnf remove maven

If you want to remove dependencies that were installed only for Maven, run sudo dnf autoremove after removal.

Next, verify the removal by checking for the Maven command:

command -v mvn || echo "mvn not found"

Expected output:

mvn not found

Remove Manually Installed Maven

The following commands permanently delete /opt/apache-maven-*, the /opt/maven symlink, and /etc/profile.d/maven.sh. If you want a backup first, run mkdir -p ~/maven-backup && sudo cp -a /opt/apache-maven-* /opt/maven /etc/profile.d/maven.sh ~/maven-backup/.

Note that the -rf flags remove directories recursively and without prompting, so double-check the paths before you run them.

To remove the manual installation, delete the installation directory, symbolic link, and environment script:

sudo rm -rf /opt/apache-maven-*
sudo rm -f /opt/maven
sudo rm -f /etc/profile.d/maven.sh

If you created a JAVA_HOME profile script while troubleshooting, you can remove that as well:

sudo rm -f /etc/profile.d/java-home.sh

After removal, open a new terminal or run hash -r to clear the command cache.

Remove Maven User Data (Optional)

The following command permanently deletes your Maven configuration, cached dependencies, and local repository in ~/.m2. If you want a backup first, run cp -a ~/.m2 ~/m2-backup.

Note that the -rf flags remove the directory recursively without prompting, so confirm you no longer need the cached dependencies.

If desired, you can also remove the local Maven repository containing cached dependencies and configuration:

rm -rf ~/.m2

Conclusion

You now have Apache Maven configured on Rocky Linux for building Java projects and managing dependencies. Whether you chose the DNF package for automatic security updates or manually installed the latest release for access to newer features, your Maven installation can now compile code, resolve dependencies, run tests, and package applications. For version control integration with your Maven projects, see how to install Git on Rocky Linux.

Leave a Comment