Cybersecurity Updates & Tools

Install Apache Maven on Ubuntu 18.04: Stable or Latest Version

Java developers use project management tools to automate building their applications. Apache Maven is an open source tool used to manage compilation, testing, and packaging. It uses an XML configuration file called a Project Object Model (POM) to control dependencies.

This tutorial will show you how to install Apache Maven on Ubuntu 18.04. We will cover a simple install using the package manager, and a manual install to get the latest release.

Method 1: Install Apache Maven with apt (Quick Setup)

Installing Maven using the Ubuntu software sources is the fastest method. The version in the official repository is stable, though it might be older than the latest release.

Update your software package index:

bashsudo apt update

Install Maven by running:

bashsudo apt install maven

Confirm that the installation was successful by checking the version:

bashmvn -version

The output will show the installed version number and the path to the Java installation on your system.

Method 2: Install the Latest Version of Apache Maven

If you need the latest features, you can download the package archive from the official website.

First, ensure you have Java installed. You can install OpenJDK with this command:

bashsudo apt install default-jdk

Next, download the Maven package archive to the temporary folder:

bashwget https://archive.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz -P /tmp

Extract the archive files into the application directory:

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

To update versions easily in the future, create a symbolic link:

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

Configure System Environment Settings

You must set system variables so your shell can find the Maven commands.

Create a new setup script file:

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

Paste these lines into the file:

bashexport JAVA_HOME=/usr/lib/jvm/default-javaexport M2_HOME=/opt/mavenexport MAVEN_HOME=/opt/mavenexport PATH=${M2_HOME}/bin:${PATH}

Save the file. Make the script executable:

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

Load the environment settings into your active session:

bashsource /etc/profile.d/maven.sh

Verify that the manual installation works:

bashmvn -version

The output will confirm the version, showing that the configuration is complete.

You have installed the build tool on your system. Using the package manager is convenient for quick setups, while the manual installation is best if your project requires a specific release. Leave a comment below if you run into any path configuration issues.