How to Install Apache Maven on Ubuntu and Debian

Apache Maven is a free software application management and build automation platform based on the conception of a project object model (POM), which is especially used for deploying Java-based projects, but can also be used on applications written in C#, Ruby and other programming languages.

Read Also: How to Install Apache Maven in CentOS 7

In this article, I will explain how to install and configure latest version of Apache Maven on Ubuntu and Debian distributions along with Java 8 from the PPA Repository.

Prerequisites

  • A newly deployed or existing Ubuntu or Debian server instance.
  • Java Development Kit (JDK)Maven 3.3+ require JDK 1.7 or above to run.

Install OpenJDK 8 in Ubuntu & Debian

Java Development Kit (JDK) is a essential requirement to install Apache Maven, so first install Java on Ubuntu and Debian system using the third-party Java PPA Repository and verify the version using following commands.

$ sudo apt install software-properties-common apt-transport-https -y
$ sudo add-apt-repository ppa:webupd8team/java -y
$ sudo apt-get update -y
$ sudo apt-get install oracle-java8-installer
$ java -version

If installation went well, you see the following output.

java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

Install Apache Maven in Ubuntu & Debian

Next, visit the Apache Maven download page and grab the latest version of apache maven binary code or use the following wget command to download it under the maven home directory ‘/usr/local/src’.

$ sudo cd /usr/local/src
$ sudo wget http://www-us.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz

Extract the downloaded archive apache-maven-3.5.4-bin.tar.gz file, and rename the directory to ‘apache-maven’ using following commands.

$ sudo tar -xf apache-maven-3.5.4-bin.tar.gz
$ sudo mv apache-maven-3.5.4/ apache-maven/ 

Configure Apache Maven Environment

Now we will configure the environments variables for Apache Maven files on our system by creating a new configuration file ‘maven.sh’ in the ‘/etc/profile.d’ directory.

$ sudo cd /etc/profile.d/
$ sudo nano maven.sh

Add the following environment variables in ‘maven.sh’ configuration file.

# Apache Maven Environment Variables
# MAVEN_HOME for Maven 1 - M2_HOME for Maven 2
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export M2_HOME=/usr/local/src/apache-maven
export MAVEN_HOME=/usr/local/src/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Now make the ‘maven.sh’ configuration file executable and then load the configuration by running the ‘source’ command.

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

Check Apache Maven Version

To verify Apache Maven installation, run the following mvn command.

$ mvn --version

If installation went well, you see a output similar to the following.

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-07-14T19:33:14+01:00)
Maven home: /usr/local/apache-maven
Java version: 1.8.0_171, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "4.17.6-1.el7.elrepo.x86_64", arch: "amd64", family: "unix"

That’s It! You have successfully installed Apache Maven 3.5.4 on your Ubuntu and Debian system. If you have any problems related to installation, do share with us in the comment section.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

5 Comments

Leave a Reply
  1. Hi, I have these error messages:

    The JAVA_HOME environment variable is not defined correctly
    This environment variable is needed to run this program
    NB: JAVA_HOME should point to a JDK not a JRE

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.