In this short tutorial, we are going to shed light on how to install Maven on Mac operating system.

Feel free to check our article on how to install maven on windows 10.

Install Maven on Mac OS

Apache Maven is renowned for its ease of installation. In general, installing Mvn on Mac is a very simple process and takes less than five minutes.

Maven as a dependency management tool is written in Java. So, to run its commands without issues, we need to make sure first and foremost that Java is installed on our mac and properly configured.

So, before moving ahead with the installation, we need to ensure that JAVA_HOME environment variable is set. To do so, run these command lines:

    
        echo $JAVA_HOME
        java -version
    
  • $JAVA_HOME should point to the JDK installation folder

  • java -version should display the right java version

Installing Apache Maven using Brew

Unfornatuly, Mac does not come with Brew installed, so our first step will be to install Homebrew first.

In short, Homebrew is a free package manager that can be used to install, update and remove software packages on Mac.

With Homebrew, maven installation is just a piece of cake and can be done automatically within few seconds.

Let’s open a terminal and run:

    
        brew install maven
        
        ==> Downloading https://www.apache.org/dyn/closer.lua?path=maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
        ==> Downloading from https://downloads.apache.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
        ######################################################################## 100.0%
        ==> Installing
    

That’s all it takes to install mvn on Mac using Brew. This will install the latest version which is now maven@3.8.

download maven using brew /></p>

<p>Homebrew provides a convenient command to search all available maven formula (packages): <strong><em>brew search maven</em></strong></p>

<pre>
    <code>
        brew search maven
        
        ==> Formulae
        maven    maven-completion    maven-shell    maven@3.0    maven@3.1    maven@3.2    maven@3.3

        ==> Casks
        mavensmate    homebrew/cask-fonts/font-maven-pro
    </code>
</pre>

<p>We can even search for a specific release with the help of the following command line:</p>

<pre>
    <code>
        brew search maven 3

        ==> Formulae
        maven@3.0    maven@3.1    maven@3.2    maven@3.3    maven@3.5
    </code>
</pre>

<p>That way, we can choose and install a specific mvn version.</p>

<p>Now that we have installed maven, let’s verify if everything is working as expected:</p>

<pre>
    <code>
        mvn -version

        Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
        Maven home: /usr/local/Cellar/maven/3.8.1/libexec
        Java version: 15.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home
        Default locale: en_MY, platform encoding: UTF-8
        OS name:

As shown above, brew install maven packages in /usr/local/Cellar/maven/3.8.1.

    
        ls -lsa /usr/local/Cellar/maven/3.8.1
        total 72
        0 drwxr-xr-x  9 devwithus  admin    288 May 18 09:43 .
        0 drwxr-xr-x  3 devwithus  admin     96 May 18 09:43 ..
        0 drwxr-xr-x  3 devwithus  admin     96 May 18 09:43 .brew
        8 -rw-r--r--  1 devwithus  admin    756 May 18 00:43 INSTALL_RECEIPT.json
        40 -rw-r--r--  1 devwithus  admin  17504 May 18 09:43 LICENSE
        16 -rw-r--r--  1 devwithus  admin   5141 May 18 09:43 NOTICE
        8 -rw-r--r--  1 devwithus  admin   2612 May 18 09:43 README.txt
        0 drwxr-xr-x  5 devwithus  admin    160 May 18 09:43 bin
        0 drwxr-xr-x  6 devwithus  admin    192 May 18 09:43 libexec
    
  • brew list maven allows us to list all installed maven packages:
    
        brew list maven  

        /usr/local/Cellar/maven/3.8.1/bin/mvn
        /usr/local/Cellar/maven/3.8.1/bin/mvnDebug
        /usr/local/Cellar/maven/3.8.1/bin/mvnyjp
        /usr/local/Cellar/maven/3.8.1/libexec/bin/ (4 files)
        /usr/local/Cellar/maven/3.8.1/libexec/boot/ (2 files)
        /usr/local/Cellar/maven/3.8.1/libexec/conf/ (3 files)
        /usr/local/Cellar/maven/3.8.1/libexec/lib/ (70 files)
    
  • brew info maven can be used to print more information about maven on Mac:
    
        brew info maven   

        maven: stable 3.8.1
        Java-based project management
        https://maven.apache.org/
        Conflicts with:
          mvnvm (because also installs a 'mvn' executable)
        /usr/local/Cellar/maven/3.8.1 (87 files, 10.7MB) *
          Built from source on 2021-05-22 at 11:10:48
        From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/maven.rb
        License: Apache-2.0
        ==> Dependencies
        Required: openjdk ✔
    

These are some useful commands that you may use:

  • brew upgrade maven to upgrade Maven

  • brew uninstall maven to uninstall Maven

in summary:

  • Pros: Easy and automatic installation

  • Cons: Technical requirements (Homebrew installation/commands)

Installing Maven from an Archive

Let’s first visit the officiel Apache Maven website and download the latest Maven binary archive.

Install mvn binary archive on mac

As we can see, the latest version is 3.8.1. Let’s click on apache-maven-3.8.1-bin.tar.gz file.

Now, we need to extract the downloaded archive. To do so, open a terminal and switch to the directory where the archive is saved.

Then, type the following command:

    
        tar -xvf apache-maven-3.8.1-bin.tar.gz
    

Please bear in mind that we can move the extracted file to any folder we want. For example, let’s move it to /Users/devwithus/:

    
        mv Downloads/apache-maven-3.8.1 /Users/devwithus/
    

Now, we’re ready to move ahead with the installation. We’ll need to set up the environment variables.

Add Maven to the PATH on Mac

The last missing piece of the puzzle is to configure MAVEN_HOME and include $MAVEN_HOME/bin to PATH.

So, let’s open .bash_profile and add the following content:

    
        JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home"
        PATH="${JAVA_HOME}/bin:${PATH}"
        export PATH
    

Type the following command to apply all the environment changes we have made:

    
        source .bash_profile
    

For macOS Catalina or higher, the default shell is zsh. So, we need to edit ~/.zshenv instead to add MAVEN_HOME environment variable and update the PATH.

Let’s open the ~/.zshenv file and append:

    
        export MAVEN_HOME=/Users/devwithus/apache-maven-3.8.1
        export PATH=$PATH:$MAVEN_HOME/bin
    

To reflect the changes, we need to issue:

    
        source ~/.zshenv
    

Now, let’s verify maven installation:

    
        mvn -v
        
        Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
        Maven home: /Users/devwithus/apache-maven-3.8.1
        Java version: 15.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home
        Default locale: en_MY, platform encoding: UTF-8
        OS name: "mac os x", version: "11.2.3", arch: "x86_64", family: "mac"
    

Congratulations, we installed Apache Maven on Mac OS X, now we are ready to use it.

We have also written an article about how to install mvn on Windows 10, feel free to share it with your friends.

Conclusion

In this short guide, we have illustrated how to install Maven on Mac.

First, we have seen how to do it using brew command, then we explained how to achieve the same thing using the mvn binary archive.