Linux Ubuntu 22.04

How To Fix “mvn command not found” Error

How To Fix “mvn command not found” Error
Written by itsubuntu

How To Fix “mvn command not found” Error

You have to deal with this error “mvn command not found” error while trying to invoke the maven utility in Unix systems. Don’t worry we are here with the solutions for this error.

How To Fix “mvn command not found” Error

Before jumping into the solutions, let’s have a look into the possible cause for the “mvn command not found” error.

  • Apache Maven Not Installed on Target System
  • Missing Environment Variables

Now, let’s have a look at the solutions:

Solution 1:

One of the most common reasons for the “mvn command not found” error is  Apache Maven is not being installed on your system. To fix this, you can install Apache Maven on your system.
Run the following command to update your system and install Apache Maven:

sudo apt-get update
sudo apt-get upgrade

Next, install Maven by running the following apt command:

sudo apt-get install maven -y

Solution 2:
Another most common reason for the “mvn command not found” error is the missing environment variables. It is compulsory to set up the necessary environment variables; otherwise, you need to deal with errors.

To set up the environment variables, you need to navigate into the /etc/profile.d/ directory and create a file called maven.sh:

cd /etc/profile.d/
sudo touch maven.sh

After creating file, You need edit the file with your favorite text editor and add the following details:

export JAVA_HOME=/usr/lib/jvm/default-java

export MAVEN_HOME=/home/maven/

export M2_HOME=/home/maven/

export PATH=${M2_HOME}/bin:${PATH}

Note: You need to replace the JAVA_HOME with the path to your JDK installation and MAVEN_HOME, and M2_HOME with the directory where Maven in your system is located.

Finally, now you need to make the file executable by running the command:

sudo chmod +x /etc/profile.d/maven.sh

Finally, apply the changes  running the following command:

source /etc/profile.d/maven.sh

About the author

itsubuntu

Leave a Comment