How to install Maven on Windows, MacOS, Linux
Introduction
Apache Maven is a widely-used build automation tool for Java projects, facilitating project management, dependency management, and build automation. Proper installation and configuration of Maven are crucial for leveraging its full capabilities. This article provides a step-by-step guide on installing Maven on various platforms, setting up environment variables, and verifying the installation.
1. Installing Maven on Different Platforms
1.1. Installing Maven on Windows
- Download Maven:
- Visit the Maven download page.
- Download the latest binary zip archive (e.g.,
apache-maven-3.x.x-bin.zip
).
- Extract the Archive:
- Extract the contents of the zip file to a directory of your choice, e.g.,
C:\Program Files\Maven
.
- Extract the contents of the zip file to a directory of your choice, e.g.,
1.2. Installing Maven on MacOS
- Using Homebrew:
- Open Terminal.
- Install Homebrew if it’s not already installed:
1
/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"
- Install Maven using Homebrew:
1
brew install maven
- Manual Installation:
- Visit the Maven download page.
- Download the latest binary tar.gz archive (e.g.,
apache-maven-3.x.x-bin.tar.gz
). - Extract the archive:
1
tar -xzf apache-maven-3.x.x-bin.tar.gz
- Move it to
/usr/local
(or any directory of your choice):1
sudo mv apache-maven-3.x.x /usr/local/apache-maven
1.3. Installing Maven on Linux
- Using Package Manager:
- On Debian-based systems (Ubuntu):
1 2
sudo apt update sudo apt install maven
- On Red Hat-based systems (Fedora/CentOS):
1
sudo dnf install maven
- On Debian-based systems (Ubuntu):
- Manual Installation:
- Visit the Maven download page.
- Download the latest binary tar.gz archive (e.g.,
apache-maven-3.x.x-bin.tar.gz
). - Extract the archive:
1
tar -xzf apache-maven-3.x.x-bin.tar.gz
- Move it to
/opt
(or any directory of your choice):1
sudo mv apache-maven-3.x.x /opt/apache-maven
2. Setting Up Environment Variables
2.1. Windows
- Open Environment Variables:
- Right-click
This PC
orComputer
. - Select
Properties
and thenAdvanced system settings
. - Click on
Environment Variables
.
- Right-click
- Add MAVEN_HOME Variable:
- Click
New
under System variables. - Enter
MAVEN_HOME
as the variable name. - Enter the path to your Maven directory as the variable value (e.g.,
C:\\Program Files\\Maven\\apache-maven-3.x.x
).
- Click
- Edit PATH Variable:
- Find the
Path
variable under System variables and clickEdit
. - Add a new entry:
%MAVEN_HOME%\\bin
.
- Find the
2.2. MacOS and Linux
- Open Terminal.
- Edit Profile File:
- For macOS, edit
~/.zshrc
or~/.bash_profile
depending on your shell. - For Linux, edit
~/.bashrc
or~/.profile
depending on your shell.1
nano ~/.bashrc # or ~/.zshrc for macOS using zsh
- For macOS, edit
- Add Environment Variables:
- Add the following lines:
1 2
export MAVEN_HOME=/usr/local/apache-maven export PATH=$MAVEN_HOME/bin:$PATH
- Add the following lines:
- Apply Changes:
- Save the file and apply changes:
1
source ~/.bashrc # or ~/.zshrc for macOS
- Save the file and apply changes:
3. Verifying the Installation
After installing Maven and setting up the environment variables, you should verify the installation to ensure everything is configured correctly.
3.1. Verify Maven Installation
- Open a Terminal or Command Prompt.
- Run the Maven Command:
1
mvn -v
- Check Output:
- The command should display Maven’s version along with Java version and other environment details.
- Example output:
1 2 3 4 5 6
Apache Maven 3.x.x (Redback Java API, Apache Maven 3.x.x) Maven home: /usr/local/apache-maven Java version: 11.0.7, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.4.0-54-generic", arch: "amd64", family: "unix"
3.2. Common Issues
- Command Not Found
Ensure that the Mavenbin
directory is correctly added to the PATH environment variable. - Incorrect Version
Verify that theMAVEN_HOME
variable points to the correct Maven installation directory.
Conclusion
Maven simplifies Java project management and build automation. By following the steps outlined above, you can install and configure Maven on Windows, macOS, and Linux, ensuring a smooth development experience. Proper setup of environment variables and verification of the installation will help you leverage Maven’s full capabilities for efficient project management and build processes.