Install Apache Maven on Ubuntu to manage Java builds, dependencies, and documentation through a standardized project structure. Whether you need to compile Java applications, manage library dependencies automatically, or integrate with CI/CD pipelines like Jenkins, Maven provides the foundation for reproducible builds. By the end of this guide, you will have a working Maven installation verified with a test project.
Choose Your Apache Maven Installation Method
Before installing, consider your options. Ubuntu offers Maven through its default repositories, while the Apache project provides the latest release for manual installation. The table below compares both approaches:
| Method | Channel | Version | Updates | Best For |
|---|---|---|---|---|
| APT Package Manager | Ubuntu Repos | Distribution default | Automatic via apt upgrade | Most users who prefer distro-tested packages |
| Manual Installation | Apache Downloads | Latest stable | Manual re-download | Users needing the newest Maven features |
We recommend the APT method for most users because it provides automatic security updates and handles Java dependencies automatically. Only install manually if you specifically need features from the latest Maven release.
Maven versions differ by Ubuntu release. Ubuntu 26.04 ships Maven 3.9.9 with Java 21, Ubuntu 24.04 ships Maven 3.8.7 with Java 21, and Ubuntu 22.04 ships Maven 3.6.3 with Java 11. If you need a newer Maven version than your Ubuntu release provides, use the manual installation method and confirm your version with
mvn -version.
Method 1: Install Apache Maven via APT Package Manager
Step 1: Update Ubuntu System Packages
First, refresh the package index so APT sees the latest Maven and Java packages:
sudo apt update
Specifically, running apt update refreshes your package index, ensuring you install the latest available version rather than a cached older release.
If you want to apply pending security updates now, run sudo apt upgrade before continuing.
Step 2: Install Apache Maven
Next, install Maven and the default Java Development Kit (JDK) so you have the compiler needed for builds:
sudo apt install maven default-jdk
While the Maven package pulls in a Java runtime, the JDK also adds the javac compiler that Maven uses to build projects.
Step 3: Verify Maven and the Java Compiler
Once installation completes, verify that Maven is accessible and check the installed version:
mvn -version
Expected output (Ubuntu 26.04):
Apache Maven 3.9.9 Maven home: /usr/share/maven Java version: 21.0.10-ea, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Additionally, your output may include additional lines for locale and OS details.
Next, confirm the Java compiler is available:
javac -version
Expected output (Ubuntu 26.04):
javac 21.0.10-ea
The outputs above reflect Ubuntu 26.04. Ubuntu 24.04 uses Java 21 and Maven 3.8.7, while Ubuntu 22.04 uses Java 11 and Maven 3.6.3, so your version numbers will match your release.
Method 2: Install Apache Maven from the Binary Tarball
Alternatively, this method downloads the latest Maven release directly from Apache and installs it under /opt, giving you access to newer features before they appear in Ubuntu repositories.
Step 1: Install Prerequisites
Maven requires a Java Development Kit (JDK). Additionally, minimal Ubuntu installations may omit curl and CA certificates, so install the prerequisites first:
sudo apt update
sudo apt install default-jdk ca-certificates curl
Specifically, the JDK provides java and javac, ca-certificates enables HTTPS downloads, and curl fetches the release metadata and tarball.
Once you install the packages, verify that Java is available:
java -version
Expected output (Ubuntu 26.04):
openjdk version "21.0.10-ea" 2026-01-20 OpenJDK Runtime Environment (build 21.0.10-ea+4-Ubuntu-1) OpenJDK 64-Bit Server VM (build 21.0.10-ea+4-Ubuntu-1, mixed mode, sharing)
Ubuntu 24.04 uses Java 21 and Ubuntu 22.04 uses Java 11, so your output will reflect your release.
Step 2: Download the Latest Maven Release
Now, use the GitHub API to detect the latest Maven release and download the tarball from Apache. Keep this terminal open because the MAVEN_VERSION variable is reused in the next steps:
cd /tmp
MAVEN_VERSION=$(curl -fsSL https://api.github.com/repos/apache/maven/releases/latest | grep -oP '"tag_name": "maven-\K[^"]+')
if [ -z "$MAVEN_VERSION" ]; then
echo "Error: Could not determine the latest Maven version."
else
curl -fL "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
fi
Specifically, this command queries the Apache Maven GitHub releases for the latest tag and downloads the matching archive from the official CDN. The -f flag makes curl fail on HTTP errors, -L follows redirects, and -o saves the tarball using the versioned filename.
If the GitHub API request fails, set
MAVEN_VERSIONmanually from the Apache Maven download page or the GitHub releases page, then re-run the download line.
If you want more examples of curl usage, see our curl command guide.
Step 3: Extract Maven to the Installation Directory
Next, extract the archive to /opt/, which is the standard location for optional software on Linux systems:
sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/
Here, the -C flag specifies the target directory for extraction. If you closed your terminal, re-run the previous step so MAVEN_VERSION is set again.
Step 4: Create a Symbolic Link
After extraction, create a symbolic link to simplify path references and make future upgrades easier:
sudo ln -sfn /opt/apache-maven-${MAVEN_VERSION} /opt/maven
As a result, this creates a /opt/maven symlink pointing to the extracted directory. The -s flag creates a symlink, -f replaces any existing one, and -n treats the current symlink as a normal file, keeping upgrades consistent when multiple Maven versions are present.
Step 5: Configure Environment Variables
Now, create a profile script to set the required environment variables system-wide:
sudo tee /etc/profile.d/maven.sh >/dev/null <<'EOF'
export M2_HOME=/opt/maven
export PATH="${M2_HOME}/bin:${PATH}"
EOF
Essentially, this command writes /etc/profile.d/maven.sh with two environment variables: M2_HOME points to the Maven installation, and PATH includes the Maven binaries. The quoted heredoc keeps the variables literal so each new shell expands them correctly.
Step 6: Apply the Environment Changes
System-wide profile scripts load during login sessions. To apply the changes immediately without opening a new terminal, reload /etc/profile:
source /etc/profile
Alternatively, log out and back in, or open a new terminal window for the changes to take effect automatically. The environment variables persist for all future sessions.
Step 7: Verify the Manual Installation
Finally, confirm that Maven is correctly installed and accessible from your PATH:
mvn -version
Expected output (Ubuntu 26.04):
Apache Maven 3.9.12 (848fbb4bf2d427b72bdb2471c22fced7ebd9a7a1) Maven home: /opt/maven Java version: 21.0.10-ea, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Here, the output confirms Maven home is /opt/maven (the symlink you created) and shows the Java version being used.
Ubuntu 24.04 and 22.04 will show different Java versions in the output, but the Maven home path remains
/opt/mavenacross releases.
Additionally, before creating a project, confirm the Java compiler is available with javac -version because Maven uses it to compile sources.
Create a Test Maven Project
To verify your Maven installation works correctly, try creating and building a simple Java project.
Step 1: Generate a New Project
Maven provides archetypes (project templates) to quickly scaffold new projects. Create a basic Java application:
mvn archetype:generate -DgroupId=com.example.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
As a result, this command creates a new directory called my-app with a standard Maven project structure, including a pom.xml configuration file and sample Java source code.
Step 2: Build the Project
Next, navigate into the project directory and compile the application:
cd my-app
mvn package
During this process, Maven downloads required dependencies, compiles the source code, runs tests, and packages the application into a JAR file in the target/ directory.
Step 3: Run the Application
Finally, execute the compiled application to confirm everything works:
java -cp target/my-app-1.0-SNAPSHOT.jar com.example.app.App
Expected output:
Hello World!
If you see this output, your Maven installation is fully functional and ready for Java development.
Troubleshooting Common Maven Issues
JAVA_HOME Not Set or Incorrect
If Maven reports that JAVA_HOME is missing or incorrect, it cannot locate the JDK:
The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.
To diagnose, check the current variable and the actual Java path:
printf 'JAVA_HOME=%s\n' "$JAVA_HOME"
readlink -f /usr/bin/java
To fix this, if JAVA_HOME is empty or incorrect, set it system-wide and reload the profile:
echo "export JAVA_HOME=/usr/lib/jvm/default-java" | sudo tee /etc/profile.d/java-home.sh
source /etc/profile
Then, verify Maven can now locate Java:
mvn -version
Maven Command Not Found
If your shell reports that mvn is missing after a manual install, the PATH update has not been applied:
bash: line 1: mvn: command not found
First, check whether the Maven binary exists and whether it is in your PATH:
command -v mvn || echo "mvn not found in PATH"
ls -l /opt/maven/bin/mvn
To fix this, reload the system profile or open a new terminal window:
source /etc/profile
Then, verify the command works:
mvn -version
Update Apache Maven
Update APT-Installed Maven
For APT installations, update Maven along with other system packages:
sudo apt update
sudo apt install --only-upgrade maven
Update Manually Installed Maven
However, for manual installations, download the latest release and refresh the symbolic link:
cd /tmp
if ! command -v curl >/dev/null || ! command -v tar >/dev/null; then
echo "Install curl and tar before updating: sudo apt install curl tar"
else
MAVEN_VERSION=$(curl -fsSL https://api.github.com/repos/apache/maven/releases/latest | grep -oP '"tag_name": "maven-\K[^"]+')
if [ -z "$MAVEN_VERSION" ]; then
echo "Error: Could not determine the latest Maven version."
else
curl -fL "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" -o "apache-maven-${MAVEN_VERSION}-bin.tar.gz"
sudo tar xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt/
sudo ln -sfn /opt/apache-maven-${MAVEN_VERSION} /opt/maven
mvn -version
fi
fi
Essentially, this script detects the newest Maven release tag, downloads it from the Apache CDN, extracts to /opt/, and updates the symlink. The -f and -L flags ensure curl fails fast on HTTP errors and follows redirects.
Do not automate Maven upgrades with cron. Review release notes and test builds before updating production environments.
Remove Apache Maven
Remove APT-Installed Maven
To uninstall Maven installed via APT:
sudo apt remove maven
sudo apt autoremove
Subsequently, the autoremove command cleans up Java and other dependencies that APT automatically installed with Maven and no longer needs.
Next, verify the removal by checking for the Maven command:
command -v mvn || echo "mvn not found"
Expected output:
mvn not found
Remove Manually Installed Maven
The following commands permanently delete the Maven installation directory. Only proceed if you no longer need your manual Maven installation.
Note that the -rf flags remove directories recursively and without prompting, so double-check the paths before you run them.
Then, for manual installations, remove the installation directory, symbolic link, and environment script:
sudo rm -rf /opt/apache-maven-*
sudo rm -f /opt/maven
sudo rm -f /etc/profile.d/maven.sh
Additionally, if you created a dedicated JAVA_HOME profile script while troubleshooting, remove it as well:
sudo rm -f /etc/profile.d/java-home.sh
After removal, open a new terminal or run hash -r to clear the command cache.
Remove Maven User Data (Optional)
The following command permanently deletes your Maven configuration, cached dependencies, and local repository. This includes downloaded JAR files and custom settings. Only proceed if you no longer need this data.
Note that the -rf flags remove the directory recursively without prompting, so confirm you no longer need the cached dependencies.
If desired, you can also remove the local Maven repository containing cached dependencies and configuration:
rm -rf ~/.m2
Conclusion
You now have Apache Maven configured on Ubuntu for building Java projects and managing dependencies. Whether you chose the APT package for automatic updates or manually installed the latest release, Maven handles the complete build lifecycle from compilation through testing to packaging. For version control integration, see how to install Git on Ubuntu. If you need additional Java environment configuration, check out our guide on setting the Java environment path in Ubuntu.