Website Vidya
Linux & ServersStep-by-Step Guide

How to Install Apache Maven on Ubuntu 24.04

Step-by-step guide to install Apache Maven on Ubuntu 24.04 LTS using APT and official binaries, with OpenJDK 17 setup and PATH configuration.

RR
Rajesh RayLead Systems Educator
Published:
6 min read
PDF Cheat Sheet
How to Install Apache Maven on Ubuntu 24.04
AI Executive SummaryWebsiteVidya AI

6 min read

Step-by-step guide to install Apache Maven on Ubuntu 24.04 LTS using APT and official binaries, with OpenJDK 17 setup and PATH configuration.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Installing the Required Java Development Kit (OpenJDK)
  • Method 1: Installing Maven via Ubuntu APT Repositories (Quick & Stable)
Grounded in full engineering runbookWebsiteVidya AI Engine

Java software development, enterprise application lifecycles, and microservice builds require standardized build orchestration, automated dependency resolution, and consistent project structures. When you install apache maven on ubuntu 24.04, you deploy an industry-standard build framework that manages third-party Java libraries, compiles source code, executes automated unit test suites, and packages binaries into deployable JAR or WAR artifacts.

Maven relies on declarative XML configuration files (pom.xml) to download required dependencies automatically from Maven Central repositories. On Ubuntu 24.04, Maven can be installed rapidly via the standard Ubuntu APT repository or deployed from the latest upstream Apache binary release. This tutorial walks you through prerequisite Java setup, dual installation avenues, environment variable exports, and hands-on build verification.

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Advertisement

Before proceeding, ensure you have the following requirements satisfied:

  • An active installation of Ubuntu 24.04 LTS (Desktop or Server).
  • A user account with active sudo administrative privileges.
  • Java Development Kit (OpenJDK 17 or OpenJDK 21) installed or ready to install.
  • Access to a command-line terminal and active Internet connectivity.

Step 1: Installing the Required Java Development Kit (OpenJDK)

Confirm that both java and javac return version 17.x or newer. If you operate multiple Java installations, verify the active default using sudo update-alternatives --config java.

# Refresh package repository index
sudo apt update

# Install OpenJDK 17 JDK
sudo apt install -y openjdk-17-jdk

# Verify Java compiler and runtime versions
java -version
javac -version

Method 1: Installing Maven via Ubuntu APT Repositories (Quick & Stable)

The package manager installs Maven executables in /usr/bin/mvn, core libraries in /usr/share/maven, and configuration settings in /etc/maven.

Review the key differences between the APT package and the standalone binary archive to decide which method best suits your project requirements:

Advertisement
# Install Apache Maven via APT
sudo apt install -y maven

# Verify the installed version
mvn -version

# Sample output:
# Apache Maven 3.8.7
# Maven home: /usr/share/maven
# Java version: 17.0.12, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Feature / DimensionUbuntu APT RepositoryApache Official Binary Tarball
Release CadenceStable LTS snapshot (e.g. Maven 3.8.7)Bleeding-edge latest release (e.g. Maven 3.9.x)
Installation SpeedSingle command (apt install maven)Multi-step (download, unpack, profile script)
MaintenanceAutomated via standard apt upgradeManual replacement of symbolic links
File Paths/usr/share/maven and /etc/mavenSelf-contained inside /opt/maven
Best Use CaseGeneral Java microservices and CI runnersStrict builds requiring exact upstream revisions

Method 2: Installing the Latest Apache Maven from Official Binary Tarball

To deploy the latest release of Maven directly from the Apache Software Foundation, download the compiled binary archive, extract it into the system-wide /opt directory, and configure execution paths manually.

First, verify that wget and tar are installed on your machine.

# Install retrieval utilities
sudo apt update && sudo apt install -y wget tar

Downloading and Extracting the Maven Binary Archive

Download the binary archive from an official Apache mirror into your /tmp directory, then extract it to /opt/maven:

# Define target Maven version
MAVEN_VER="3.9.8"

# Download the official binary archive
wget https://dlcdn.apache.org/maven/maven-3/${MAVEN_VER}/binaries/apache-maven-${MAVEN_VER}-bin.tar.gz -P /tmp

# Extract the archive into /opt
sudo tar -xzf /tmp/apache-maven-${MAVEN_VER}-bin.tar.gz -C /opt

# Create a symbolic link for streamlined version upgrades
sudo ln -sfn /opt/apache-maven-${MAVEN_VER} /opt/maven

Configuring Environment Variables in /etc/profile.d/maven.sh

To make the mvn command available to all users across the system, create an environment profile script in /etc/profile.d/maven.sh:

