X
    Categories: Java Core

How to install Maven on Windows, Linux and Mac

In this tutorial, we will be going through the installation process of Maven.

If you are a WINDOWS user (if you are not, scroll down to view how to install Maven if you are a Linux or Mac user):

Before proceeding with the next steps, make sure you have JDK installed on your system. If you don’t, you can view this article for a quick step-by-step guide. To view all the necessary minimum requirements for Maven, either click here or view the image below

Click here and download the latest version. You can either select the Binary version or the full version (a.k.a. with source and docs). I recommend downloading the Binary one as you can find all the source and docs online anyway.

Select the Binary edition if you don’t want the source and docs included in it. When you click on the link, it should automatically start downloading the zip file. If it doesn’t refresh the page and try again.

After your download is complete, extract the archive to C and preferably, create a folder in C named Maven and extract the files into there.

After you have created the Maven folder, extract the files from the downloaded zip file into that folder.

Now we have to set up M2_HOME. Before being able to run Maven, we need to set it in the environment variables and add the “bin” to PATH.

Adding M2_HOME

  1. Right-click somewhere on This PC
    1. Select Properties
    2. On the right, select Advanced System Settings
    3. After that, click on “Environmental Variables…”
    4. Under “System variables” select New…
    5. When the input box opens, type M2_HOME as a variable name and the path to the maven
    6. Under System variables, select “Path” and click on Edit…
    7. Add the path “C:\Maven\apache-maven-3.6.1\bin”

Verifying if the installation has been successful

To know if the installation has been successful for you, type this in the CMD:

mvn --version

If you get something like:

Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T20:00:29+01:00)
Maven home: C:\Maven\apache-maven-3.6.1\bin\..
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_211\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

then you are good to go.



Linux

Requirements:

  1. Download apache-maven-3.6.1-bin.tar.gz binary (if you do not want the source and docs included).
  2. typecd /optin the terminal
  3. extract the apache-maven archive into the opt directory: sudo tar -xvzf ~/Downloads/apache-maven-3.6.0-bin.tar.gz
  4. Add as environment variable:M2_HOME="/opt/apache-maven-3.6.0" and append the bin directory to the Path variable:/opt/apache-maven-3.6.0/bin
  5.  Update the mvn command:
    1. sudo update-alternatives --install "/usr/bin/mvn" "mvn" "/opt/apache-maven-3.6.0/bin/mvn" 0
    2. sudo update-alternatives --set mvn /opt/apache-maven-3.6.0/bin/mvn
  6. Add bash completion to mvn:sudo wget https://raw.github.com/dimaj/maven-bash-completion/master/bash_completion.bash --output-document /etc/bash_completion.d/mvn. This will allow you to complete Maven commands by hitting Tab a couple of times.
  7. Type mvn –version to verify if it works

MAC

  1. Open the terminal and go to the directory in which the files were extracted and login as Super User
  2. Remove the tar.gz archive by typing rm Downloads/apache-maven*bin.tar.gz
  3. Fix the permissions: chown -R root:wheel Downloads/apache-maven*
  4. Switch the maven contents: mv Downloads/apache-maven* /opt/apache-maven
  5. Archive the Admin session: exit
  6. Add Maven binaries to the path and then append:
    1. nano $HOME/.profile
    2. export PATH=$PATH:/opt/apache-maven/bin
  7. Ctrl + x to Save and Exit from nano
  8. To load the new set up execute bash
  9. To test if the installation has been successful, type mvn -version
5 2 votes
Article Rating
ym_coding:
Related Post