How to Install Apache Maven on Ubuntu 20.04

install apache maven on ubuntu 20.04

Apache Maven is a powerful project management tool used for projects build, dependency and documentation. It is based on POM (project object model) that helps you to manage an entire project’s build process, including storing documents, reporting and more. It has built-in objective commands to deal with the packages and code composition. It is designed to build and manage Java based project by proficiently documenting and reporting the central project related information.

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

Prerequisites

  • An Ubuntu 20.04 VPS (we’ll be using our SSD 2 VPS plan)
  • Access to the root user account (or access to an admin account with root privileges)

Step 1: Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

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

Step 2: Install Java

Apache Maven is based on Java. So you will need to install Java in your system. You can install it with the following command:

apt-get install default-jdk -y

Once installed, verify the Java version with the following command:

java -version

You should get the Java version in the following output:

openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

Step 3: Install Apache Maven from Source

In this section, we will show you how to install Apache Maven from the source. This way you can install the latest version of Apache Maven in your system.

Download Apache Maven

First, download the latest version of Apache Maven to the /opt directory with the following command:

cd /opt
wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

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

tar xzf apache-maven-3.6.3-bin.tar.gz

Next, rename the extracted directory with the following command:

mv apache-maven-3.6.3 apachemaven

Setup Environment Variable

Next, you will need to setup environment variable to define the Java and Apache Maven path. To do so, create a new file named apachemaven.sh inside /etc/profile.d/ directory:

nano /etc/profile.d/apachemaven.sh

Add the following lines:

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

Save and close the file then give the execution permission:

chmod +x /etc/profile.d/apachemaven.sh

Next, activate the environment variable with the following command:

source /etc/profile.d/apachemaven.sh

Verify the Apache Maven

Now, you can verify the Apache Maven installation using the following command:

mvn -version

If everything is fine, you should get the following output:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apachemaven
Java version: 11.0.9.1, 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-29-generic", arch: "amd64", family: "unix"

Step 4: Install Apache Maven From APT

You can also install the Apache Maven from the Ubuntu 20.04 default APT repository.

First, update the repository cache with the following command:

apt-get update -y

Once your repository is up-to-date, you can install the Apache Maven using the following command:

apt-get install maven -y

After installing the Apache Maven, verify the Apache Maven installation using the following command:

mvn -version

You should get the following output:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.9.1, 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-29-generic", arch: "amd64", family: "unix"

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

installing apache maven on ubuntu 20.04

Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment