keepcalculator

06 Numbers and Powers

Install Apache Maven on Debian: A Step-by-Step Guide

Card No. 06·12769 · Updated · Standard formula, shown below

Install Apache Maven on Debian to build Java projects and manage their dependencies from the command line. This guide shows how to install Maven from Debian’s package repository, check that it works, and use an optional manual installation when you need a different version. You’ll need a Debian account with sudo access and a Java Development Kit (JDK).

What You’ll Need

  • A Debian system with an internet connection.
  • A user account that can run commands with sudo, or root access.
  • A JDK. Maven needs Java to run; install a JDK rather than only a Java runtime.
  • A terminal window.

Step-by-Step

  1. Open a terminal and refresh the package list. Run sudo apt update. This updates the local list of available packages; it does not upgrade the system by itself. If you are already logged in as root, omit sudo.
  2. Install a JDK. On Debian releases that provide the default JDK package, run sudo apt install default-jdk. Debian selects the default version for that release. If your project requires a specific Java release, install the corresponding JDK package available for your Debian version instead. Confirm Java is available with java -version, then check the compiler with javac -version. Both commands should print version information.
  3. Install Maven from Debian’s repository. Run sudo apt install maven and confirm the package manager’s prompt. APT installs Maven and its required dependencies, and sets up the executable for normal command-line use. This is the simplest method for most Debian users. The Maven version in the repository depends on your Debian release, so it may not be the newest upstream release.
  4. Check the installation. Run mvn -version. The output should include an Apache Maven version, a Java version, and the Java home directory. Check that the Java version is one your project supports. If you see “command not found,” continue to Troubleshooting below.
  5. Run Maven in a project directory. Change to a Java project that contains a pom.xml file, then run mvn test or the build command specified by that project. Maven may download plugins and dependencies the first time; this requires network access. For a quick independent check, create an empty directory and run mvn archetype:generate only if you want to create a sample project. This command can prompt you for options and is not required to verify installation.
  6. Use a newer Maven manually only if needed. If the repository version does not meet a project requirement, get the binary archive for the required Maven release from the Apache Maven project’s official download page. Verify the archive’s published checksum or signature before using it. Extract it to a stable location, such as /opt/maven, and point your PATH to its bin directory. For example, if you extracted the files into /opt/apache-maven-3.x.y, add export PATH="/opt/apache-maven-3.x.y/bin:$PATH" to your shell’s startup file, then open a new terminal and run mvn -version. Replace the example directory with the real extracted path. Avoid replacing Debian-managed files in /usr/bin.

Common Mistakes

  • Installing Java but not the JDK: Maven can start with a runtime, but Java builds commonly need compiler tools. Install a JDK and verify javac -version.
  • Assuming APT provides the latest Maven: Debian prioritizes packages maintained for each release. Check mvn -version against the version required by your project before choosing a manual installation.
  • Using a mismatched Java version: A project may need a particular JDK even if Maven itself starts successfully. Read the project’s build instructions and use a compatible JDK.
  • Editing the wrong PATH file: Bash, Zsh, and other shells can read different startup files, and interactive and login sessions may behave differently. Confirm your shell with echo "$SHELL" and start a fresh terminal after editing its configuration.
  • Downloading an unverified archive: Use the official project download source and verify the provided checksum or signature. Do not run an archive from an untrusted mirror.

Tips

  • Prefer sudo apt install maven for a straightforward installation. APT handles package updates and removal cleanly.
  • Use mvn -version whenever you need to confirm which Maven and Java installations your shell is using.
  • Keep project-specific Java requirements in mind. Maven’s ability to run does not guarantee that a particular project can compile with the selected JDK.
  • If you install Maven manually, keep its directory separate from Debian-managed software. Update the PATH entry when changing releases, and remove obsolete entries to avoid ambiguity.
  • For repeatable builds, follow the project’s own instructions. Some projects include a Maven Wrapper, which selects or downloads the Maven version configured by the project; run it as ./mvnw where provided.

Troubleshooting

  • mvn: command not found: For an APT installation, check dpkg -l maven and try sudo apt install maven again. For a manual install, confirm the extracted bin directory is in PATH, then open a new terminal. Use command -v mvn to see which executable your shell finds.
  • Maven reports that Java is missing: Run java -version and install a JDK if needed. If Java is installed but not detected, inspect JAVA_HOME with echo "$JAVA_HOME". It should refer to the JDK directory, not its bin subdirectory. Debian’s alternatives system usually configures Java for package installations; avoid setting JAVA_HOME unless your project or environment needs it.
  • The wrong Maven version appears: Run command -v mvn to locate the executable and inspect PATH order. APT’s executable may be found before a manual installation, or an older manual PATH entry may take precedence. Adjust the PATH so the intended installation comes first.
  • Dependency downloads fail: Check internet access, proxy settings, and any network restrictions. Retry after the connection is available. Do not delete Maven’s local repository by default; it is usually under ~/.m2/repository, and removing it forces dependencies to download again.
  • APT cannot locate the package: Run sudo apt update and check that your Debian package sources are configured for your release. If repository access remains unavailable, consult your system administrator rather than adding an untrusted repository.

Frequently Asked Questions

Does Maven require Java?
Yes. Maven runs on Java, and Java projects generally need a JDK for compilation. Install a JDK compatible with your project.

What is the quickest way to install Maven on Debian?
Install a JDK, then run sudo apt update and sudo apt install maven. Check the result with mvn -version.

Will APT install the latest Maven release?
Not necessarily. APT installs the version packaged for your Debian release. Use a verified upstream archive if you specifically require a newer version.

How do I uninstall Maven installed with APT?
Run sudo apt remove maven. To remove package configuration files as well, use sudo apt purge maven. This does not necessarily remove every dependency or your files under ~/.m2.

Can I install Maven without root access?
Yes, if you can download and verify the archive and extract it into a directory you own. Add that installation’s bin directory to your user PATH. You will still need an appropriate JDK.

What is the Maven Wrapper?
It is a project-provided script that runs a configured Maven version. If the project includes it, use ./mvnw (or mvnw.cmd on Windows) instead of relying on a system-wide Maven version.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →