Apache Maven requires no introduction to Java developers. Most Linux distributions have Apache Maven in their package repositories. However, installing from the repository may also install OpenJDK even if you already have Oracle JDK installed. Debian-based distributions like Ubuntu or Linux Mint carry a slightly older version of Apache Maven in their repository. This may cause unexpected errors when building complex Java projects because some of the plugins used in the project may not be compatible with an older version of Apache Maven. Therefore, I always recommend installing Apache Maven from the binary source and this article aims to help you get the latest version of Apache Maven installed on your Linux machine.

Requirements

Apache Maven requires either the Open Java Development Kit or Oracle Java Development Kit installed in your system. If you don’t have it already, follow this link to install the Oracle Java Development Kit on your computer.


Download Apache Maven

Visit the official Apache Maven website and download the latest binary archive file. apache-maven-3.8.6-bin.tar.gz is used in this article. If you download a different version, change the commands accordingly.

Installation

Step 1:
Open the terminal (Ctrl + Alt + T) and enter the following command to change the directory.

cd /opt

Step 2:
Extract the apache-maven-3.8.6-bin.tar.gz file in the opt directory using the following command.

sudo tar -xvzf ~/Downloads/apache-maven-3.8.6-bin.tar.gz

The above command assumes the Apache Maven binary archive filename is apache-maven-3.8.6-bin.tar.gz and it is located in the ~/Downloads folder. If your downloaded file is in any other location, change the command according to your path.


Setup The Environment

Step 1:
Enter the following command to open the environment variables file. According to your personal preference, you can choose any text editor instead of nano.

sudo nano /etc/environment

Step 2:
In the opened file, add the following bin folders to the existing PATH variable. Note that the PATH variables must be separated by a colon.

/opt/apache-maven-3.8.6/bin

Append the following environment variable to the end of the file.

M2_HOME="/opt/apache-maven-3.8.6"

A sample environment file before making these changes would look like the following:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

The same file after making the changes should look like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/apache-maven-3.8.6/bin"
M2_HOME="/opt/apache-maven-3.8.6"

Save the changes and close the editor. If you are using nano as in this article, the shortcut to save the changes is Ctrl + O; the shortcut to close nano is Ctrl + X. To learn more about setting environment variables and/or setting the environment variable without root privilege, check How to Set Environment Variables in Linux.


Step 3:
Update the mvn alternative shortcuts by running the following commands:

sudo update-alternatives --install "/usr/bin/mvn" "mvn" "/opt/apache-maven-3.8.6/bin/mvn" 0
sudo update-alternatives --set mvn /opt/apache-maven-3.8.6/bin/mvn

Step 4:
To verify the setup enter the following command and make sure that it prints the location of mvn command as you have provided in the previous step.

update-alternatives --list mvn

Step 5 (Optional):
Run the following command to install the Maven Bash Completion if you are using Bash Shell. Zsh users can activate the Maven Plugin in the Oh My Zsh framework.

sudo wget https://raw.githubusercontent.com/javahelps/maven-bash-completion/master/bash_completion.bash --output-document /etc/bash_completion.d/mvn

Step 6:
Restart the computer (or just log out and log in) and open a new terminal.


Step 7:
Enter the following command.

mvn --version

If you get the Apache Maven version 3.8.6 as the output, you have successfully installed Apache Maven on your Linux system. If you have any questions or feedback, please comment below.

Share.
Exit mobile version