Bobcares

How to install and configure Apache Maven on Ubuntu 18.04?

by | Sep 22, 2020

Install of Apache Maven from the default Ubuntu repository often result in installing an older version of Maven.

As a part of our Server Management Services, we help our Customers with software installations regularly.

Let us today discuss the steps to install Apache Maven on Ubuntu

How to Install Apache Maven on Ubuntu?

Apache Maven can be installed either using apt or from the official website. Let us look at each of these methods in detail.

Install Apache Maven via Apt

The default Ubuntu package repository includes a package for Maven, and we can install it easily with the apt command. Prior to the package install, it is a good idea to update the existing packages in the system.

root@ubuntu:~# apt update -y

After the update, we can install Maven with the command below:

root@ubuntu:~# apt install maven -y

Now, to verify the installation, run the following command to check the version of Maven on the system:

root@ubuntu:~# mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-91-generic", arch: "amd64", family: "unix"

 

Install Apache Maven via Download

Often the version of Maven within the Ubuntu repository is not the latest one. To obtain the most up-to-date version of Maven, we can install it after downloading the installation file from the Apache Maven website.

This method includes the following tasks:

  • Install OpenJDK
  • Download Maven
  • Extract Maven and Symlink
  • Configure Environment Variables
  • Verify Maven Installation

To proceed with this method, we need to install OpenJDK as Maven 3.3+ requires JDK 1.7 or above.

To install java, initially, we need to update the packages as we did earlier. Once the packages are up to date, install the OpenJDK package by typing:

root@ubuntu:~# apt install default-jdk

Now, verify the installation by running the following command:

root@ubuntu:~# java -version
Download Maven

The second task in this method is to download Maven into the /tmp directory utilizing the following command:

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

Next, extract the newly downloaded Maven archive to the /opt directory:

root@ubuntu:~# tar xf /tmp/apache-maven-3.6.3-bin.tar.gz -C /opt

To make it more straightforward to upgrade in the future, create a symlink to the Maven installation directory:

root@ubuntu:~# ln -s /opt/apache-maven-3.6.3 /opt/maven
Configure Environment Variables

To configure environment variables, initially, we need to create a maven.sh file inside the /etc/profile.d/ directory:

root@ubuntu:~# vim /etc/profile.d/maven.sh

Now, copy the following configuration out and paste it into the maven.sh file:

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

Then, make the script executable with chmod:

root@ubuntu:~# chmod +x /etc/profile.d/maven.sh

Finally, to get these variables into play in the environment, run the source command on the file:

root@ubuntu:~# source /etc/profile.d/maven.sh
Verify Maven Installation

Finally to verify that Maven is installed properly, run the version command and check the output:

root@ubuntu:~# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-91-generic", arch: "amd64", family: "unix"

 

[Need any further assistance in installing Apache Maven on Ubuntu? – We’re available 24*7]

Conclusion

In short, Apache Maven is a popular build management tool for Java projects. Today we saw two different method of how our Support Engineers install Apache Maven on Ubuntu.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.