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.

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.
- Prerequisites and Supported Releases
- Step 1: Installing the Required Java Development Kit (OpenJDK)
- Method 1: Installing Maven via Ubuntu APT Repositories (Quick & Stable)
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.
Apache Maven is a premier build automation and project management tool for Java applications based on the Project Object Model (POM) concept. This comprehensive tutorial demonstrates how to install Apache Maven on Ubuntu 24.04 LTS using both the official Ubuntu APT repository and manual binary tarball distribution, configure system-wide environment variables, and build your first Java project.
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.
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:
# 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 / Dimension | Ubuntu APT Repository | Apache Official Binary Tarball |
|---|---|---|
| Release Cadence | Stable LTS snapshot (e.g. Maven 3.8.7) | Bleeding-edge latest release (e.g. Maven 3.9.x) |
| Installation Speed | Single command (apt install maven) | Multi-step (download, unpack, profile script) |
| Maintenance | Automated via standard apt upgrade | Manual replacement of symbolic links |
| File Paths | /usr/share/maven and /etc/maven | Self-contained inside /opt/maven |
| Best Use Case | General Java microservices and CI runners | Strict 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:
# 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 Phase | Operational Scope | Execution Output |
|---|---|---|
| validate | Validates project structure | Confirms pom.xml is syntactically sound |
| compile | Compiles Java source code | Places .class bytecode files into target/classes |
| test | Executes unit test suites | Runs Surefire test runner on tests in src/test/java |
| package | Packages compiled classes | Creates target/*.jar or *.war distribution artifact |
| verify | Runs integration tests | Checks quality gates and integration test outcomes |
| install | Installs artifact locally | Copies package into ~/.m2/repository for local reuse |
| deploy | Publishes to remote repository | Uploads 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).
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- How to Install Java on Ubuntu 24.04 — Configure OpenJDK versions and manage system-wide
JAVA_HOMEpaths on Ubuntu. - How to Install Jenkins on Ubuntu 24.04 LTS — Automate Maven build pipelines and test suites using a Jenkins CI server.
- How to Install Tomcat 10 on Ubuntu 24.04 LTS — Deploy Maven-compiled WAR packages to production Apache Tomcat application servers.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
~/.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)./opt/, then update the symbolic link: sudo ln -sfn /opt/apache-maven-NEW_VERSION /opt/maven. Existing environment variables remain intact.openjdk-21-jdk and update JAVA_HOME to point to the Java 21 directory.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
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.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides


Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!