Signup/Sign In

How to Install Maven on Windows, Linux, and Mac

Maven is a command-line tool used for building and managing Java-based applications. We can get an archive containing all the files needed to set up Maven. There is no need for an installer. We need to set up the prerequisites and configure our environment to run Maven.

Maven is written using Java, so make sure that your system has Java installed in it.

Run the following command to make sure that Java is installed on your system. Install the latest JDK and add the JAVA_HOME environment variable, if Java is not installed.

java -version

Installing Maven On Windows

Maven Binary Files

  • Unzip the contents where you want to store the Maven files.
  • Next, add the M2_HOME and MAVEN_HOME variables to the Windows environment variables by using the system properties. Point them to where you stored your Maven files.
  • The last step includes updating the PATH variable to run Maven commands from anywhere. We need to append the Maven bin folder - %M2_HOME%\bin - to the path variable.
  • We can confirm whether Maven was successfully set up or not by running the following command to check its version.
mvn -version

Installing Maven On Linux

  • To install Maven on Linux, head over to the official Apache Maven Project website(https://maven.apache.org/download.cgi) and download the binary tar.gz file. Extract the archive to the folder where you want to use Maven.

Maven tar.gz file

  • Next, open the terminal and use the following commands to set the environment variables. Note that the M2_Home path will have to correspond with the location of the extracted Maven files.
export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.1
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m
  • Next, we need to append the M2 variable to the path.
export PATH=$M2:$PATH
  • To confirm whether Maven was successfully added or not, use the following command.
mvn -version

Installing Maven on Ubuntu

We can easily install Maven on Ubuntu by using the apt command.

  • First, update the package index by running the following command.
sudo apt update
  • Next, install Maven by using the following apt command.
sudo apt install maven
  • The above command can take a few minutes to download and install Maven. Once done, we can verify the installation by using the following command.
mvn -version

Installing Maven on Mac OS X

  • Head over to the official Apache Maven Project website(https://maven.apache.org/download.cgi) and download the latest Maven binary tar.gz file. Extract it to the location where you want Maven to live.

Maven tar.gz file

  • Next, we need to set the environment variables. We need to add the Maven bin directory to the Path variable. Open .bash_profile in your text editor and add the following commands to it.
export M2_HOME="/Users/User-1/Downloads/apache-maven-3.8.1"
export PATH=${PATH}:${M2_HOME}/bin
  • Use the source .bash_profile command to apply the changes. To verify the installation, run the following mvn command.
mvn -version

Using Homebrew

If you have Homebrew installed, then you can simply run the following command to install Maven.

brew install maven

To confirm the installation, run the mvn version command.

Summary

Maven is a popular build and dependency management tool for Java applications. Installing and setting up Maven is not very complicated. We can download the necessary files from the Apache Maven project website. In this tutorial, we learned how to install Maven on different operating systems.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.