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"
also, append the bin directory to the PATH variable:
/opt/apache-maven-$maven_version/bin

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 for other applications to function properly. Notice the end of PATH variable and the M2_HOME variable.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk-10.0.2/bin:/opt/apache-maven-$maven_version/bin"
JAVA_HOME="/usr/lib/jvm/jdk-10.0.2"
M2_HOME="/opt/apache-maven-$maven_version"
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 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 *