How to Install Maven on Ubuntu 22.04: A Step-by-Step Guide

Maven is a powerful build automation tool primarily used for Java projects. To run Maven, you’ll need to have Java installed on your system. In this guide, we’ll show you how to install Maven on Ubuntu 22.04, using JDK 17 as the Java version. Let’s get started!

Prerequisites

  • A system running Ubuntu 22.04
  • Root or sudo access to install software

Step 1: Update Your System

Before installing new software, it’s a good practice to update your system’s package index. Open your terminal and run:

sudo apt-get update -y

Step 2: Install JDK 17

Maven requires Java to function, so the next step is to install the Java Development Kit (JDK). Here, we’re installing JDK 17, the current Long-Term Support (LTS) version. Run the following command:

sudo apt-get install openjdk-17-jdk -y

Step 3: Verify Java Installation

To confirm that JDK 17 has been installed correctly, check the Java version with:

java -version

You should see an output similar to this:

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

Step 4: Install Maven

To get the latest version of Maven, we’ll install it from the official binary instead of using the default package, which may be outdated.

    1. Visit the Apache Maven download page and copy the link for the latest “Binary tar.gz archive”.
    2. Download the Maven binary using the wget command:
wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
    1. Extract the downloaded archive to the /opt directory:
sudo tar xvzf apache-maven-3.9.2-bin.tar.gz -C /opt/
    1. Set up the environment variables by editing the .profile file. Open the file with:
nano ~/.profile
    1. Add the following lines to set the Maven home directory and update the system PATH:
M2_HOME='/opt/apache-maven-3.9.2'
PATH="$M2_HOME/bin:$PATH"
export PATH
    1. Save the changes and exit the editor. Apply the new environment variables with:
source ~/.profile
  1. Alternatively, you can log out and log back in to apply the changes.

Step 5: Verify Maven Installation

To confirm Maven has been installed successfully, check the version:

mvn -version

You should see an output similar to:

Apache Maven 3.9.2 (c9616018c7a021c1c39be70fb2843d6f5f9b8a1c)
Maven home: /opt/apache-maven-3.9.2
Java version: 17.0.7, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-71-generic", arch: "amd64", family: "unix"

Conclusion

You have successfully installed Maven on Ubuntu 22.04 along with JDK 17. Maven is now ready to be used for your Java projects. Whether you’re setting up new builds or managing dependencies, Maven provides a robust and flexible solution for your development needs. By following this guide, you’ll ensure that your installation is up to date and configured correctly, optimized for both usability and performance.

Facebook
Pinterest
Twitter
LinkedIn

Leave a Reply

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