Skip to content

Simple Guide to Install Apache Maven on Ubuntu 24.04

Advertisement

Apache Maven is a useful tool for managing Java projects. It helps with building, reporting, and organizing project files. This guide explains how to install Apache Maven on Ubuntu 24.04 in simple steps. Even if you are new to Ubuntu, you can follow this easily.

What You Need Before Starting

You need access to your Ubuntu 24.04 system through a terminal. If you are working on a remote server, connect to it using SSH with this command:

ssh user@ubuntu

Make sure your system is up to date. Run these commands to update and upgrade your packages:

sudo apt update
sudo apt upgrade -y

Step 1: Install Java

Maven needs Java to work. We will install OpenJDK 17, which is a good version for Maven. Use this command to install it:

sudo apt install openjdk-17-jdk -y

After installation, check if Java is installed correctly:

java -version

You should see something like this:

openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-0ubuntu1.24.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-0ubuntu1.24.04, mixed mode, sharing)

Step 2: Download and Install Maven

Now, download Apache Maven 3.9.11, the latest version, from its official website. Run these commands to download and extract it:

cd /usr/local
sudo wget https://www-us.apache.org/dist/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.tar.gz

Extract the downloaded file and create a shortcut for easier use:

sudo tar xzf apache-maven-3.9.11-bin.tar.gz
sudo ln -s apache-maven-3.9.11 apache-maven

Step 3: Set Up Maven Environment

To use Maven easily, you need to set some environment variables. Create a new file to store these settings:

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

Add the following lines to the file:


export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export M2_HOME=/usr/local/apache-maven
export MAVEN_HOME=/usr/local/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Save the file and exit. Then, load the settings into your current session:

source /etc/profile.d/apache-maven.sh

Step 4: Check Maven Installation

To make sure Maven is installed correctly, check its version:

mvn -version

You should see output like this:

Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: /usr/local/apache-maven
Java version: 17.0.12, vendor: Ubuntu, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.8.0-31-generic", arch: "amd64", family: "unix"

Step 5: Clean Up

Remove the downloaded file to save space:

sudo rm -f /usr/local/apache-maven-3.9.11-bin.tar.gz

Step 6: Test Maven with a Sample Project

To test Maven, create a simple project:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Go to the project folder and build it:

cd my-app
mvn package

If the build is successful, Maven is working correctly.

Tips for Using Maven

  • Check Java Path: If Maven does not work, ensure JAVA_HOME points to the correct Java folder.
  • Update Regularly: Keep your system and Maven updated for better performance.
  • Use an IDE: Tools like IntelliJ IDEA or Eclipse make working with Maven easier. Set the Maven home path to /usr/local/apache-maven in your IDE settings.

For more information about Maven, visit the official Maven website.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

8 Comments

  1. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
    export M2_HOME=/usr/local/apache-maven
    export MAVEN_HOME=/usr/local/apache-maven
    export PATH=${M2_HOME}/bin:${PATH}

  2. I followed the steps and I could get following output.

    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T00:03:14+05:30)
    Maven home: /usr/local/apache-maven
    Java version: 1.8.0_101, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-oracle/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: “linux”, version: “3.16.0-40-generic”, arch: “amd64”, family: “unix”

    But when I opened a new terminal and type mvn -version I get the following error.

    Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

  3. i am on Ubuntu and followed your instructions with the exception that i put the export statements in my .profile

    it works, but is that wrong?

    i also do not know how the system is finding the commands “java” and “mvn” without having $JAVA_HOME/bin:$MAVEN_HOME/bin in my path

    please explain, thanks

    1. Hi Dan,

      Please check the Step 3. You will find that all environment variables including PATH are configured in /etc/profile.d/apache-maven.sh file. Which is automatically loaded by the system during startup.

  4. can anyone say how to remove this maven ……………..
    even i used the remove command its not working and if i check the version again i get the same as maven 3.3.9…..
    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
    Maven home: /usr/local/apache-maven
    Java version: 1.8.0_111, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-8-oracle/jre
    Default locale: en_IN, platform encoding: UTF-8
    OS name: “linux”, version: “4.2.0-42-generic”, arch: “amd64”, family: “unix”

  5. I followed your instructions as it is. All steps done, but in the end when I do mvn -version I get the error
    -bash: mvn: command not found
    Please help

Leave a Reply

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


Back to top