Apache Maven is a free, open-source project management software that manages the creation, reporting, and documentation of a project from a central location. It is based on the concept of a project object model and is used in particular for the deployment of Java-based projects. Apache Maven facilitates the daily work of Java developers and generally helps to understand a Java-based project. You can easily integrate your project with Subversion or Git. Maven can also be used to create and manage projects written in C#, Ruby, Scala, and other languages.

In this tutorial, I will explain how to install Apache Maven on Ubuntu 20.04 server.

Requirements

  • A server running Ubuntu 20.04.
  • A root password is set up on your server.

Getting Started

The commands in this tutorial must be run with root privileges. To become the root user, run this command:

sudo -s

and enter your sudo password when requested.

Before starting, you will need to update your system with the latest version. You can do this by running the following command:

apt-get update -y
apt-get upgrade -y

Once your system is updated, restart the system to apply the changes.

Install Java JDK

Maven requires Java JDK to be installed on your system. By default, the Java Development Kit (JDK) 11 is available in the Ubuntu 20.04 default repository. You can install it by running the following command:

apt-get install default-jdk -y

Once Java is installed, you can check the version of Java with the following command:

java -version

You should see the following output:

openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13 8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13 8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Install Apache Maven

First, you will need to download the latest version of Apache Maven from their official website. You can download it with the following command:

cd /tmp
https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf apache-maven-3.8.4-bin.tar.gz

Next, copy the extracted directory to the /opt/ directory with the following command:

cp -r apache-maven-3.8.4 /opt/maven

Next, you will need to set up the environment variables for Java and Maven. You can do this by creating maven.sh file:

nano /etc/profile.d/maven.sh

Add the following lines:

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

Next, give proper permission to the maven.sh file with the following command:

chmod 755 /etc/profile.d/maven.sh

Finally, load the environment variables by running the following command:

source /etc/profile.d/maven.sh

You can now check the Maven installation by running the following command:

mvn -version

You should see the following output:

Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /opt/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-28-generic", arch: "amd64", family: "unix"

Congratulations! you have successfully installed Apache Maven on Ubuntu 20.04 LTS server.