Installing Maven - Linux

This article gives you a step by step walkthrough for installing Apache Maven on Linux.

You can watch the entire installation demo in this video.

Introduction

Apache Maven is the most popular build and project management tool for Java projects. Maven is built on top of Java, so if you plan to install Maven on your system, you need to have Java installed on your system.

Installation of Maven on any system consists of following 3 steps.

  1. Download the Apache Maven binary zip from the official website
  2. Extracting the binary zip into a specific folder
  3. Configure the environment variables of the System.

Let me walk you through these steps in detail for a Linux system.

Info

The latest Apache Maven version at the time of writing this is 3.9.9. All screenshots and commands in this article refer to this version. But they are equally applicable to other versions, as well.

1. Downloading the Apache Maven

The first step is to download the Apache Maven from the official website. The downloads page for Apache Maven is this - https://maven.apache.org/download.cgi. On this page, you have two binary options to download - either a zip file or a tar.gz file(marked in blue boxes). Here is the screenshot below.

Screenshot of Maven Downloads page listing different binaries | The Developer Journal Screenshot of Maven Downloads page listing different binaries | The Developer Journal

You can choose either of these. If you are not sure, choose the zip file. Also, download the corresponding checksum file(marked in orange boxes) so that you can validate the zip file download.

You can use your browser to download these, or you can use the wget or curl on the command prompt to download the files.

Here is the command using the wget that download the zip file and the corresponding checksum file.

-> wget -q --show-progress https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.zip

apache-maven-3.9.9-bin.zip      100%[======================================================>]   8.78M  17.8MB/s    in 0.5s


-> wget -q --show-progress https://downloads.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.zip.sha512
apache-maven-3.9.9-bin.zip.sha5 100%[======================================================>]     128  --.-KB/s    in 0s

-> ls -l
-rw-rw-r-- 1 thedevjournal thedevjournal 9202456 Jan 27 15:24  apache-maven-3.9.9-bin.zip
-rw-rw-r-- 1 thedevjournal thedevjournal     128 Jan 27 15:26  apache-maven-3.9.9-bin.zip.sha512
Tip

If you don’t have wget and/or curl installed, you can run sudo apt install wget curl -y. Find more install options here - for wget, and for curl

2. Extracting the Binary archive File

2.1. Verifying the Checksum

Before you extract the zip or tar.gz file, perform a quick checksum validation of the downloaded file to ensure that there were no issues with the download.

The checksum files mentions which kind of hash is used for zip file. Its sha512, and hence we need sha512sum command to calculate the checksum of the zip file. This command is part of coreutils package and if you don’t have install, you can install it with your package manager. Here is a list of installation commands for installing coreutils on your Linux distribution.

-> sha512sum apache-maven-3.9.9-bin.zip
8beac8d11ef208f1e2a8... (removed in favour of brevity) ...82ee8a2ff4c4418ba  apache-maven-3.9.9-bin.zip

-> cat apache-maven-3.9.9-bin.zip.sha512
8beac8d11ef208f1e2a8... (removed in favour of brevity) ...82ee8a2ff4c4418ba

A quick glance, you would see that both the checksums are same and that ensures that the download was completed without any errors.

2.2. Installation Directory

Before we extract the binary archive file, we need a directory. It is a good practice to keep all of your installations together, so you can place them in a directory called installed in your home directory(~).

You can create this directory if it does not exist using the mkdir command. Inside this installed directory, we need to create our maven3 directory where we will extract our binary archive file. You can create these directories like this.

-> mkdir -p ~/installed/maven3

Afterwards, we need to move our binary archive file in this maven3 directory, and then we cd into this directory so that we can extract the archive file here.

-> mv apache-maven-3.9.9-bin.zip ~/installed/maven3/

-> cd ~/installed/maven3

2.3. Extracting the Binary file

If you have downloaded the zip file, you will need unzip command to extract the zip file. After extracting the zip file, you won’t need it, so you can delete that.

-> unzip -q apache-maven-3.9.9-bin.zip

-> rm apache-maven-3.9.9-bin.zip -f

And if you have downloaded the tar.gz file, you will need tar command.

-> tar -xf apache-maven-3.9.9-bin.tar.gz

-> rm apache-maven-3.9.9-bin.tar.gz -f

You don’t need to create a symbolic link to the newly extracted folder, but it will make your life easier if a symbolic link always pointed to latest Maven installation.

So, if next time, you upgrade the Maven installation from 3.9.9 to 3.9.10, you can just update the symbolic link and everything should continue to work as it is. You won’t have to update any environment variables(discussed in the next section)

Here is how you will create a symbolic link.

-> ln -s apache-maven-3.9.9 latest

Configure the Environment Variables

With the extraction of binary archive file in the installed/maven3 directory, Maven is pretty much installed in your system. The only gotcha is that every-time you need to run the mvn command, you will have to traverse the entire path - ~/installed/maven3/latest/bin/mvn. It will be much better, if we could just run the mvn command from any directory without specifying the entire path to the mvn executable.

A lot of Java tools look for M2_HOME environment variable to locate the Maven installation directory in the system. This environment variable can be configured in any of your .bash_profile, .bashrc, .profile file. This is the entry that you will need to put.

export M2_HOME="~/installed/maven3/latest"

Once, this is configured, we need to configure the PATH variable, that will enable running of the mvn command from any directory. Here is how, it will be configured in your .bashrc or .bash_profile file.

export PATH="$PATH:$M2_HOME/bin"

Once this is configured, you need to re-import the configurations. Simple close the current terminal window and open a new one.

And now if you run mvn -version, you should see the Maven installation details confirming that you have Apache Maven installed successfully.

-> mvn -version

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /home/thedevjournal/installed/maven3/latest
Java version: 17.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.8.0-52-generic", arch: "amd64", family: "unix"



Now, you can start to run your builds locally with Maven on your Linux system.