How to install Apache Maven on Ubuntu 20.04

Apache Maven is a powerful build automation and dependency management tool developed by the Apache Software Foundation.Apache Maven is a widely adopted build automation and dependency management tool that provides a consistent and streamlined approach to building Java projects. Its emphasis on convention, standardization, and automation helps developers save time and effort in the software development lifecycle. Maven utilizes a Project Object Model (POM) represented by an XML file called pom.xml. The POM contains project configuration information, including project metadata, dependencies, plugins, repositories, and build profiles. Maven uses this declarative configuration to manage project builds and dependencies consistently across different environments.

Prerequisites

Before installing Apache Maven on Ubuntu 20.04, ensure that you have the following prerequisites:

  1. Java Development Kit (JDK): Apache Maven requires Java to be installed on your system. Ensure that you have a JDK installed, preferably JDK 8 or a later version. You can verify your Java installation by running java -version in a terminal. If Java is not installed, you can install it using the following command:
   sudo apt install default-jdk
  1. Internet Connection: The installation process for Apache Maven requires an active internet connection. Maven will download dependencies from remote repositories during the build process.
  2. Package Manager: Ensure that your Ubuntu 20.04 system has a package manager, such as apt, set up and functioning correctly. The package manager is used to install Maven and its dependencies.

With these prerequisites met, you should be ready to proceed with the installation of Apache Maven on Ubuntu 20.04.

Install Apache Maven on Ubuntu 20.04.

you can follow these steps:

Update the package index:

sudo apt update

Install Apache Maven using the apt package manager:

sudo apt install maven

Verify the installation by checking the Maven version:

mvn --version

Apache Maven should now be installed on your Ubuntu 20.04 system.

Apache Maven should now be installed on your Ubuntu 20.04 system. You can start using Maven by creating a pom.xml file in your project directory and executing Maven commands, such as mvn clean install, within your project directory. Maven will automatically download dependencies and execute the specified build tasks based on the configuration in the pom.xml file.

Install the latest release of Apache Maven on Ubuntu 20.04.

you can follow these steps:

  1. Update the package index:
sudo apt update
  1. Install Apache Maven using the apt package manager:
sudo apt install maven

At this point, you will have installed the version of Apache Maven available in the Ubuntu package repositories, which may not always be the latest release.

If you specifically need to install the latest release of Apache Maven, you can follow these alternative steps:

  1. Download the latest Apache Maven release from the official Apache Maven website (https://maven.apache.org/download.cgi). Look for the “Binary zip archive” for the latest version.
  2. Open a terminal and navigate to the directory where you downloaded the Apache Maven release.
  3. Extract the downloaded archive using the unzip command. Replace apache-maven-x.x.x with the actual version you downloaded:
unzip apache-maven-x.x.x-bin.zip
  1. Move the extracted Maven directory to the /opt directory (you may need to provide administrator privileges):
sudo mv apache-maven-x.x.x /opt/
  1. Set up the environment variables by editing the /etc/environment file:
sudo nano /etc/environment

Add the following line at the end of the file:

PATH="/opt/apache-maven-x.x.x/bin:$PATH"

Save the file and exit the editor.

  1. Reload the environment variables:
source /etc/environment
  1. Verify the installation by checking the Maven version:
mvn --version

You should see the latest version of Apache Maven and related information printed on the console if the installation was successful.

By following these steps, you will have installed the latest release of Apache Maven on your Ubuntu 20.04 system.

Download Apache Maven.

you can follow these steps:

  1. Visit the official Apache Maven website at https://maven.apache.org/.
  2. Navigate to the “Download” section of the website.
  3. Look for the latest stable release of Apache Maven and click on the download link next to it. You will typically find it under the “Binary zip archive” or “Binary tar.gz archive” section.
  4. Choose the appropriate archive format based on your preference or system compatibility.
  5. Once you click on the download link, the archive file will start downloading to your local machine.
  6. After the download is complete, navigate to the directory where the file was downloaded.
  7. Extract the downloaded archive using a suitable extraction tool. For example, if you have downloaded a zip file, you can use the following command in the terminal:
   unzip apache-maven-x.x.x-bin.zip

Replace x.x.x with the specific version number you downloaded.

  1. The extracted archive will contain the Apache Maven distribution files.

At this point, you will have downloaded and extracted Apache Maven. You can proceed with installing and configuring Maven as per your requirements.

Set up the environment variables for Apache Maven.

you can follow these steps:

  1. Open a terminal on your Ubuntu 20.04 system.
  2. Determine the directory path where you have extracted the Apache Maven distribution files. Let’s refer to this directory as [MAVEN_HOME].
  3. Edit the .bashrc file using a text editor such as nano or vi:
nano ~/.bashrc
  1. Add the following lines at the end of the .bashrc file to set the M2_HOME and PATH variables:
export M2_HOME=[MAVEN_HOME]
export PATH=$M2_HOME/bin:$PATH

Replace [MAVEN_HOME] with the actual directory path where you extracted Apache Maven in step 2.

  1. Save the changes and exit the text editor.
  2. Update the current terminal session with the modified .bashrc file:
source ~/.bashrc
  1. Verify that the environment variables are set correctly by running the following command:
mvn --version

You should see the Apache Maven version and related information displayed in the terminal if the environment variables are configured correctly.

By following these steps, you have set up the environment variables for Apache Maven on your Ubuntu 20.04 system. This allows you to use Maven commands from any terminal session without specifying the full path to the Maven executable.

Verify the installation of Apache Maven on your Ubuntu 20.04 system.

you can follow these steps:

  1. Open a terminal on your Ubuntu system.
  2. Run the following command to check the Maven version:
mvn --version

If Maven is correctly installed and the environment variables are set up properly, you should see the Maven version and related information displayed in the terminal.

The output should resemble something like this:

Apache Maven x.x.x
Maven home: /path/to/maven
Java version: x.x.x_xxx, vendor: Oracle Corporation, runtime: /path/to/java
Default locale: en_US, platform encoding: UTF-8
...

Make sure that the Maven version matches the one you installed or expect to see.

If you encounter any issues or receive an error message, double-check the installation steps and ensure that the environment variables (M2_HOME and PATH) are correctly set.

Conclusion.

we have covered the steps to install Apache Maven on Ubuntu 20.04. By following the provided instructions, you should have successfully installed Maven on your system and can now proceed to use it for managing your Java projects.

If you encounter any issues during the installation process or have any further questions, feel free to ask. We are here to assist you.

Hope you like this blog….
Mahesh Wabale

Leave a Comment