How to Install Apache Maven on Ubuntu 16.04

Introduction #

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. Apache Maven provides a complete framework to automate the project’s build infrastructure.

In this tutorial, you will learn how to install Apache Maven on Ubuntu 16.04.

Prerequisites #

  • A newly deployed OneHost Cloud Ubuntu 16.04 server with all software up to date.
  • A non-root user with sudo privileges on your server.

1. Install Java #

Apache Maven requires Java, and Java is not available in Ubuntu’s repository by default. Add the Oracle Java PPA to apt with the following command:

$ sudo add-apt-repository ppa:webupd8team/java

Next, update your apt package database with the following command:

$ sudo apt-get update -y

Install the latest stable version of Oracle Java 8.

$ sudo apt-get install oracle-java8-installer

Verify the Java version by running the following command:

$ java -version

Output:

java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

2. Install Apache Maven #

This example shows version 3.6.3. Substitute the download URL for the most recent version of Apache Maven from the official website. Choose the “Binary tar.gz archive”.

$ cd /opt/
$ wget http://apache.mirrors.pair.com/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

Once the download has completed, extract the downloaded archive.

$ sudo tar -xvzf apache-maven-3.6.3-bin.tar.gz

Next, rename the extracted directory.

$ sudo mv apache-maven-3.6.3 maven 

3. Set Up Environment Variables #

Set up the environment variables M2_HOME and PATH by creating mavenenv.sh in /etc/profile.d/.

$ sudo nano /etc/profile.d/mavenenv.sh

Add the following lines:

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

Save and close the file, update its permissions, then load the environment.

$ sudo chmod +x /etc/profile.d/mavenenv.sh
$ source /etc/profile.d/mavenenv.sh

4. Verify installation #

Check the version of the Apache Maven.

$ mvn --version

The output will be similar to this, depending on your version.

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /opt/maven
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.13.0-32-generic", arch: "amd64", family: "unix"

You have successfully installed Apache Maven on Ubuntu 16.04.

Powered by BetterDocs