Install Apache Maven on Linux

Ubuntu and most other Linux distributions have Apache Maven in their official repository but I prefer manual installation to avoid dependencies like Open JDK. This article explains how you can install the latest Apache Maven in Linux.


Requirements:
Apache Maven depends on Java Development Kit so you must have either Oracle JDK or OpenJDK installed on your system. In case if you do not have JDK in your system, follow these articles first and install Oracle JDK on your computer.

Step 1:
Download apache-maven-$maven_version-bin.tar.gz binary archive from this official link: Download Apache Maven. This article uses the Maven version $maven_version. You need to replace the version number by whatever the version you are downloading.

Step 2:
Open the Terminal and move to the /opt directory.
cd /opt

Step 3:
Extract the apache-maven archive into the opt directory.
sudo tar -xvzf ~/Downloads/apache-maven-$maven_version-bin.tar.gz

Step 4:
Edit the /etc/environment file and add the following environment variable:
M2_HOME="/opt/apache-maven-$maven_version"

and append the bin directory to the PATH variable:
/opt/apache-maven-$maven_version/bin

Step 5:
Update the mvn command:
sudo update-alternatives --install "/usr/bin/mvn" "mvn" "/opt/apache-maven-$maven_version/bin/mvn" 0
sudo update-alternatives --set mvn /opt/apache-maven-$maven_version/bin/mvn

Step 6:
Add Bash completion to mvn so that you can compltecomplex Maven commands by hitting Tab multiple times.
sudo wget https://raw.github.com/dimaj/maven-bash-completion/master/bash_completion.bash --output-document /etc/bash_completion.d/mvn
Credits to Juven Xu: maven-bash-completion

Step 7:
Logout and login to the computer and check the Maven version using the following command.
mvn --version
If it works, hooray! you have successfully installed the latest Apache Maven on your computer.

Previous
Next Post »

Contact Form

Name

Email *

Message *