Skip to main content

Maven Fundamentals

How to install Maven

Introduction

Before you can use Apache Maven in your Java projects, you need to install Maven on your machine, configure the required environment variables, and verify the setup.

In this lesson, you’ll learn:

  • How to install Maven on Windows, macOS, and Linux
  • How to set up JAVA_HOME and MAVEN_HOME variables
  • How to verify Maven installation using the mvn -v command

1. Installing Maven on Windows

Step 1 – Download Maven

  1. Go to the official Apache Maven website:
    https://maven.apache.org/download.cgi
  2. Download the binary zip archive (e.g., apache-maven-3.9.x-bin.zip).

Step 2 – Extract Maven

  1. Unzip the downloaded file.
  2. Place the extracted folder (e.g., apache-maven-3.9.x) in a location like C:\Program Files\Apache\Maven.

Step 3 – Set Environment Variables

  1. Open Control Panel → System and Security → System → Advanced system settings.
  2. Click Environment Variables.
  3. Add/Update:
    • JAVA_HOME → Path to your JDK (e.g., C:\Program Files\Java\jdk-17)
    • MAVEN_HOME → Path to Maven folder (e.g., C:\Program Files\Apache\Maven\apache-maven-3.9.x)
  4. Update the Path variable to include:
%JAVA_HOME%\bin
%MAVEN_HOME%\bin

2. Installing Maven on macOS

Option 1 – Install with Homebrew (Recommended)

If you use Homebrew:

brew install maven

Option 2 – Manual Installation

  • Download the binary tar.gz archive from Maven Downloads.
  • Extract it to a directory (e.g., /usr/local/apache-maven).
  • Add to your shell config (.zshrc or .bash_profile):
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export M2_HOME=/usr/local/apache-maven/apache-maven-3.9.x
export PATH=$M2_HOME/bin:$PATH
  • Save the file and run:
source ~/.zshrc

3. Installing Maven on Linux

Option 1 – Install via Package Manager

For Ubuntu/Debian:

sudo apt update
sudo apt install maven

For CentOS/Fedora:

sudo dnf install maven

Option 2 – Manual Installation

  • Download the tar.gz from Maven Downloads.
  • Extract it:
tar -xvzf apache-maven-3.9.x-bin.tar.gz -C /opt
  • Set environment variables in ~/.bashrc or ~/.zshrc :
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export M2_HOME=/opt/apache-maven-3.9.x
export PATH=$M2_HOME/bin:$PATH
  • Apply changes:
source ~/.bashrc

4. Setting JAVA_HOME and MAVEN_HOME (All OS)

  • JAVA_HOME → Points to your Java Development Kit installation
  • MAVEN_HOME → Points to your Maven installation directory
  • PATH → Should include $JAVA_HOME/bin and $MAVEN_HOME/bin so you can run java, javac, and mvn from any terminal or command prompt

Example (cross-platform shell syntax):

export JAVA_HOME=/path/to/jdk
export M2_HOME=/path/to/apache-maven
export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH

5. Verifying Maven Installation

Once installed and environment variables are set, check Maven’s version:

mvn -v

Expected Output Example:

Apache Maven 3.9.x (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apache-maven-3.9.x
Java version: 17.0.8, vendor: Eclipse Adoptium
Default locale: en_US, platform encoding: UTF-8
OS name: Mac OS X, version: 13.5.1, arch: x86_64

If you see the Maven version, Java version, and OS details, your installation is successful.

Troubleshooting Tips

  • Command not found: Ensure Maven’s bin directory is in your PATH.
  • Wrong Java version: Update JAVA_HOME to point to the correct JDK.
  • Permission denied: Use sudo (Linux/macOS) or run as administrator (Windows) for installation.

Key Takeaways

  • Maven requires Java to be installed first.
  • Correctly setting JAVA_HOME and MAVEN_HOME is critical.
  • The mvn -v command is the quickest way to verify a successful Maven installation.