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

If you are a Windows 10 user, please refer to 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 a 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
    

As we can see in the log, Maven is installing. That’s all it takes to install mvn on Mac using brew command.

Please bear in mind that brew will install the latest version which is now maven@3.8.

install maven on Mac using brew

Homebrew provides a convenient command to search all available maven formula (packages): brew search maven

    
        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
    

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

    
        brew search maven 3

        ==> Formulae
        maven@3.0    maven@3.1    maven@3.2    maven@3.3    maven@3.5
    

That way, we can choose and install a specific mvn version.

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

    
        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: "mac os x", version: "11.2.3", arch: "x86_64", family: "mac"
    

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

    
        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 the version of maven installed 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 other useful commands that we 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 File

First, we need to visit the official Apache Maven website and download the latest Maven binary archive.

Install mvn mac using binary archive

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 file 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 variable: MAVEN_HOME

Add Maven to the Environment PATH on Mac

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

Adding MAVEN_HOME for macOS Mojave and Before

macOS 10.14 Mojave and all above versions use bash as the default shell.

So, to add maven environment variable, we need to open .bash_profile and paste the following content:

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

Type the following command to apply all the changes:

    
        source .bash_profile
    

Adding MAVEN_HOME for HighSierra

We have to additionally add /Users/devwithus/apache-maven-3.8.1 to the .bashrc file for macOS HighSierra:

    
        nano ~/.bashrc
        export PATH=$PATH:/Users/devwithus/apache-maven-3.8.1/bin
    

Then, we can type bash to apply the new setup.

Adding MAVEN_HOME for macOS Catalina or Later

For macOS Catalina or higher, the default shell is zsh and not bash. So, we need to edit ~/.zshenv instead to add MAVEN_HOME to 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 run:

    
        source ~/.zshenv
    

Verify Maven Installation on Mac

Now that we have explained every step of maven installation process, let’s see how to check if maven is installed.

    
        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"
    

mvn -v command does the trick for us. Alternatively, we can use mvn -version.

As we can see in the output, we have installed Maven on Mac OS X, now we are ready to use it.

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.