How to Install Apache Maven on Rocky Linux/Alma Linux 8

Maven is a popular open source build tool for

used primarily for Java projects, designed to take much of the hard work out of the build process. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. Maven uses a declarative approach, where the project structure and contents are described, rather then the task-based approach used in Ant or in traditional make files, for example. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

In this guide, we will learn how to install Apache Maven on a Rocky Linux 8 server. This guide will also work for other RHEL 8 based systems like Alma Linux and Oracle Linux 8.

Related Content

Table of Content

  1. Ensuring that the server is up to date
  2. Installing Java 11
  3. Downloading and installing Maven

1. Ensure that the Server is up to date

Before proceeding, ensure that the server packages are updated. Use this command:

sudo dnf update -y

2. Installing Java 11

Apache Maven requires JDK 7 or above. We are going with openjdk 11. The package for openjdk 11 is available in the default Rocky Linux repos, install it with this command:

sudo dnf install -y java-11-openjdk-devel

Once the installation is done, verify java version using this command:

$ java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)

3. Downloading and installing Maven

Apache Maven is available as a compressed binary that can be downloaded from its downloads page here. The latest version as of the writing of this guide is v3.8.4. Download it using this command:

curl -LO https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz

Next, let us extract the downloaded file and extract it to the /opt directory

tar -xvzf apache-maven-3.8.4-bin.tar.gz
sudo mv apache-maven-3.8.4 /opt/

4. Setup Maven Environment variables

Next, let us set up Maven environment variables. We will do it in a file maven.sh in the directory /etc/profile.d/.

Note: Before that, check java installed path to set Java home directory using below command.

sudo alternatives --config java

Open the maven.sh environment file with your text editor. I am using vim

sudo vim /etc/profile.d/maven.sh

Add below content to above file

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64
export M2_HOME=/opt/<meta charset="utf-8">apache-maven-3.8.4
export MAVEN_HOME=/opt/<meta charset="utf-8">apache-maven-3.8.4
export PATH=${M2_HOME}/bin:${PATH}

Replace  java home path if it is different then save and exit the file.

Now set executable permissions to the script with following command.

sudo chmod +x /etc/profile.d/maven.sh

Then load the environment variables using source command.

source /etc/profile.d/maven.sh

Finally verify Apache Maven installation

mvn --version

This is the output on my machine

$ mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /opt/apache-maven-3.8.4
Java version: 11.0.13, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-348.el8.0.2.x86_64", arch: "amd64", family: "unix"

That is it! You have successfully installed Apache Maven on Rocky Linux 8 system.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy