How to Install Maven on Ubuntu?

how to install maven on Ubuntu

Maven is one of the most popular build automation tools in the world of Java development. Whether you’re a beginner trying to compile and manage Java projects or an experienced developer setting up a robust environment, Maven simplifies your build processes significantly. If you’re using Ubuntu as your development environment, you’ve come to the right place. This guide will walk you through the steps of install Maven on Ubuntu and getting it up and running smoothly.

In this tutorial, we’ll cover the essentials of Apache Maven, including:

  • What Maven is and why it’s important
  • The prerequisites for installing Maven on Ubuntu
  • A step-by-step guide to installing Maven
  • How to verify your installation
  • Troubleshooting common issues

By the end of this guide, you’ll be equipped with everything you need to set up Maven on your Ubuntu system and start working on your Java projects efficiently. Let’s dive in!

What is Apache Maven?

Before we jump into the installation process, let’s take a moment to understand what Maven is and why it’s so widely used in the software development world.

Maven is a build automation tool primarily used for Java projects. It simplifies the process of building, packaging, and deploying Java applications by using Project Object Model (POM) files. These files contain configuration details, including dependencies, project versions, and build goals.

Some key features of Maven include:

  • Dependency management: Automatically handles libraries and project dependencies.
  • Project structure: Encourages standardization, making it easier for teams to collaborate.
  • Build automation: Automates tasks like compiling code, running tests, and packaging the application.
  • Plugins: Extends functionality with various plugins for compiling, testing, and deployment.

Now that we have an idea of what Maven is, let’s get started with the installation process.

Prerequisites: What You Need to Install Maven

Before you begin installing Maven on Ubuntu, ensure your system meets the following prerequisites:

  • Java Development Kit (JDK): Maven requires Java to run, so you need to have a JDK installed. The recommended version is Java 8 or later.
  • Ubuntu System: This guide assumes you are using an Ubuntu system (Ubuntu 18.04, 20.04, or later).
  • Sudo Privileges: You’ll need administrative privileges to install software packages.

Let’s make sure you have Java installed before moving forward.

how to install maven on Ubuntu

Step 1: Install Java Development Kit (JDK)

To check if Java is already installed on your system, open a terminal and run:

java -version

If Java is installed, you’ll see output like this:

openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9)
OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)

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

  1. First, update your package list:
sudo apt update
  1. Install the default OpenJDK package:
sudo apt install openjdk-11-jdk
  1. Verify the installation by running:
java -version

Step 2: Install Maven on Ubuntu

Now that we have Java installed, let’s move on to installing Maven. There are multiple ways to install Maven on Ubuntu, including using the default APT repositories, downloading the latest version manually, or installing it via a package manager. Let’s cover each method.

Method 1: Install Maven Using APT (Recommended for Beginners)

The easiest and most straightforward way to install Maven on Ubuntu is by using the APT package manager. This method installs Maven from the official Ubuntu repositories.

  1. Open the terminal and update your system’s package list:
sudo apt update
  1. Install Maven by running the following command:
sudo apt install maven
  1. Once the installation is complete, verify Maven’s installation by checking its version:
mvn -version

You should see output like this:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: OpenJDK, runtime: /usr/lib/jvm/java-11-openjdk-amd64

This confirms that Maven has been installed correctly.

Method 2: Install Maven Manually (Latest Version)

If you want to install the latest version of Maven, you can manually download it from the Apache Maven website.

  1. First, visit the official Maven download page: https://maven.apache.org/download.cgi and find the latest version. Download the binary tar.gz file.
  2. Once you’ve downloaded the file, extract it in a directory of your choice. For example, you can extract it to /opt:
cd /opt
sudo wget https://downloads.apache.org/maven/maven-3/3.8.3/binaries/apache-maven-3.8.3-bin.tar.gz
sudo tar -xvzf apache-maven-3.8.3-bin.tar.gz
  1. After extracting the Maven files, you need to set up the MAVEN_HOME environment variable. Edit your ~/.bashrc file:
nano ~/.bashrc
  1. Add the following lines at the end of the file:
export MAVEN_HOME=/opt/apache-maven-3.8.3
export PATH=$PATH:$MAVEN_HOME/bin
  1. Save the file and source it to apply the changes:
source ~/.bashrc
  1. Verify the installation by running:
mvn -version

You should see the latest Maven version details.

Method 3: Install Maven Using Snap (Alternative Method)

Ubuntu also supports the Snap package manager, which simplifies the installation process. You can install Maven via Snap as follows:

  1. Install Maven using Snap:
sudo snap install maven --classic
  1. After the installation is complete, check the installed version:
mvn -version

Step 3: Verify Maven Installation

Regardless of the installation method you chose, it’s important to verify that Maven is working properly. To do so, follow these steps:

  1. Open a terminal and check the Maven version:
mvn -version

You should see output similar to this, indicating the version of Maven installed, the Java version, and other relevant details.

  1. Create a simple Maven project to verify that Maven is functioning properly:
mvn archetype:generate -DgroupId=com.example -DartifactId=demo-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command generates a basic Maven project using the quickstart archetype. After the project is created, you can navigate into the project directory and run the following:

cd demo-project
mvn package

This will compile the project and package it into a JAR file located in the target directory.

Troubleshooting Common Issues

While installing Maven on Ubuntu is typically straightforward, there may be a few issues that can arise. Here are some common problems and their solutions:

Issue 1: Command Not Found Error

If running mvn -version results in a “command not found” error, ensure that Maven is installed correctly and added to your system’s PATH. Double-check that you sourced your .bashrc file after editing it:

source ~/.bashrc

Issue 2: Incorrect Java Version

Maven requires a Java Runtime Environment (JRE) or JDK to function. If you encounter errors related to Java version, ensure that the correct JDK is installed and that the JAVA_HOME environment variable is properly set:

echo $JAVA_HOME

You can set JAVA_HOME by adding this line to your .bashrc:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

After editing .bashrc, remember to run:

source ~/.bashrc

Issue 3: Permission Denied Errors

If you encounter permission errors while installing Maven, try using sudo for commands that require elevated privileges. Ensure that you have proper permissions to install software and modify system files.

Conclusion

Apache Maven is an invaluable tool for Java developers, automating the build process and managing dependencies with ease. In this guide, we’ve covered multiple methods of install Maven on Ubuntu, including using APT, manually installing the latest version, and using Snap. We also walked through how to verify the installation and troubleshoot common issues.

Now that Maven is installed on your Ubuntu system, you can take advantage of its powerful features to streamline your Java development workflow. Whether you’re building small applications or large-scale enterprise systems, Maven will be a reliable companion.

Did you find this guide helpful? Let us know in the comments below if you encountered any issues or if you have further questions about Maven installation. Feel free to share this guide with others who might benefit from it. Happy coding, and stay tuned for more installation tutorials!

Read Also : How to Install GCC on Ubuntu?

Leave a Comment

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

Scroll to Top