How to Install Apache Maven on Ubuntu 24.04


Apache Maven is the project management tool for the Java Ecosystem. Maven is based on a Project Object Model which is an XML file containing the build process, dependencies, structure, plugins, and any other configurations relevant to that project.

It automates the project, ensures consistency throughout the project, and provides the ease for developers to just focus on writing code instead of fulfilling other requirements. Moreover, the cross-platform availability of Maven makes it more favorable among developers who have to work on multiple platforms.

In today’s guide, we will demonstrate the methods to install Apache Maven on Ubuntu 24.04.

Outline

How to Install Apache Maven on Ubuntu 24.04

Apache Maven is available as a whole project on the Ubuntu default repositories and its source binaries are also available on the official website. Let’s start with the default repositories method:

Method 1: Ubuntu Default Repositories

The default repositories of the Ubuntu might not get you the latest but have the tested Maven for that Ubuntu release. Maven requires JDK for its work which is already equipped with the “maven” package in the default repositories. Thus, you do not have to install JDK explicitly.

Step 1: Update the Core Libraries of Ubuntu 24.04

Get the latest list of packages on your default repositories:

sudo apt update

Step 2: Install Maven

Now, install Maven using the following command:

sudo apt install maven

To confirm the installation, you can launch it as follows or check the Maven version:

The version “3.8.7” is installed via the default repositories method. Let’s check the second method, now:

Method 2: Maven Binaries | Available on the Official Maven Website

Since the Maven is a Java-based project thus it requires a specific version of JDK to be there. For instance, the Maven 3.9 or higher versions require JDK 8+. Thus, before going for Maven, you must have supported JDK installed on your system.

Step 1: Install JDK

First, update the packages list using the “sudo apt update” command and then install JDK using the command:

sudo apt install jdk

Step 2: Download the Binaries of Apache

Get the latest available binary package from the Maven Download Page:

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

Step 3: Extract the Package

Use the tar with the “xf” flag to extract the newly downloaded “tar.gz” file:

sudo tar xf <Package-Name>

Step 4 (Optional): Create a Symbolic Link for the Executable

To make the newly installed Maven recognizable by the whole system, you need to map the executable (in the new directory) to the location (/usr/bin/mvn). Here’s the command:

sudo ln -s <current-executable> /usr/bin/mvn

Step 5: Configure the Environment Variables

Right now, if you try to run the “mvn” command, you won’t be able to get the result as shown below:

This is because the system can only access the executable but is unable to recognize the maven configurations. For that, you have to add some environment variables for the error-free execution of maven.

It is recommended to create a shell script in the “/etc/profile.d” directory and add the relevant environment variables inside it. For that, create/open a shell script:

sudo nano /etc/profile.d/mavenev.sh

All these variables are called when you run the “mvn” command to scan for projects. Here’s the breakdown of the variables:

  • “JAVA_HOME”: Put the location where the java/jdk is installed.
  • “MAVEN_HOME”: Where the maven directory is extracted (path of the main directory after extraction that contains all the executables/configurations).
  • “PATH”: The environment variable that contains the path of the executables. So, put the executable location of maven in it. If you have created the symbolic link of the executable, then you can skip this as well.

Make the script executable:

sudo chmod +x /etc/profile.d/mavenev.sh

Reload the shell session or source the shell script:

source /etc/profile.d/mavenev.sh

Now, when you run the “mvn” command from the terminal. You will get the following output:

Look at the version, i.e., “3.9.6”. It is the latest as compared to the version installed via the default repositories method, i.e., “3.8.7”.

That’s how you can install Apache Maven on Ubuntu 24.04.

How to Remove Apache Maven From Ubuntu 24.04

The removal of Maven depends on the installation method. Here, we will list both the methods to remove it from Ubuntu 24.04:

If Maven is Installed From the Default Repositories | Method 1

If you have installed Apache Maven from the default repositories then you can remove it using one one-liner command:

sudo apt autoremove maven --purge

This command removes any unused dependencies and the configurations relevant to maven:

If Maven is Installed From the Binaries | Method 2

You have to remove the maven in a sequential manner, i.e., start removing from the last installation step. Let’s understand it:

First, remove the environment variables file using the rm command:

sudo rm <File/Script-Path>

After that, you need to remove the directory where the maven executables/configurations are extracted:

sudo rm -r <Path-of-Extracted-Directory>

Now, your system won’t recognize Maven anymore. That’s how it is removed completely.

Bottom Line

Apache Maven is available on the default repositories and can be installed from the binaries on the official website. To install maven from default repositories, use the “sudo apt install maven” and you will get version “3.8.7” (at the time of writing). While, when you install the Maven from the binaries obtained from the official website, you will get version “3.9.6” (when writing this) which is the latest.

Thus, if you want to get the latest/updated Maven then follow the binaries method.

This post has demonstrated both methods to install Apache Maven on Ubuntu 24.04.

Print Friendly, PDF & Email
Categories