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
- Go to the official Apache Maven website:
https://maven.apache.org/download.cgi - Download the binary zip archive (e.g.,
apache-maven-3.9.x-bin.zip
).
Step 2 – Extract Maven
- Unzip the downloaded file.
- Place the extracted folder (e.g.,
apache-maven-3.9.x
) in a location likeC:\Program Files\Apache\Maven
.
Step 3 – Set Environment Variables
- Open Control Panel → System and Security → System → Advanced system settings.
- Click Environment Variables.
- 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
)
- JAVA_HOME → Path to your JDK (e.g.,
- 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 runjava
,javac
, andmvn
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.