24.07.2025

How to Install Apache Maven on Ubuntu 20.04 (Step-by-Step Guide)

Introduction

Apache Maven is toolset disigned for automate software building. Generally it is used for Java projects but Maven could be easily extended to build different project types and development tasks. Maven will "close" two critical "pains" of software builders: first, it describes how the software is constructed, and second, it describes its dependencies. It employs standard practices for the building process and encompasses an extensive array of plugins for its fundamental capabilities.


Prerequisites

Don't hurry to start the installation procedure, renew system packages before:

apt-get update; apt-get upgrade -y

Maven needs Java, its presence can be verified by the follow command:

java -version

In case you couldn't find Java on your system, you can install it with this operation:

apt install -y default-jdk

Then check version again. We expect you see picture like this:

Open the download page in your browser and get the latest version (3.9.2 on the June 2023).

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

Installation process

Unpack the downloaded archive:

tar xf apache-maven-3.9.2-bin.tar.gz


Next, move the result to the /opt directory:

mv apache-maven-3.9.2 /opt/maven

Set correct ownership:

chown -R root:root /opt/maven

Environment Variables editing

Being that Maven is now installed in the /opt/maven directory, we should add some environment variables to enable Maven execution from the command line:

cat <<EOF >> /etc/profile.d/mymavenvars.sh
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
EOF
ln -s /opt/maven/bin/mvn /usr/bin/mvn

Then you should change script's permissions:

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

Then rebooting the machine or run this command:

source /etc/profile.d/mymavenvars.sh

Check the installation result

To check the correctness, run this command:

mvn --version

We expect that you see picture like this, its indicates that everything is OK:

First app building

Lets illustrate our first app build process.  First, create your project app and "fall" inside:

mkdir myproject; cd myproject

Remember, Maven uses special file called pom.xml to build your artifact (special termin meant Maven's work result). So, lets create this:


cat <<eof>> pom.xml
<project>
<modelversion>4.0.0</modelversion>
<groupid>instr.serverspace.io</groupid>
<artifactid>you_are_the_best</artifactid>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version>3.8.0</version>
<configuration>
<release>1</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
EOF
</eof>

You also can download this file here. Then start building process itself:

mvn package

Result should be in target subdirectory:

Conclusion

After reading this guide, you should now be able to install Apache Maven on Ubuntu 20.04 using the binary archive method, configure the necessary environment variables, and verify that the installation was successful. For more advanced usage, make sure to consult the official Apache Maven documentation to explore its rich set of features, lifecycle phases, and an extensive library of plugins tailored for various project types and development tasks.

FAQ - Installing Maven on Ubuntu 20.04