How To Installing Maven on Ubuntu

What is Maven?

Maven, by Apache, is a tool used for project management and comprehension, mostly Java-based projects. It is based on POM (Project Object Model). Maven can simplify the build process of your project. It might seem like Apache ANT but is a lot more advanced than ANT as conventions are used for the build procedures in Maven, so the only thing that needs to be written down now are exceptions. There are XML files in Maven which describe the project dependencies, the built order, required plug-ins, directories and so on.

This XML file is also called ‘pom.xml’.

Why Maven?

  • It is fairly simple to set up a project on Maven.
  • Very efficient space configuration. Doesn’t take much space for projects.
  • Easy to work on multiple projects at once.
  • No third party libraries.
  • Little or no configuration required to upgrade to new features.
  • Easy to integrate projects with the source control system.
  • Easy and fast debugging and deploying.
  • Easy to build projects into predefined output types such as JAR, WAR and others.

Installation

Check if your machine has Java installed or not. To install Maven 3.3 or later, you should have JDK 1.4 or later installed on your machine. Use SSH, access your VPS and update your package index before installing Java:

sudo apt update

Now you have two ways to install Maven on Ubuntu. Either you can use the Apache Maven official website or go with apt. It is much easier to use the latter. However, we’ll show you how to do it either ways and then you can make the choice.

-using the official website

  1. Use the following command to install the default open JDK for your Ubuntu:
apt-get update

sudo apt install default-jdk

  1. Use the following command to verify and check the version once the installation is complete:
sudo apt install default-jdk

  1. The following command can be used to download the latest version of Maven from the official website and saving it /tmp directory:
wget https://www-us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz -P /tmp

  1. Once downloaded, it can be extracted to the /opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt


You’ve successfully downloaded and extracted Maven. Now all you need to make sure is that it’s working properly. Some environment variables like JAVA_HOME, M3_HOME, MAVEN_HOME and PATH need to be configured for Maven to work appropriately.

  1. Using the following command will create a file named maven.sh in the directory /etc/profile.d/ :
sudo vi /etc/profile.d/maven.sh

  1. Add the following configuration to this new file:
export JAVA_HOME=/usr/lib/jvm/default-java

export M3_HOME=/opt/maven

export MAVEN_HOME=/opt/maven

export PATH=${M2_HOME}/bin:${PATH}

  1. After adding the configurations, save the file by hitting the esc key on the keyboard, type :wq and enter the following command to provide these privileges to the file:
sudo chmod +x /etc/profile.d/maven.sh

  1. Now you’ll have to refresh and load the environment variables. Use the following command for this:
source /etc/profile.d/maven.sh


And tadaa! You have successfully installed Maven in your Ubuntu.

-using Apt

As we already mentioned, this is a much easier method to install Maven on Ubuntu. This is because by default Maven packages are present in Ubuntu but you’ll still need to ensure that they are the latest.

  1. Here also you’ll have to use the following command to install the default open JDK for Ubuntu:
apt-get update

sudo apt install default-jdk

  1. Use the following command to verify and check the version once the installation is complete:
sudo apt install default-jdk

  1. Use the following command to update the package index:
sudo apt-get update

  1. The following command will install Maven in /usr/share/Maven and /etc/Maven:
sudo apt-get -y install maven

  1. Use the following command to verify installation and check version:
mvn -version


Yay! You have successfully installed Maven on Ubuntu using the Apt method.

Was this article helpful?