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.
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.
41 Comments
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.
Thanks, great guide!
How to uninstall maven on Redhat linux
Thank you! It was helpful!
Straight to the point! thanks so much!
wget https://www-eu.apache.org/dist/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
This link is not working. I think it is not on servers for me
wget https://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
This worked please update the link and other commands according to it.
Slick and informative
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
Thanks Hansa, Article has been updated
Hello Rahul ,
I am also a RHCE 🙂 Great to view your blog. helped so much at various places. Great going brother. all the best !
Very nice HowTo!
Nice, easy for newbie like me ^^!
Nice and easy! thanks so much
Gracias desde Mexico
Your are great
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.
Thanks for the instructions. I had to use a different link though -> http://mirrors.gigenet.com/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz
Thank you so much. I have upgraded tutorial to the install Maven latest version on Linux.
Great. Thanks for the help.
Awesome. Thank you so much
GREAT BLOG DUDE!
Thanks man!!
Very precise and concise steps to follow!!
Awesome. Very crisp and clean instructions.
Thanks Nitin
Thanks
Thanks!!!!
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 ~]$
My bad, $JAVA_HOME path was wrong, actually i had copy pasted this path, later found out that jdk version was different.
Thanks
Sounds good Sushant. It’s working for you.
Nice post.
Worked like a charm on centos 6.9, thanks!
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}
Hi Abhishek,
The tutorial has created a softlink of /opt/apache-maven-3.5.0 to /opt/maven in Step 2.
Nice job! Works great.
thanks !!
Thank You!
very nice
gracias
Thank you for a great tutorial!
Thank You!