How to Install Apache Maven on Ubuntu 24.04 LTS or 22.04/20.04

Maven is an open-source tool hosted by Apache to manage Java projects. It contains a Project Object Model (POM): a config file (XML) that contains essential information, including configurations, project dependencies, etc.

Maven can handle project dependencies, reporting, documentation, run tests, compile source code into binary code, package the source code, and many more. It automates all these tasks to minimize the risk of human error while building projects.

Moreover, Maven works well for Java projects and is suitable for C, C#, Scala, Ruby, and other programming languages. Apache Maven can be a fantastic tool for working on Java projects. It’s time to move forward and find simple ways to install Maven on Ubuntu.

There are two ways to install the Maven on Ubuntu 24.04 or other versions such as Ubuntu 22.04 or 20.04- one is with the APT package manager and the other by directly downloading its Tarball file. Here we show you how to use both.

#1st method:

Installing Maven on Ubuntu Linux using APT

Let’s start with installing Java JDK, as Apache Maven is primarily used for Java. That’s why please run the below command to update your system and then install JDK:

sudo apt update && sudo apt upgrade -y
sudo apt install default-jdk
install default jdk

Now, install you can install Maven by executing the below command:

sudo apt install maven -y
install Maven using Ubuntu 24 APT package manager

Once you are done, it is time to check the version of the currently installed version of Apache Maven:

mvn --version

As the above image shows, Apache Maven 3.6.3 is currently installed on the system. However, you can download and install the latest version of Maven.

Check MVN version

#2nd Method:

Install the Latest Version of Apache Maven via Tarball

First, open the official Apache Maven website and download the apache-maven-3.9.7-bin.tar.gz file.

Webiste to download Maven binary

Those who want to use the command line to download the file can right-click on the file and copy the link address to download it using the wget command:

Copy maven file link

Syntax:

wget paste-link -P Downloads

Example:

wget https://dlcdn.apache.org/maven/maven-3/3.9.7/binaries/apache-maven-3.9.7-bin.tar.gz -P Downloads

After downloading the file, you must extract the Maven tar.gz in the /opt directory. Hence, run the command below to open the directory in the terminal where you downloaded the tar.gz file.

For example, we have downloaded the file in the Downloads directory:

cd ~/Downloads
ls
Switch to downloads directory

Now, run the following command to extract the file in the opt directory which is used to store optional packages:

tar xf apache-maven-*.tar.gz

Once you extract the file, move it to /opt. Note: Change Apache-maven–3.9.7 in the given command with the version of your extracted Maven.

sudo mv /opt/apache-maven-3.9.7 /opt/maven

Now, you need to set the environment variable so that you can use Maven from the terminal. Create and open the maven.sh using the below command

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

In the maven.sh file, add the below-given information to make Maven executable:

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

After saving the file, run the below command to give executable permission to the maven.sh file:

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

Finally, load the maven.sh file, and now you can use the latest version of Maven:

source /etc/profile.d/maven.sh

Let’s check the currently installed version of Apache Maven with the following command:

mvn --version

Create your first project

Well, those who are familiar with Maven would already know what to do to create a project, whereas the new ones can check out the official Apache Maven website documentation to know more about it. Nevertheless, just to give an idea of how to start with this tool, here are the commands:

Create a project

Paste the given commanding your terminal. You replace the group ID, artifact ID, and other values as per your choice.

——————————————————————————————————–

———————————————————————————————————

  • -DgroupId=com.example: Specifies the group ID for the project. This is typically your organization’s domain name in reverse.
  • -DartifactId=my-first-maven-project: Specifies the artifact ID, which is the name of the project.
  • -DarchetypeArtifactId=maven-archetype-quickstart: Specifies the archetype to use, which provides a simple project structure.
  • -DinteractiveMode=false: Disables interactive mode and uses the provided arguments to generate the project.

This command creates a directory named my-first-maven-project with a basic Maven project structure.

Switch to your create app project directory, here it is as per the above command- my-first-maven-project

cd my-first-maven-project

Compile and Run:

To compile the project, use the following Maven command:

mvn compile

To execute the project’s main class (App.java), you need to package it into a JAR file first.

mvn package
Create your first project Apache maven Ubuntu 22.04

To test the newly compiled Jar files:

This command creates a JAR file in the target directory. To run the JAR file, use the java -cp command, specifying the JAR file and the main class:

java -cp target/my-first-maven-project-1.0-SNAPSHOT.jar com.example.App

You should see the output from the App class, which by default is “Hello World!”.

To create a site using POM.XML, run the given command inside your app directory:

mvn site

Once done, run

firefox target/site/index.html
Installing Apache Maven on Ubuntu 24.04

Uninstall or Remove Apache Maven

In case you don’t want Maven on your system anymore, then as per the method, you have used to install it on Ubuntu 22.04, select one of the given ones to remove the same.

#1st method: For those who have used APT package manager:

sudo apt remove maven*

#2nd Method: Those who manually installed it:

sudo rm /usr/share/maven

To remove Java as well:

sudo apt autoremove default-jdk --purge

A Quick Wrap-up

For developers, Apache Maven is a fantastic tool for managing your software projects from top to bottom. With this tool, you do not need to worry about project documentation, development, and distribution. This blog has everything you need to learn to install Apache Maven on Ubuntu, including its latest version from the official website.

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.