In this article we are going to cover the steps to install maven on ubuntu 20.04/22.04 LTS. So, please follow the below commands for safe installation.

What is Maven?

Apache Maven is a build automation and project management tool used primarily for Java projects. It simplifies the process of building, packaging, and managing software projects by providing a standardized project structure and dependency management. Maven uses XML-based configuration files (POM files) to define project settings and dependencies, enabling developers to easily compile, test, package, and deploy their applications. Its vast ecosystem of plugins and a central repository for dependencies make it a popular choice for Java development.

Prerequisites

  • A system running Ubuntu 20.04.
  • A working Internet connection.
  • Access to an account with sudo privileges.
  • Access to the terminal window.
  • Access to a text editor such as Nano.

Steps to Install Maven on Ubuntu with apt

To install Maven on Ubuntu using the apt package manager, you can follow these simple steps:

Step #1: Update the Package Repository

Open a terminal window and update the package repository to ensure you have the latest information about available packages:

ubuntu@Rushi-InfoTech:~$ sudo apt-get update

Step #2: Install Default jdk using apt

Use the ‘apt’ command to install Java

ubuntu@Rushi-InfoTech:~$ sudo apt install default-jdk

Step #3: Install Maven

Use the ‘apt’ command to install Maven

ubuntu@Rushi-InfoTech:~$ sudo apt install maven

Step #4: Verify the Installation

After the installation is complete, you can verify that Maven is installed by checking its version

ubuntu@Rushi-InfoTech:~$ mvn -version

Step #5: Uninstall maven on ubuntu using apt(Optional)

To uninstall maven on ubuntu using apt, please enter the below command

ubuntu@Rushi-InfoTech:~$ sudo apt remove maven

Verify the Maven uninstallation on ubuntu by the below command

ubuntu@Rushi-InfoTech:~$ mvn --version

Steps to Install the Latest Release of Maven on Ubuntu 20.04/22.04 LTS

To install the latest release of Apache Maven on Ubuntu, you can use the following steps. Note that this method involves manually downloading and installing Maven.

Step #1: Update the Package Repository

Open a terminal window and update the package repository to ensure you have the latest information about available packages:

ubuntu@Rushi-InfoTech:~$ sudo apt-get update

Step #2: Install default jdk by using apt

Use the ‘apt’ command to install Java

ubuntu@Rushi-InfoTech:~$ sudo apt install default-jdk

Step #3: Download the Latest Maven Release

Visit the official Apache Maven website to check for the latest version: https://maven.apache.org/download.cgi. Copy the link to the binary tar.gz file for the latest version.

Step #4: Download and Extract Maven

In the terminal, use the ‘wget’ command to download the Maven binary tar.gz file using the link you copied in the previous step. Replace ‘<maven_version>’ with the actual version number.

ubuntu@Rushi-InfoTech:~$ wget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz -P /tmp
  • Next, extract the downloaded tar.gz file using the following command:
ubuntu@Rushi-InfoTech:~$ sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
  • Create a symbolic link called maven leading to the Maven installation directory:

Replace the ‘apache-maven-3.9.5’ with the downloaded version

ubuntu@Rushi-InfoTech:~$ sudo ln -s /opt/apache-maven-3.9.5 /opt/maven

Step #5: Set Up Environment Variables

  • Use the Nano text editor to create and open the maven.sh script file in the /etc/profile.d/ directory:
ubuntu@Rushi-InfoTech:~$ sudo nano /etc/profile.d/maven.sh
  • Add the following lines to the maven.sh file:
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Press Ctrl + X, then type Y and press Enter to save changes to maven.sh.

  • Make the maven.sh file executable using the ‘chmod’ command
ubuntu@Rushi-InfoTech:~$ sudo chmod +x /etc/profile.d/maven.sh
  • Execute the maven.sh script file with the source command to set up the new environment variables
ubuntu@Rushi-InfoTech:~$ source /etc/profile.d/maven.sh

Step #6: Verify Maven Installation

Check the current version of Maven to verify the installation:

ubuntu@Rushi-InfoTech:~$ mvn --version

Conclusion

installing Apache Maven on Ubuntu 20.04/22.04 LTS is a straightforward process that empowers developers to efficiently manage and build Java projects. By following the steps outlined in this guide, users can quickly set up Maven and ensure it’s ready for use. Whether you’re a seasoned developer or just getting started, having Maven at your disposal simplifies the Java project development and management process on Ubuntu.

Reference:

For Apache Maven Documentation please visit official website.

Any queries pls contact us @Rushi-InfoTech

Similar Posts

Leave a Reply

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