Read in 4 minutes

last updated 

How to Install Apache Maven on Debian 9

Apache Maven is an open source project management and comprehension tool used primarily for Java projects. Maven uses a Project Object Model (POM) which is essentially a XML file containing information about the project, configuration details, the project’s dependencies, and so on.

In this tutorial we will show you two different ways to install Apache Maven on Debian 9.

The official Debian repositories contains Maven packages that can be installed with the apt package manager. This is the easiest way to install Maven on Debian, however the version included in the repositories is always several releases behind the latest version of Maven.

To install the latest version of Maven follow the instructions provided in second part of this article where we will be downloading Maven from their official website.

Choose one of the installation methods that will work best for you.

Prerequisites

In order to be able to install packages on your Debian system, you must be logged in as a user with sudo privileges.

Installing Apache Maven on Debian with Apt

Installing Maven on Debian using apt is a simple, straightforward process.

  1. First, update the package index:

    sudo apt update
  2. Install Maven by running the following command:

    sudo apt install maven
  3. Verify the installation by typing:

    mvn -version

    The output should look something like this:

    Apache Maven 3.3.9
    Maven home: /usr/share/maven
    Java version: 1.8.0_181, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "4.9.0-8-amd64", arch: "amd64", family: "unix"

That’s it. Maven is now installed on your Debian system.

Install the Latest Release of Apache Maven

The following sections provide detailed information for installing the latest Apache Maven version on Debian 9. We will download the latest release of Apache Maven from their official website.

1. Install OpenJDK

Maven 3.3+ require JDK 1.7 or above to be installed on your system. We’ll install OpenJDK, which is the default Java development and runtime in Debian 9.

Start by updating the package index:

sudo apt update

Install the OpenJDK package by typing:

sudo apt install default-jdk

Verify the Java installation by checking its version:

java -version

The output should look something like this:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

2. Download Apache Maven

At the time of writing this article, the latest version of Apache Maven is 3.6.0. Before continuing with the next step you should check the Maven download page to see if a newer version is available.

Download the Apache Maven archive in the /tmp directory using the following wget command:

wget https://www-us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz -P /tmp

Once the download is completed, extract the archive in the /opt directory:

sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt

To have more control over Maven versions and updates, we will create a symbolic link maven which will point to the Maven installation directory:

sudo ln -s /opt/apache-maven-3.6.0 /opt/maven

Later if you want to upgrade your Maven installation you can simply unpack the newer version and change the symlink to point to the latest version.

3. Setup environment variables

Next, we’ll need to setup the environment variables. To do so open your text editor and create a new file named mavenenv.sh inside of the /etc/profile.d/ directory.

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

Paste the following configuration:

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

Save and close the file. This script will be sourced at shell startup.

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

4. Verify the installation

To validate that Maven is installed properly use the mvn -version command which will print the Maven version:

mvn -version

You should see something like the following:

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /opt/maven
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.9.0-8-amd64", arch: "amd64", family: "unix"

That’s it. The latest version of Maven is now installed on your Debian system.

Conclusion

You have successfully installed Apache Maven on your Debian 9. You can now visit the official Apache Maven Documentation page and learn how to get started with Maven.

If you hit a problem or have feedback, leave a comment below.