How to Install Maven on Ubuntu

April 30, 2024

Introduction

Apache Maven is an open-source project management tool primarily used to develop Java applications. It incorporates a POM (Project Object Model) approach, which stores information about projects, configurations, and dependencies in an XML file.

This tutorial explains how to install Maven on Ubuntu using the official repository or the installation file on the Maven website.

How to install Maven on Ubuntu.

Prerequisites

Install Maven on Ubuntu with APT

The apt package manager provides a straightforward way to install Maven on Ubuntu. However, the user is limited to the Maven version currently available in the repository.

Follow these steps to install Maven with APT:

1. Update the local package repository index:

sudo apt update

2. Install Maven from the official Ubuntu repository:

sudo apt install maven -y

3. Check the current Maven version to verify the installation:

mvn -version

If successful, the output shows the program version.

Check the current version of Maven to verify the installation.

Download and Install Maven's Recent Version on Ubuntu

Installing Maven by downloading the installation file from the official website is more complex than using the apt command, but it enables users to install the latest available version. Follow the steps below to install Maven manually.

Step 1: Install OpenJDK

OpenJDK is an open-source Java implementation that is a Maven dependency on Ubuntu. Proceed with the following steps to install OpenJDK on the system:

1. Update the system's package repository index:

sudo apt update

2. Install the latest OpenJDK version by running:

sudo apt install default-jdk -y

3. Verify the installation by checking the current OpenJDK version:

java -version
Verify the installation by checking the current version of OpenJDK.

Step 2: Download and Install Maven

The latest Maven version is listed in the Files section of the official website. Earlier versions are available in the Previous Releases section. Follow the procedure below to download and install Maven on an Ubuntu system:

1. Visit the Maven download page.

2. Right-click the version of Maven you want to install and copy the link.

Copying the link to the latest Maven version.

3. Download the Maven installation file to the /tmp directory using the wget command. The syntax is:

wget [link] -P /tmp

Replace [link] with the link to the Maven version you copied in the previous step.

Download the Maven installation file

4. Once the download is complete, extract the installation file to the /opt directory:

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

5. Create a symbolic link leading to the Maven installation directory:

sudo ln -s /opt/apache-maven-[version] /opt/maven

Replace [version] with the Maven version you downloaded. For example:

sudo ln -s /opt/apache-maven-3.9.6 /opt/maven

Step 3: Set Up Environment Variables

1. Use a text editor like Nano to create and open the maven.sh script file in the /etc/profile.d/ directory:

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

2. Add the following lines to the maven.sh 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}
Creating the maven.sh script file.

Save the file and exit.

3. Use the chmod command to make the maven.sh file executable:

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

4. Execute the maven.sh script file with the source command to set up the new environment variables:

source /etc/profile.d/maven.sh

Step 4: Verify Maven Installation

Check the current version of Maven to verify the installation:

mvn -version

The example output below shows the 3.9.6 version of Maven installed on the system.

Check the current version of Maven to verify the installation.

Conclusion

After following this tutorial, you should have a copy of Maven installed and ready to use on your Ubuntu system. The article explained the steps for installing Maven via the official Ubuntu APT repository and the official website.

If you want to try Maven in a different environment, check out our guides on installing Maven on Debian 9 and installing Maven on Windows.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
What is Jenkins?
January 20, 2022

This article explains how Jenkins integrates into the CI/CD pipeline as well as some basic terminology needed to understand how Jenkins works.
Read more
Jenkins Tutorial: Basics for Beginners
January 13, 2022

Jenkins seems overwhelming when just starting out. Follow our guide to learn how to set up and use Jenkins through hands on tutorials.
Read more
13 Best Java IDEs {With Pros and Cons}
November 10, 2021

Java IDE bundles all the useful tools for writing, debugging, and testing your Java code. Take a look at the best Java IDEs you can use.
Read more
How to Install Java on Ubuntu
April 30, 2024

Java is one of the most popular programming languages used for developing anything from lightweight mobile to desktop applications. This step-by-step guide shows you how simple it is to install Java on Ubuntu.
Read more