How to install Apache Maven on Ubuntu 22.04

Apache Maven is a tool that helps the developers in developing different projects by making the process easy. It provides a uniform build system, and the best quality of project information. Apache Maven is used mainly for Java-based projects and it uses the XML file format by following the POM(Project Object Model). POM is used to find out the details of the project, configuration files, and their dependencies.

If we talk about the features of Apache Maven, then it is necessary to tell the readers that:

  • You can start the project and its modules using Apache Maven in a few seconds
  • Apache Maven allows you to work with multiple projects at the same time without having a delay in execution of your project
  • You can access new features of the Apache Maven in few seconds
  • It provides you the ability to write plugins in JAVA or any other programming language
  • It is also used with the projects of C programming language

In this article, we are going to install Apache Maven by different possible methods on Ubuntu 22.04 using the command-line interface.

How to install Apache Maven on Ubuntu

There are two different installation methods for Apache Maven on Ubuntu:

  • using the package available in the default repository of Ubuntu
  • by downloading its installation medium from its official website

Method 1: Installation of Apache Maven from the default repository on Ubuntu

The easy and most convenient method to install any package on Ubuntu is from its default repository if the package is available in it.

To install the Apache Maven from the repository, first, we will find out the available package of Apache Maven on the Ubuntu repository by using the command:

$ sudo apt show maven

To install the package, use the command provided below:

$ sudo apt install maven -y

To confirm the installation, check the version of installed Apache Maven using the command:

$ mvn --version

The latest stable version 3.6.3 is installed on Ubuntu 22.04 system.

Uninstallation of Apache Maven on Ubuntu 22.04

As we are supposed to install it with another method, so we will uninstall it using the purge option of the apt package manager:

$ sudo apt purge maven -y

Method 2: Installation of Apache Maven by downloading its installation medium on Ubuntu

Another method of installing the Apache Maven on Ubuntu is by downloading its package from the official website.

Step 1: The Installation of the JAVA package on Ubuntu

Java is a pre-requisite of the Apache Maven so to install the Java package on Ubuntu use the command typed-below:

$ sudo apt install default-jdk -y

To check the version of installed JAVA, we will use the command:

$ java --version

Step 2: Download the installation medium of Apache Maven from its official website

We will use the wget command to download the installation medium of the Apache Maven from its official website to the /tmp directory using the command:

$ wget -P /tmp

Apache Maven’s tar.gz file is downloaded and ready to be extracted.

Step 3: Extract tar.gz file

When the downloading is complete, extract the tar.gz file to the /opt directory using the command:

$ sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt

Step 4: Set the environment variables on Ubuntu

First, create a new file using nano editor:

$ sudo nano /etc/profile.d/

Copy the script mentioned below and paste it into the newly created file:

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
export MAVEN_HOME=/opt/maven

Once added, save and close the file by using CTRL+S and CTRL+X shortcut keys.

Step 5: Load the environment variables on Ubuntu

When the script is saved, make the script executable by using the command:

$ sudo chmod +x /etc/profile.d/

Load the environment variables to install the Apache Maven on Ubuntu using the command:

$ source /etc/profile.d/

Step 6: Verify the installation of Apache Maven on Ubuntu

To confirm the installation of Apache Maven, we will again check its version using the command:

$ mvn -version


Apache Maven is the best tool for the developers that help to minimize their efforts and time by helping them in making the projects, especially of Java. in this write-up, Apache Maven has been installed by different two different methods on Ubuntu.