Home  »  ArticlesGuidesHow ToTechnologyTools   »   How to Install Maven on Ubuntu 22.04

How to Install Maven on Ubuntu 22.04

As we show you how to install Maven on Ubuntu 22.04, Apache Maven is a software project management and comprehension tool designed to can manage a project’s build, reporting and documentation from a central piece of information.

Maven aims to make the build process easy, provide a uniform build system, provide quality project information, and encourage better development practices. You can get full details here now or read it later.

Meanwhile we can proceed with the installation of Maven on Ubuntu.

Prerequisites

Apache Maven can be installed and configured on any system with Java installed. The latest Maven 3.3+ version requires JDK 1.7 or newer versions. However, it will still allow you to build against JDK 1.3 and other JDK versions by using Toolchains. About 10MB is required for the Maven installation itself with additional space for your local Maven repository which on average is at least 500MB.

Do This to Install Maven on Ubuntu 22.04

First update the apt package database by running this command:

$ sudo apt update

Run the following command to install JDK 11 which is the default on Ubuntu 22.04:

$ sudo apt install default-jdk

Once the Java JDK is installed, you can confirm the installed Java version by running:

$ java -version
Output:

openjdk 11.0.7 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

If you want to install a different version, usually the latest Java JDK on your system you can follow this tutorial on how to install OpenJDK 17 on Ubuntu.

Method 1: Installing Maven Using Apt

The easiest way to install Maven on Ubuntu is by using the Apt package manager. Run the following command in the terminal:

$ sudo apt install maven

Once completed, you can confirm your installation and version by running this command:

$ mvn -v

Method 2: Installing Latest Maven on Ubuntu From Source Code

The problem with installing Maven using Method 1 above is that you do not usually get the latest version. To install the latest Maven on Ubuntu systems, you can follow the steps outlined below.

1. Download Apache Maven from its official website using the following command. We will be downloading Apache Maven 3.8.6 in our examples.

$ wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz

2. Extract the downloaded Maven archive file using the following command below. (We will extract it into the /opt directory):

$ sudo tar xzf apache-maven-3.8.5-bin.tar.gz -C /opt

Optional: Create a symbolic link /opt/maven to that directory.

$ sudo ln -s /opt/apache-maven-3.8.5 /opt/maven 

3. Set the environments variables by creating new file using the commands below.

$ /etc/profile.d/maven.sh

Add these directives below to the file:

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

Save the file and close it.

4. Load the environment variables in the current shell using the command below. We do this so that at the next system reboot, the environment will automatically loaded.

$ source /etc/profile.d/maven.sh

If you reached this stage without any errors then you have successfully installed and configured Apache Maven on your Ubuntu system.

Check and confirm the installed Maven version using this command:

$ mvn -version

5. Clean up the system by removing the downloaded archive file using this command:

$ rm -f apache-maven-3.8.6-bin.tar.gz

Uninstall Apache Maven from Ubuntu

Like any software on your system, if it is no longer needed you can always remove it from the system. These steps will show you how to do it.

If you installed maven using the Apt package manager you can run the following command to uninstall it.

$ sudo apt remove --purge maven

If you installed maven from the downloaded source code, you can use the following commands to remove it.

$ sudo unlink /opt/maven 
$ sudo rm -rf /opt/apache-maven-3.8.5 
$ sudo rm -f /etc/profile.d/maven.sh

In Closing

This tutorial has shown you how to install Maven on Ubuntu 22.04 using the Apt package manager as well as from the latest source downloads from the official website.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, How To, Technology, Tools