How to Install Apache Maven on Ubuntu 20.04

Install Apache Maven on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Apache Maven on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Apache Maven is an open-source project management software and comprehension tool used primarily for Java projects. Maven is built based on the concept of a project object model (POM), Maven can manage a project’s build, configuration details, the project’s dependencies, reporting and documentation from a central piece of information.

Install Apache Maven on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Java.

We install the Java OpenJDK, run the commands below:

sudo apt install openjdk-11-jdk

Verify our Java version installed on our system:

$ java --version

openjdk 11.0.7 2020-07-16
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+12-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Step 3. Install Apache Maven on Ubuntu system.

In this step, we will download the apache maven binary code using the wget command:

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

Next, run the commands below to extract the downloaded package:

cd /home/ramona
tar xzf apache-maven-3.6.3-bin.tar.gz

After the file has been extracted, the files will be located on /home/ramona/apache-maven_3.6.3 directory. then we will create a symbolic link:

sudo ln -s apache-maven-3.6.3 maven

Then, set the necessary environment variables by creating a new file with the following content:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export M2_HOME=/home/ramona/maven
export MAVEN_HOME=/home/ramona/maven
export PATH=${M2_HOME}/bin:${PATH}

Make the script executable by typing:

sudo chmod +x /etc/profile.d/maven.sh

Finally, load the environment variables using the following command:

source /etc/profile.d/maven.sh

Now run the commands below to check the version number:

$ mvn --version

Apache Maven 3.6.3 (cegama3002696d0bmwe462b541b8a6ba2883f)
Maven home: /home/ramans/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-46-generic", arch: "amd64", family: "unix"

That’s all you need to do to install Apache Maven on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.