2 Ways of Installing Apache Maven on Ubuntu

install maven in ubuntu

Java is a popular choice for building business applications. In addition to following the software development best practices, the performance of these projects depends on how these projects are deployed. 

Apache Maven is an open-source project management tool that uses the Project Object Model (POM) methodology, which stores project, configuration, and dependency information in an XML file. 

In this comprehensive tutorial, we will cover how to install Maven in Ubuntu 22.04. We will cover two ways of how to install maven in ubuntu system. 

Let’s start with a brief overview of Maven. 

What is Maven?

Maven is a popular tool for project management and build automation within a Java environment. Its purpose is to make the development and administration of Java-based applications easier by automating repetitive tasks. Maven provides a standardized way to manage project structures, dependencies, and the build lifecycle.

History of Maven

Maven was created by Jason van Zyl in 2002. The goal was to create a more streamlined and standardized way of building projects in the Java ecosystem. 

Before Maven, it was difficult for engineers to collaborate easily across projects since different build tools had distinct setups. Maven aimed to provide a standard structure, build lifecycle, and dependency management system for Java projects.

“Maven” comes from the Yiddish word for “accumulator of knowledge.” It reflects the tool’s purpose of accumulating and managing project knowledge, configurations, and dependencies.

Features of Maven:

  1. Project Object Model (POM): The Project Object Model used by Maven is an XML file called pom.xml. This file describes the project’s configuration details, dependencies, and other settings. The POM serves as the project’s blueprint and helps Maven understand how to build the project.
  2. Dependency Management: Maven makes managing dependencies in a project more manageable. Maven will automatically download and include the necessary libraries from a central repository if you specify dependencies in the pom.xml file.
  3. Build Lifecycle Management: Maven defines a set of standard build phases and goals for a project. Typical phases of the standard buildcycle include Compile, Test, Package, and Install. Each phase consists of one or more goals executed in a specific order. Maven automatically manages these build phases, making it easy to build and package Java projects.
  4. Plugins: Maven’s plugin architecture enables programmers to add new features and alter the build procedure. An array of plugins provides additional functionality, such as code generation, testing, and documentation generation.
  5. Central Repository: The central Maven repository contains a huge array of open-source libraries and dependencies. When you declare a dependency in your project, Maven can automatically download the required artifacts from this central repository.
  6. Consistency: By enforcing a uniform project structure, Maven facilitates developers’ comprehension of and the ability to contribute to various projects. This consistency helps with collaboration and reduces the learning curve between different Maven-based projects.

How to Install Apache Maven on Ubuntu 22.04

Installing Apache Maven on Ubuntu is a simple procedure that allows developers to manage Java projects. We will now discuss two easy ways of installing Maven so that you can start harnessing its capabilities for streamlined project building and dependency management on the Ubuntu platform.

The Prerequisites

Before you try the following installation steps, make sure you have the following:

  • A machine with Ubuntu 22.04
  • A user account with administrator or sudo privileges

Method #1: Use apt to install Maven on Ubuntu

The standard method of installing Maven in Ubuntu is to use apt, the native package manager of the distribution. Here are the steps of this process:

Step #1: Update the System Package Index

Start by updating the current system package index to ensure you have the latest version of the packages. 

# sudo apt update

Step #2: Install the Maven Package

Next, run the following command to fetch and install the Maven package available in the official Ubuntu repository: 

# sudo apt install maven

sudo apt install maven

When prompted, enter Y and press Enter to confirm the installation.

Step #3: Verify the Installation

When the process finishes, we recommend verifying the installation by checking the version of Maven:

# mvn -version

mvn version

Method #2: Install Maven’s Most Recent Version on Ubuntu 22.04

In some cases, the Maven’s package version in the official Ubuntu might not be the latest. This is because the Ubuntu maintenance team prefers stability over the most recent features.

However, if you wish to install the most recent version on your Ubuntu system, we recommend the following steps. 

Step #1: Set Up OpenJDK

Start by updating the system package index with the following command:

# sudo apt update

Next, install the latest version of OpenJDK on your system:

# sudo apt install default-jdk

sudo apt install default jdk

When prompted to confirm the installation, type Y and hit Enter.

Step #2: Verify OpenJDK Version

Verify the installation by printing the OpenJDK version information with the following command:

# java -version

java version

Step #3: Download the Latest Maven Version

Go to the Maven download page and select the most recent Maven version from the Files section. Copy the link of the tar.gz file. 

Next, launch the terminal and navigate to the /tmp directory.

Run the following wget command to download the file from the link you copied in the previous step.

# wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz -P /tmp

wget

Step #4: Extract the Archive

Navigate to the /opt directory with the cd command. Next, run the following command to extract the archive: 

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

Step #5: Create a Symbolic Link

Once the extraction finishes, create a symbolic link named maven that points to the installation directory:

# sudo ln -s /opt/apache-maven-3.9.6 /opt/maven

Step #6: Configure Environment Variables

We will now set up the environment variables for Maven installation. 

Go to the /etc/profile.d/ directory, create and open the maven.sh script file in your preferred text editor. We will use the following command to create and edit the file in Vi:

# sudo vi /etc/profile.d/maven.sh

Add the following export statement lines in the maven.sh file to make the script executable:

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

Finally, save the file and exit the editor. If you used Vi, press Esc+wq!, then hit Enter.

cat

 Next, use the chmod command to give the maven.sh file executable permission:

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

Finally, run the following source command to load the configuration from the maven.sh file:

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

sudo chmod

Step #7: Verify the Maven Installation

Print the Maven version information to verify the installation. 

# mvn -version

mvn version

If you are in a Windows environment, you can follow our tutorial on installing Maven on Windows.

Conclusion

Installing Apache Maven on Ubuntu is a straightforward process that enhances the efficiency of Java project management. By following the outlined steps, users can quickly set up Maven, benefitting from its standardized project structure, dependency management, and build automation features. This installation ensures a seamless development experience and empowers developers to focus on building high-quality software.

At RedSwitches, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is Apache Maven?

Apache Maven is a software project management and comprehension tool. It is primarily used for managing a project’s build process, including compilation, testing, and code packaging.

Q. How do I install Apache Maven on Ubuntu 18.04 and Ubuntu 20.04?

You can install Apache Maven on Ubuntu 20.04 using the apt package manager. Simply run the command “sudo apt install maven” in your terminal to install it.

Q .What are the steps to install Apache Maven on Ubuntu?

To install Apache Maven on Ubuntu, follow these steps: 1. Update the package index using “sudo apt update” 2. Install Apache Maven with “sudo apt install maven” 3. Verify the installation with the “mvn -version” command.

Q. How do I install the latest version of Apache Maven on Ubuntu?

To install the latest version of Apache Maven on Ubuntu, you can first ensure that the most recent version is available in the official Ubuntu repository. If not, you may manually download and install it from the Maven download page.

Q. Can I install Apache Maven on other Linux distributions?

Apache Maven can be installed on other Linux distributions using the respective package managers or manually downloading and installing the Maven binaries.

Q. How can I install Apache Maven on Ubuntu 22.04?

The installation process for Apache Maven on Ubuntu 22.04 is the same as for Ubuntu 20.04. You can use the apt package manager to install Maven on Ubuntu 22.04.

Q. What is the recommended Java Development Kit (JDK) for use with Apache Maven?

It is recommended to have a Java Development Kit (JDK) installed on your system when using Apache Maven. You can use OpenJDK or Oracle JDK with Maven.

Q. How do I install Apache Maven on Windows?

Instructions for installing Apache Maven on Windows may vary. However, you can typically download the Maven binaries from the Apache Maven website and then follow the installation instructions provided.

Q. Where can I find the Apache Maven download page?

The Apache Maven download page can be accessed from the official Apache Maven website. It provides access to the latest version of Maven and other relevant resources.

Q. What is the “mvn -version” command used in Apache Maven?

 The “mvn -version” command verifies the installation and displays your system’s currently installed version of Apache Maven.

Pranita

Pranita Kulkarni is a System Administrator at RedSwitches (Leading provider of the best managed enterprise web hosting solutions). She possesses excellent problem-solving skills in critical server management and administration. She is a great cook and loves sketching when she can find the time. Reach her at [email protected]

Related articles

Latest articles