Web Servers

How to Install Apache Maven on Ubuntu 20.04

In this article, we will see how to install Apache Maven on Ubuntu 20.04. We will install Apache Maven 3.6.3 version.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Apache Maven is a free and open source project management tool used for Java projects. You can easily manage a project’s build, reporting, and documentation from a central piece of information using Apache Maven.


System Requirements

  • Maven 3.3+ require JDK 1.7 or above to execute.
  • No minimum memory requirement.
  • No minimum OS requirement. Start up scripts are included as shell scripts and Windows batch files.

Step 1 – Keep your server up to date

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

Step 2 – Install Java

Next, we will install OpenJDK 11.

# apt-get install -y default-jdk

To verify the installation, check the version using following command:

# java -version

The output will be similar to the following:

openjdk version “11.0.8” 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Step 3 – Install Apache Maven

We will change our current working directory to /opt/ directory:

# cd /opt/

We can download the latest stable version of Apache Maven from the official website: using wget command. In case, wget command is missing, you can install it using # apt-get install wget -y command.

# wget https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

Once the download has completed, extract the downloaded archive:

# tar -xvzf apache-maven-*tar.gz

Now, rename the extracted directory using following command:

# mv apache-maven-3.6.3 maven

Step 4 – Setup environment variables

Next, we will need to setup the environment variables such as M2_HOME, JAVA_HOME and PATH. You can do this by creating a mavenenv.sh file inside of the /etc/profile.d/ directory:

# vi /etc/profile.d/mavenenv.sh

Add following lines:

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

Save and close.

Make the file executable:

# chmod +x /etc/profile.d/mavenenv.sh

Now you can load the environment variables:

# source /etc/profile.d/mavenenv.sh

Step 5 – Verify the installation

Once everything has been successfully configured, check the version of Apache Maven:

# mvn –version

The output will similar to the following:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 11.0.8, 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-51-generic”, arch: “amd64”, family: “unix”

We have successfully installed Apache Maven.

In this article, we have seen how to install Apache Maven on Ubuntu 20.04.

Related Articles