Ubuntu

How to Install Apache Maven on Ubuntu 20.04

Install Apache Maven on Ubuntu 20.04

Apache Maven is an open-source and free project management utility that is used for building and managing projects written in various languages. It is particularly used for the deployment of Java-based applications. It helps you to get all the necessary libraries that you need to build your application. This article explains how to install Apache Maven on the Ubuntu OS. There are following two ways to install Apache Maven on the Ubuntu system:

  • Installing Apache Maven via apt
  • Installing Apache Maven via .deb package

Note: We have tested the commands and methods discussed here on Ubuntu 20.04 LTS. You must have sudo access to install/remove the Apache Maven on your system.

Method 1: Installing Apache Maven via apt

In the following procedure, we are going to install Apache Maven via apt:

1. Update system’s repository index using the command below in Terminal:

$ sudo apt-get update

Enter the password for sudo.

2. Then use the following command in order to install Apache Maven:

$ sudo apt-get install maven

Now you might be provided with the Y/n option, press Y to carry on the installation of Apache Maven. Then the installation will be started on your system.

3. Once the installation is completed, you can verify it by using the command below in Terminal:

$ mvn -version

After running the above command, you will receive the following similar output:

The above output also displays the installed version of Apache Maven which is 3.6.3.

Now Apache Maven has been successfully installed and is ready to use.

Method 2: Installing Apache Maven from the official website

In the following procedure, we are going to download and install Apache Maven from its official website:

1. Before installing Apache Maven, first you will need to install JDK. To install JDK, update the system repository index using the below command:

$ sudo apt update

Then install JDK as follows:

$ sudo apt install default-jdk

2. To verify if JDK has been installed, issue the below command in Terminal:

$ java --version

The following output verifies that JDK has been successfully installed on your system,

3. Now, you will need to download the Apache Maven from its official website. Issue the command below in Terminal to download the most recent release:

$ wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp

This command will save the downloaded Apache Maven archive file apache-maven-3.6.3-bin.tar.gz in the /tmp directory.

4. Now, extract the archive using the below command in Terminal:

$ tar -xvzf apache-maven-3.6.3-bin.tar.gz

This command will extract the file to the apache-maven-3.6.3 directory.

5. Now, copy the extracted directory apache-maven-3.6.3 to /opt/ as follows.

$ cp -r apache-maven-3.6.0 /opt/maven

6. Now, you will need to setup the environment variables. To do this, create a script file maven.sh in the /etc/profile.d directory using the below command in Terminal:

$ sudo nano /etc/profile.d/maven.sh

Add the below lines in the script:

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

export M2_HOME=/opt/maven

export MAVEN_HOME=/opt/maven

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

Now you can save and exit this script.

7. Now use the below command to give the script executable permissions:

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

8. Load the environment variables using the below command:

$ source /etc/profile.d/maven.sh

Now the installation of Apache Maven has been completed. You can verify it by running the below command in Terminal:

$ mvn -version

After running the above command, you will receive the following similar output which verifies that Maven version 3.6.3 has been successfully installed.

Uninstall Maven

In case you no longer need Maven on your system, you can easily uninstall it using the below command in Terminal:

$ sudo apt-get remove maven

By following any of the two methods discussed above, you can easily install Apache Maven on the Ubuntu system. We have also shared how to uninstall Apache Maven from Ubuntu if you ever need to do so.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *