UbuntuUbuntu Based

How To Install Apache Maven on Ubuntu 24.04 LTS

Install Apache Maven on Ubuntu 24.04

Apache Maven is a powerful and widely-used build automation tool for Java projects. It simplifies the management of dependencies, builds, and documentation, making it an essential tool for Java developers. Maven’s convention-over-configuration approach streamlines the development process, allowing developers to focus on writing code rather than worrying about build configurations. In this article, we will guide you through the process of installing Apache Maven on Ubuntu 24.04 LTS, providing step-by-step instructions for both APT and manual installation methods. By the end of this tutorial, you will have a fully functional Maven setup ready to enhance your Java development workflow.

Prerequisites

Before proceeding with the installation of Apache Maven, ensure that your system meets the following requirements:

  • Ubuntu 24.04 LTS operating system
  • Sudo privileges to execute commands with administrative permissions
  • Java Development Kit (JDK) installed on your system

To verify your Ubuntu version, open a terminal and run the following command:

lsb_release -a

Make sure the output confirms that you are running Ubuntu 24.04 LTS. Additionally, check if Java is installed by running:

java -version

If Java is not installed, you can install it using the following command:

sudo apt install default-jdk

Method 1: Installing Apache Maven Using APT

The easiest way to install Apache Maven on Ubuntu 24.04 LTS is by using the APT package manager. Follow these steps to install Maven using APT:

Step 1: Update the Package Index

Before installing any packages, it’s important to update the package index to ensure you have access to the latest versions. Open a terminal and run the following command:

sudo apt update

This command retrieves the latest package information from the repositories, ensuring that you install the most up-to-date version of Maven.

Step 2: Install Maven using APT

With the package index updated, you can now install Maven using the APT package manager. Run the following command:

sudo apt install maven

APT will handle the installation process, automatically resolving any dependencies required by Maven. This makes the installation process straightforward and hassle-free.

Step 3: Verify the Installation

Once the installation is complete, verify that Maven is correctly installed by running the following command:

mvn -version

If the installation was successful, you should see the version information for Maven, along with the Java version and other relevant details.

Method 2: Installing Apache Maven Manually

If you prefer more control over the installation process or want to install a specific version of Maven, you can opt for the manual installation method. Follow these steps to install Maven manually:

Step 1: Download Maven from the Official Website

Visit the official Apache Maven download page. Scroll down to the “Files” section and copy the URL for the binary tar.gz archive of the desired Maven version.

Open a terminal and use the wget command to download the Maven archive. Replace <maven-url> with the copied URL:

wget <maven-url>

For example:

wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz

Step 2: Extract the Downloaded Archive

Once the download is complete, extract the Maven archive to the /opt directory using the following command:

sudo tar xf apache-maven-*.tar.gz -C /opt

The /opt directory is a standard location for installing optional or third-party software on Linux systems.

Step 3: Configure Environment Variables

To make Maven accessible from anywhere in the terminal, you need to set up the necessary environment variables. Create a new file called maven.sh in the /etc/profile.d/ directory:

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

Add the following lines to the file:

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

Make sure to replace apache-maven-3.9.1 with the actual directory name of your extracted Maven archive.

Save the file and exit the text editor.

Step 4: Apply Changes and Verify Installation

To apply the changes made to the environment variables, run the following command:

source /etc/profile.d/maven.sh

This command loads the new environment variables into the current shell session.

Finally, verify the Maven installation by running:

mvn -version

You should see the Maven version information, confirming that the manual installation was successful.

Configuring Environment Variables

Setting up environment variables is crucial for Maven to function properly. Here’s a detailed explanation of each variable:

  • JAVA_HOME: This variable points to the directory where the Java Development Kit (JDK) is installed. Maven requires the JDK to compile and run Java projects.
  • M2_HOME and MAVEN_HOME: These variables specify the directory where Maven is installed. They are used by Maven to locate its configuration files and dependencies.
  • PATH: The PATH variable is updated to include the bin directory of Maven. This allows you to run Maven commands from anywhere in the terminal without specifying the full path.

By setting these environment variables, you ensure that Maven can locate the necessary Java libraries and its own configuration files.

Verifying the Installation

After completing the installation process, it’s essential to verify that Maven is correctly set up. Run the following command in the terminal:

mvn -version

If the installation was successful, you should see output similar to the following:

Apache Maven 3.9.9 (c27a7f5c3dgdt1a4c1a5f4e4b4f4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d)
Maven home: /opt/apache-maven-3.9.1
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-72-generic", arch: "amd64", family: "unix"

This output confirms that Maven is installed correctly and provides information about the Maven version, Java version, and system details.

Common Issues and Troubleshooting

While installing Apache Maven on Ubuntu 24.04 LTS is generally straightforward, you may encounter a few common issues. Here are some troubleshooting tips to help you resolve them:

  1. PATH issues: If you encounter errors related to the mvn command not being found, double-check that the PATH variable is correctly set in the maven.sh file. Ensure that the Maven bin directory is added to the PATH.
  2. JDK errors: Maven requires a compatible Java Development Kit (JDK) to function properly. If you encounter JDK-related errors, verify that you have the JDK installed and that the JAVA_HOME variable points to the correct JDK directory.
  3. Permission denied: If you face permission issues while running Maven commands, ensure that you have the necessary permissions to read and execute the Maven files. You can use the sudo command to run Maven with elevated privileges.

Congratulations! You have successfully installed Apache Maven. Thanks for using this tutorial for installing Apache Maven on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Apache Maven website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button