How to Install Maven on Ubuntu 22.04

Introduction

Before we begin talking about how to install Maven, let's briefly understand - What is Maven?

Maven is a powerful build automation tool used primarily for Java projects. It simplifies the development process by managing project dependencies, compiling source code, and packaging applications. With Maven, developers can easily configure and manage projects, making it ideal for large-scale software development.

It promotes a standardized project structure and provides a wide range of plugins for various tasks. Whether you're a beginner or an experienced developer, Maven streamlines your workflow and improves project efficiency.

In this tutorial, you will install Maven on Ubuntu 22.04. We will also address a few FAQs on how to install Maven on Ubuntu 22.04.

Advantages of Maven

  1. Dependency Management: Maven simplifies handling project dependencies, automatically downloading and managing the required libraries.
  2. Build Automation: Maven automates the build process, making it easier to compile, test, and package applications.
  3. Project Structure: Maven promotes a standardized project structure, enhancing code organization and maintainability.
  4. Plugin Ecosystem: Maven offers a vast array of plugins for different tasks, enabling developers to extend its functionality easily.
  5. Continuous Integration: Maven integrates seamlessly with popular CI tools like Jenkins, facilitating automated testing and deployment workflows.

Perquisites to Install Maven on Ubuntu 22.04

  • A user with sudo privileges

Step 1 - Install Apache Maven on Ubuntu with Apt

1) Firstly, update the package index:‌

sudo apt update

2) After that, install Maven by running the following command:‌

sudo apt install maven

3) You will have to verify the installation. Do it by running the mvn -version command:‌

mvn -version

You will get the following output:‌

Output 

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.19, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.19.0-1025-aws", arch: "amd64", family: "unix"

Installation of Maven is now complete.

Step 2 - Install the Latest Version of Maven

1) You will now install the OpenJDK. OpenJDK is the default Java development and has good runtime in Ubuntu 18.04. Start the installation of Java by updating the package index. Run the command:‌

sudo apt update

2) Now, install the OpenJDK package by running:‌

sudo apt install default-jdk

‌3) After that, you will verify the installation. Do it by running the following command:‌

java -version

The output will look like this:‌

Output 

openjdk version "11.0.19" 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1, mixed mode, sharing)

4) After that, check the download page of Maven, if a newer version is available. Start by downloading Maven in the /tmp directory, by the following wget command:‌

wget https://downloads.apache.org/maven/maven-3/3.9.3/binaries/apache-maven-3.9.3-bin.tar.gz

5) After the download is complete, then extract the archive in the /opt directory.‌

sudo tar xf apache-maven-3.9.3-bin.tar.gz -C /opt

6) For more control over Maven versions and updates, create a symbolic-link maven. It will directly point to the Maven installation directory.‌

sudo ln -s /opt/apache-maven-3.9.3 /opt/maven

7) To upgrade your Maven installation later, simply unpack the newer version and then change the symlink to point to that version.

8) Next, you will set up the environment variables. Create a new file named mavenenv.sh inside /etc/profile.d/ directory.‌

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

9) After that, paste the following configurations in /etc/profile.d/maven.sh.‌

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

10) You will then save and close the file. The script will get sourced at the shell startup. Further, make the script executable with the chmod.‌

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

11) Finally, load the environment variables using source command with the following command.‌

source /etc/profile.d/maven.sh

12) After that, you need to verify the installations. You will do it using the mvn -version command, which will print the Maven version:‌

mvn -version

You will get the following output:‌

Output

Apache Maven 3.9.3 (21122926829f1ead511c958d89bd2f672198ae9f)
Maven home: /opt/maven
Java version: 11.0.19, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.19.0-1025-aws", arch: "amd64", family: "unix"

FAQs to Install Maven on Ubuntu 22.04

How do I set up the Maven environment variables on Ubuntu 22.04?

Edit the /etc/environment file and add the line: MAVEN_HOME=/usr/share/maven. Save the file and restart your terminal or system for the changes to take effect.

Can I install a specific version of Maven on Ubuntu 22.04?

By default, Ubuntu's package manager installs the latest version of Maven. However, you can manually install a specific version by downloading it from the Apache Maven website and following the installation instructions.

How do I update Maven to the latest version on Ubuntu 22.04?

To update Maven to the latest version on Ubuntu 22.04, you can use the package manager by running the command: sudo apt update && sudo apt upgrade maven.

How do I uninstall Maven from Ubuntu 22.04?

To uninstall Maven from Ubuntu 22.04, open a terminal and run the command: sudo apt remove maven.

Where can I find the Maven configuration file on Ubuntu 22.04?

The Maven configuration file, settings.xml, can be found in the /etc/maven director.

Can I use Maven with other programming languages besides Java?

While Maven is primarily used for Java projects, it can also be used for projects in other programming languages like C#, Ruby, and more, with the help of appropriate plugins.

Are there any alternatives to Maven for build automation on Ubuntu 22.04?

Yes, there are alternatives like Gradle and Ant, which offer similar build automation capabilities. You can choose the tool that best suits your project's requirements and your familiarity with the tool.

Conclusion

We hope this detailed tutorial helped you to install Maven on Ubuntu 22.04. To learn more about Maven installation, check out the official Maven Documentation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.