Ubuntu

How to Install Apache Maven on Ubuntu 22.04

Apache Maven is an open-source utility that enables you to automate the development procedure of Java projects. It can also be utilized for Ruby, C#, and other programming languages. In Linux-based systems such as Ubuntu 22.04, Apache Maven is a well-known tool for developing projects related to Java. It is hosted by the maven project, which was once a part of the Jakarta Project until splitting off.

This write-up will demonstrate the method of installing Apache Maven on Ubuntu 22.04 using two different methods. So, let’s start!

Method 1: Installing Apache Maven on Ubuntu 22.04 using apt

You must follow the below-given step-by-step instructions for installing Apache Maven on Ubuntu 22.04 using the official APT repository.

Step 1: Update system repositories
First of all, open up the terminal by hitting “CTRL+ALT+T” in Ubuntu 22.04 and write out the below-given commands for updating the system repositories:

$ sudo apt update

Upgrade the system packages as well:

$ sudo apt upgrade

Step 2: Apache Maven Installation on Ubuntu 22.04
In the next step, execute the following command for the installation of Apache Maven on Ubuntu 22.04 system:

$ sudo apt install maven -y

Wait for a few minutes as the Apache Maven will take some time to install:

Step 3: Check Apache Maven version
Lastly, verify the version of Apache Maven which you have installed on your Ubuntu 22.04:

$ mvn -version

Now, let’s check out the manual procedure of Apache Maven installation.

Method 2: Installing Apache Maven on Ubuntu 22.04 using Manual method

For experienced Linux users, the manual method for installing Apache Maven is more suitable as it permits you to download and install the latest version.

Want to manually install Apache Maven? Follow the below-given procedure.

Step 1: Install Java
First of all, make sure that Java is installed on your Ubuntu 22.04; if you do not have it, then utilize the given command for its installation:

$ sudo apt install default-jdk -y

Step 2: Download Apache Maven latest version
Next, write-out the below-given “wget” command for downloading the latest version of Apache Maven from the official website:

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

Step 3: Extract downloaded Apache Maven package
Now, move to the “/tmp” directory with the help of the “cd” command:

$ cd /tmp

Then, execute the following command to extract the downloaded Apache Maven archive file:

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

Wait for a few minutes:

After extracting the required package, copy it to the “/opt/” directory:

$ sudo cp -r apache-maven-3.6.3 /opt/maven

Step 4: Set up environment variables
In the next step, we will set up the environment variables by creating a maven script file “maven.sh” inside the “/etc.profile.d” directory:

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

In the opened “maven.sh” file, paste the follow code and press “CTRL+O” to save it:

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Next, write out the following command for enabling the executable permission of the created Apache Maven script:

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

After doing so, load the environment variables with the help of the following “source” command:

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

Step 5: Check Apache Maven version

Lastly, verify the version of Apache Maven which you have installed on your Ubuntu 22.04:

$ mvn -version

How to uninstall Apache Maven from Ubuntu 22.04

Want to uninstall Apache Maven from Ubuntu 22.04? If yes, then utilize the following command:

$ sudo apt remove maven

We have compiled the two simplest methods for the installation of Apache Maven on Ubuntu 22.04. Choose any of them according to your preferences.

Conclusion

For Apache Maven installation, you can either utilize “apt” or the manual installation method. In order to use “apt”, open your Ubuntu 22.04 terminal and execute the “$ sudo apt install maven -y” command. While in the manual installation, you have to download the archive file from the official website, extract it, and set the environment variables accordingly. This write-up discussed two methods for the installation of Apache Maven on Ubuntu 22.04.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.