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.

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 the official link. 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 change the directory to /opt folder.
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:
also, append the bin directory to the PATH variable:

You can use nano to edit the file in the terminal itself. Execute the following command and modify the content as given below.
sudo nano /etc/environment
WARNING: Do not replace your environment file with the following content because you may already have different environment variables which are required by other applications to function properly. Notice the end of PATH variable and the M2_HOME variable.
After the modification, press Ctrl + O to save the changes and Ctrl + X to exit nano.

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 complete complex Maven commands by hitting Tab multiple times.
sudo wget --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.

Next Post »

Contact Form


Email *

Message *