Installing Apache Maven on CentOS or RHEL (Red Hat Enterprise Linux) systems is a straightforward process that allows developers to manage and understand the build lifecycle, dependencies, and project documentation of their Java projects. Maven, a powerful project management tool developed by the Apache Software Foundation, uses an XML file to describe the software project being built, its dependencies on other external modules and components, the build order, directories, and required plugins.

Advertisement

This article will guide you through the steps to install Apache Maven on CentOS/RHEL versions 9, 8, and 7.

Prerequisites

  • A system running CentOS/RHEL 7, 8, or 9
  • Access to a terminal/command line
  • sudo or root privileges

Step 1: Update Your System

Before installing any new package, it’s a good practice to update your system’s package repository to ensure you have access to the latest versions and patches. Open your terminal and execute the following command:

# CentOS/RHEL 8 and 9

sudo dnf update

# CentOS/RHEL 7

sudo yum update

Step 2: Install Java

Maven is a Java-based tool, so Java SDK needs to be installed on your system. You can install Java using the following command:

# CentOS/RHEL 8 and 9

sudo dnf install java-11-openjdk-devel

# CentOS/RHEL 7

sudo yum install java-11-openjdk-devel

Verify the Java installation by running:

java -version

Step 3: Download Apache Maven

The next step is to download the latest version of Apache Maven from the official website. As of today, the latest version might have changed. Check the Apache Maven official website for the most recent version.

You can download Maven using the wget command. If wget is not installed on your system, you can install it using sudo dnf install wget or sudo yum install wget. Then, download Maven with:

wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz

Replace 3.9.6 with the latest version number you found on the Maven website.

Step 4: Extract and Install Maven

After downloading, extract the Maven archive to the /opt directory or another directory of your choice:

sudo tar xzf apache-maven-3.9.6-bin.tar.gz -C /opt

Step 5: Configure Environment Variables

To use Maven globally, you need to set up the M2_HOME and PATH environment variables. You can do this by creating a new file in the /etc/profile.d directory:

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

Add the following lines to the file:


export M2_HOME=/opt/apache-maven-3.9.6
export PATH=${M2_HOME}/bin:${PATH}

Make the script executable and load the new environment variables:

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

Step 6: Verify Installation

To ensure Maven is installed correctly, you can check its version by executing:

mvn -version

This command will display the Maven version, the Java version, and the operating system information, confirming that Maven has been successfully installed on your CentOS/RHEL system.

Conclusion

You have now successfully installed Apache Maven on your CentOS/RHEL system. Maven can help streamline your Java project builds and manage dependencies more effectively. With Maven installed, you can now proceed to create or manage your Java projects using this powerful tool.

Share.

41 Comments

  1. Thank you for sharing. The only typo is in the last command when you remove the downloaded maven archive, – you took the 3.6.3 version but were removing 3.6.2 one.

  2. Could you please correct that mistake….sudo tar xzf aapache-maven-3.6.0-bin.tar.gz. => sudo tar xzf apache-maven-3.6.0-bin.tar.gz

  3. chetan tiwary on

    Hello Rahul ,

    I am also a RHCE 🙂 Great to view your blog. helped so much at various places. Great going brother. all the best !

  4. Hi ,
    I have a doubt whether two different maven versions can be installed and used in same linux machine,Im using centos

    • Yes, You can install different Maven version on the same machine. Extract the different Maven version to different-2 directories and create /etc/profile.d/maven-v1.sh and /etc/profile.d/maven-v2.sh with proper setup.

      Now use source the required Maven version configuration file and use.

  5. Getting below error , even though it’s pointing to correct JDK
    [sw@localhost ~]$ echo $JAVA_HOME
    /usr/java/jdk1.8.0_25/
    [sw@localhost ~]$ mvn -version
    The JAVA_HOME environment variable is not defined correctly
    This environment variable is needed to run this program
    NB: JAVA_HOME should point to a JDK not a JRE
    [sw@localhost ~]$

  6. your script in set up environment variable is faulty
    export M2_HOME=/opt/maven
    export PATH=${M2_HOME}/bin:${PATH}

    its should include maven version also
    correct command is :—
    export M2_HOME=/opt/maven/apache-maven-3.5.0
    export PATH=${M2_HOME}/bin:${PATH}

Exit mobile version