How to Install Apache Maven on Ubuntu 22.04

Apache Maven is a powerful build automation tool widely used in Java-based projects. It simplifies the build process, manages dependencies, and facilitates project management. If you’re working on Ubuntu 22.04 and need to install Maven, you’ve come to the right place. In this blog post, we’ll guide you through the step-by-step process of installing Apache Maven on Ubuntu 22.04.

Prerequisites:

  • Pre-Installed Ubuntu 22.04
  • Sudo User with admin rights
  • Stable Internet Connectivity

Step 1: Update System Packages

To ensure that your system has the latest package information, open a terminal and run the following command:

$ sudo apt update

Enter your password when prompted and wait for the update process to complete.

Step 2: Install OpenJDK

Maven requires Java Development Kit (JDK) to run. Ubuntu 22.04 comes with OpenJDK in its default repositories. So, to Install OpenJDK, run

$ sudo apt install openjdk-19-jdk -y

apt-command-install-openjdk-ubuntu

Once the installation is complete, you can verify if Java is correctly installed by running the following command:

$ java --version

If everything is set up correctly, you should see the Java version information displayed in the terminal.

Check-Java-Version-Ubuntu-Linux

Step 3: Install Apache Maven

Maven package and its dependencies are available on the default Ubuntu 22.04 package repositories. Run following apt command to install maven from Ubuntu repositories.

$ sudo apt install maven -y

apt-install-maven-ubuntu-linux

Post installation, run beneath command to verify maven version,

$ mvn --version

Check-Maven-Verison-Ubuntu-Linux

Alternate Way to Install latest Apache Maven from Source Code

Visit the Apache Maven website (https://maven.apache.org/download.cgi) and navigate to the “Files” section. Look for the latest stable release of Maven and copy the download link of the Binary tar.gz file.

Back in the terminal, navigate to a directory where you want to store Maven. You can use the following cd command to move to the /opt directory:

$ cd /opt

Download Maven using the wget command and paste the copied download link:

$ sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz

The download process may take a few moments depending on your internet speed.

Download-Apache-Maven-SourceCode-File-Ubuntu

Extract the Maven archive with the following tar command

$ sudo tar xpvf apache-maven-3.9.2-bin.tar.gz

Above command will extract the contents of the tar.gz file into a directory named “apache-maven-*” (where * denotes the version number).

$ sudo mv apache-maven-3.9.2 apache-maven

To make maven accessible system-wide, we need to set up the environment variables. Open the /etc/environment file in a text editor using the following command:

$ sudo vi /etc/environment

Add the following lines at the end of the file:

JAVA_HOME=/usr/lib/jvm/java-19-openjdk-amd64
M2_HOME=/opt/apache-maven
MAVEN_HOME=/opt/apache-maven
PATH="$PATH:$JAVA_HOME/bin:$M2_HOME/bin"

Save and close the file and apply these environment variables by running

$ source /etc/environment

Let’s verify that Maven is correctly installed, run the following command:

$ mvn --version

If Maven is installed properly, you should see the Maven version, Java version, and other related information displayed in the terminal.

View-Maven-Version-Ubuntu-Linux

Perfect, output above confirms that apache maven has been installed successfully on Ubuntu 22.04. Kindly do post your queries and feedback in below comments section.

Leave a Comment

3 × 3 =