How to Install Apache Maven on Ubuntu 22.04

Maven is a free, open-source, popular build tool developed by the Apache Group. It is used to build, publish, and deploy several projects simultaneously for better performance. It is based on the concept of a project object model (POM) and is used to manage projects written in C#, Ruby, Scala, and other languages. Maven supports test-driven development, and its declarative configuration and other plugins make it a popular option for CI/CD.

This tutorial will show you the installation of Apache Maven on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Install Apache Maven From the APT Repository

By default, the Apache Maven package is included in the Ubuntu 22.04 default repository. You can install it by running the following command:

apt install maven -y

Once the Apache Maven is installed, you can verify its version using the following command:

mvn -version

You should see the Apache Maven version in the following output:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.15, vendor: Private Build, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-30-generic", arch: "amd64", family: "unix"

Install Apache Maven from Source

The latest version of Apache Maven is not included in the Ubuntu 22.04 default repository by default. So it is a good idea to install the latest version of Apache Maven from the source.

Install Java JDK

Apache Maven is a Java-based application. So you will need to install the Java JDK to your server. You can install it with the following command:

apt install default-jdk -y

After the successful installation, verify the Java version using the following command:

java --version

You should see the following output:

openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Install Apache Maven

Next, download the latest version of Apache Maven using the following command:

wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz

Once the download is completed, extract the downloaded file to the /opt directory:

tar xzf apache-maven-3.8.5-bin.tar.gz -C /opt/

Next, navigate to the /opt directory and rename the extracted directory:

cd /opt/
mv apache-maven-3.8.5 maven

Next, you will need to create a maven.sh file to export the Apache Maven path.

nano /etc/profile.d/maven.sh

Add the following lines:

export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Save and close the file then activate the Maven environment with the following command:

source /etc/profile.d/maven.sh

Next, verify the Apache Maven version using the following command:

mvn -version

You should see Apache Maven in the following output:

Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /opt/maven
Java version: 11.0.15, vendor: Private Build, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-30-generic", arch: "amd64", family: "unix"

Conclusion

In this post, we explained how to install Apache Maven using two methods. You can now choose your preferred way to install Apache Maven on Ubuntu 22.04. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)