How to Install Maven on Ubuntu

February 10, 2022

Introduction

Apache Maven is an open-source project management tool primarily used for developing Java applications. Maven incorporates a POM (Project Object Model) approach, which means it uses an XML file to store information about projects, configurations, and dependencies.

This tutorial explains how to install Maven on Ubuntu 20.04, both using the official repository and the installation file on the Maven website.

How to install Maven on Ubuntu

Prerequisites

  • A system running Ubuntu 20.04.
  • A working Internet connection.
  • Access to an account with sudo privileges.
  • Access to the terminal window.
  • Access to a text editor such as Nano.

Install Maven on Ubuntu with apt

The apt command provides a simple and straightforward way of installing Maven on Ubuntu. Follow these steps to complete the installation:

1. Update the system repository using:

sudo apt update

2. Install Maven from the official repository:

sudo apt install maven

3. When prompted, type Y and press Enter to confirm the installation.

Confirm the Maven installation when prompted

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

mvn -version

If successful, the output will look like this:

Check the current version of Maven to verify the installation

Install the Latest Release of Maven on Ubuntu

Manually installing Maven is more complex than using the apt command but offers more flexibility when choosing which version to install.

Step 1: Install OpenJDK

1. Update the system repository with:

sudo apt update

2. Install the latest version of OpenJDK by using:

sudo apt install default-jdk

3. Type Y and press Enter when prompted to confirm the installation.

Confirm the OpenJDK installation when prompted

4. Verify the installation by checking the current version of OpenJDK:

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

Step 2: Download and Install Maven

1. Visit the Maven download page and select the version of Maven you want to install. The latest version is listed in the Files section, and you can access earlier versions by using the archive link in the Previous Releases section.

We are using version 3.8.4, which is the latest version at the time of writing this article.

2. Download the Maven install file in the /tmp directory:

wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz -P /tmp
Download the Maven installation file

Note: Learn more about using the Linux wget command.

3. Once the download is complete, extract the installation file in the /opt directory:

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

4. Create a symbolic link called maven leading to the Maven installation directory:

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

Step 3: Set Up Environment Variables

1. Use the Nano text editor 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}
Create the maven.sh script file

3. Press Ctrl + X, then type Y and press Enter to save changes to maven.sh.

4. Make the maven.sh file executable using the chmod command:

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

5. 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
Check the current version of Maven to verify the installation

Conclusion

After reading this tutorial, you should have a copy of Maven installed on your Ubuntu system and ready to use.

If you are interested in trying Maven on a different Linux distribution, have a look at our guides on installing Maven on Debian 9 and installing Maven on CentOS 7. For Windows users, refer to our guide on installing Maven on Windows.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
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
June 19, 2019

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