How to install Apache Maven on CentOS 8

Apache Maven is an open source project management and understanding tool used primarily for Java projects. Maven uses the Project Object Model (POM), which is an XML file that contains project information, configuration information, project dependencies, and more.

In this article, we will explain how to install Apache Maven on CentOS 8.

The standard CentOS repositories contain Maven packages that can be installed using the dnf package manager. This is the easiest way to install Maven on CentOS. However, the version included in the repositories may lag behind the latest Maven version. We’ll also show you how to install the latest version of Maven by downloading the binary distribution archive from their official website.

Choose the installation method that best suits your installation and environment.

Prerequisites

The instructions assume that you are logged in as root or a user with sudo privileges.

Installing Apache Maven on CentOS with Dnf / Yum

Installing Maven on CentOS 8 using dnf is a simple and straightforward process.

  1. Install Maven by entering the following command in your terminal:
    sudo dnf install maven
  2. Run the mvn -version command to check the installation:
    mvn -version

    The output should look something like this:

    Apache Maven 3.5.4 (Red Hat 3.5.4-5)
    Maven home: /usr/share/maven
    Java version: 1.8.0_232, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "4.18.0-80.7.1.el8_0.x86_64", arch: "amd64", family: "unix"

That’s all. Maven has been installed on your CentOS system and you can start using it.

Installing the latest version of Apache Maven

In this section, we will provide step-by-step instructions for installing the latest Apache Maven on CentOS 8.

1. Installing OpenJDK

Maven 3.3+ requires JDK 1.7 or higher.

Install OpenJDK 11 by typing:

sudo dnf install java-11-openjdk-devel

Verify Java was installed successfully by running the following command:

java -version

The output should look something like this:

openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)

2. Download Apache Maven

At the time of this writing, the latest version of Apache Maven is 3.6.3. Visit the Maven download page to see if a newer version is available.

Start by downloading Apache Maven to the / tmp directory using the wget command:

wget https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp

After the download is complete, unpack the archive to the / opt directory:

sudo tar xf /tmp/apache-maven-3.6.3-bin.tar.gz -C /opt

To better control Maven versions and updates, we will create a symbolic link maven that will point to the Maven installation directory:

sudo ln -s /opt/apache-maven-3.6.3 /opt/maven

To update your Maven installation, simply unpack the newer version and change the symbolic link to point to it.

3. Setting environment variables

Next, we need to set up environment variables. Open your text editor and create a new file named mavenenv.sh in the /etc/profile.d/ directory.

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

Paste in the following code:

/etc/profile.d/maven.sh

export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Save and close the file. The script will be obtained when the shell starts.

Make the script executable by running the following chmod command:

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

Load environment variables using the source command:

source /etc/profile.d/maven.sh

4. Check the installation

To make sure Maven is installed, use the mvn -version command, which will display the Maven version:

mvn -version

You should see something like the following:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 11.0.5, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el8_0.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-80.7.1.el8_0.x86_64", arch: "amd64", family: "unix"

That’s all. The latest version of Maven was installed on your CentOS system.

Output

We showed you how to install Apache Maven on CentOS 8. You should now visit the official Apache Maven documentation page and learn how to get started with Maven.

If you run into an issue or have feedback, please leave a comment below.

Sidebar