Advertisement
# Create the global Maven environment profile script
sudo tee /etc/profile.d/maven.sh <<'EOF'
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
EOF

# Grant execution permissions to the script
sudo chmod +x /etc/profile.d/maven.sh

# Source the profile into your current shell session
source /etc/profile.d/maven.sh

Verifying the Manual Binary Installation

Verify that the new binary path is recognized and reports the updated version:

# Verify the newly installed Maven version
mvn -version

# Sample output:
# Apache Maven 3.9.8 (36645f0530e5cf77786d599f3292c7345253b323)
# Maven home: /opt/maven
# Java version: 17.0.12, vendor: Ubuntu

Step 3: Creating and Building a Sample Java Project with Maven

Understanding Maven's sequential default lifecycle ensures you execute only the phases required for your workflow. Invoking a downstream phase automatically executes all preceding phases; for instance, running mvn package automatically triggers validate, compile, and test.

# Create and enter a projects workspace
mkdir -p ~/projects && cd ~/projects

# Generate a starter Java project non-interactively
mvn archetype:generate \
  -DgroupId=com.example.app \
  -DartifactId=my-java-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.4 \
  -DinteractiveMode=false
Lifecycle PhaseOperational ScopeExecution Output
validateValidates project structureConfirms pom.xml is syntactically sound
compileCompiles Java source codePlaces .class bytecode files into target/classes
testExecutes unit test suitesRuns Surefire test runner on tests in src/test/java
packagePackages compiled classesCreates target/*.jar or *.war distribution artifact
verifyRuns integration testsChecks quality gates and integration test outcomes
installInstalls artifact locallyCopies package into ~/.m2/repository for local reuse
deployPublishes to remote repositoryUploads final artifact to Nexus or Artifactory

Compiling and Packaging the Project into a JAR File

Navigate into the project root and invoke the package lifecycle goal. Maven will compile source files, execute test cases, and package the final application into an executable JAR archive:

# Enter project folder and build the application
cd my-java-app
mvn package

# Execute the compiled Java application
java -cp target/my-java-app-1.0-SNAPSHOT.jar com.example.app.App

# Output:
# Hello World!

Step 4: Completely Uninstalling Apache Maven from Ubuntu

If you need to remove Maven from your Ubuntu system, execute the commands matching your original installation approach:

# If installed via APT:
sudo apt purge -y maven
sudo apt autoremove -y

# If installed manually via binary tarball:
sudo rm -rf /opt/apache-maven-* /opt/maven
sudo rm -f /etc/profile.d/maven.sh

# Remove local Maven cache repository:
rm -rf ~/.m2

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

mvn: command not found after manual installation.

Cause: The environment script /etc/profile.d/maven.sh was not sourced or the terminal was not restarted.

Resolution: Source the environment script manually: source /etc/profile.d/maven.sh or log out and log back in.

source /etc/profile.d/maven.sh

Error: JAVA_HOME is not defined correctly.

Cause: The JAVA_HOME environment variable points to a non-existent or invalid directory path.

Resolution: Locate your active JDK directory using update-java-alternatives -l and export the correct path in /etc/profile.d/maven.sh.

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))

Maven build fails with 'Could not transfer artifact' SSL certificate error.

Cause: System Java security certificate store is missing or out of date.

Resolution: Reinstall the CA certificates package: sudo apt install --reinstall ca-certificates-java.

sudo apt install --reinstall ca-certificates-java

Security Best Practices and Hardening

Inspect POM dependencies regularly for vulnerable third-party libraries using the OWASP Dependency-Check Maven plugin (dependency-check-maven).

Never build untrusted Maven projects under the root user account, as Maven build plugins can execute arbitrary Java code and system commands during the compile phase.

Always configure Maven to download dependencies exclusively over encrypted HTTPS endpoints from Maven Central and trusted enterprise mirrors.

Store private repository credentials, Nexus passwords, and GPG signing keys inside ~/.m2/settings.xml with restricted read-only permissions (chmod 600 ~/.m2/settings.xml).

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
By default, Maven stores all downloaded dependency JAR files and plugin artifacts in your user home directory under ~/.m2/repository/.
mvn clean removes all previously generated build artifacts and compiles in the target/ directory. mvn package takes the compiled source code, runs automated tests, and packages it into its distributed format (JAR or WAR).
Download and extract the new Maven tarball into /opt/, then update the symbolic link: sudo ln -sfn /opt/apache-maven-NEW_VERSION /opt/maven. Existing environment variables remain intact.
Yes. Both Maven 3.8.x and 3.9.x fully support Java 21 LTS. Simply install openjdk-21-jdk and update JAVA_HOME to point to the Java 21 directory.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout