---
title: "Surefire Way to Install Maven on Ubuntu (Step by Step)"
description: "Learn how to install Maven on Ubuntu and start simplifying and automating Java builds and code deployments in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/install-maven-on-ubuntu/"
---

# Surefire Way to Install Maven on Ubuntu (Step by Step)

> Learn how to install Maven on Ubuntu and start simplifying and automating Java builds and code deployments in this step-by-step tutorial!

Source: https://adamtheautomator.com/install-maven-on-ubuntu/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Surefire Way to Install Maven on Ubuntu (Step by Step)](https://adamtheautomator.com/wp-content/uploads/2022/04/Automate-your-Java-Builds-and-Install-Maven-on-Ubuntu.jpg)

# Surefire Way to Install Maven on Ubuntu (Step by Step)

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)11 April 20226 min. read

Categories: [DevOps](/category/devops/)

Tags:[Java](/tag/java/)[Ubuntu Linux](/tag/ubuntu-linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing Java on Ubuntu Machine](#installing-java-on-ubuntu-machine)
*   [Install Maven on Ubuntu Machine](#install-maven-on-ubuntu-machine)
*   [Integrating Jenkins with Maven Plugin](#integrating-jenkins-with-maven-plugin)
*   [Creating a Jenkins Job to Build a Java Project](#creating-a-jenkins-job-to-build-a-java-project)
*   [Conclusion](#conclusion)

If you’re a developer, you’d typically prefer to automate the tedious manual build deployment process. So why not install [Maven](https://maven.apache.org/what-is-maven.html) on Ubuntu?

Apache Maven allows you to simplify the deployment process and provide a quality uniform build system for any Java-based project. And in this tutorial, you’ll learn how to install Maven on Ubuntu machine to automate build and code deployment.

Read on and save yourself the stress of manual build deployments!

## Prerequisites

This post will be a step-by-step tutorial. To follow along, be sure you have the following:

*   An Ubuntu Linux machine (14.04.4 LTS or greater) – This tutorial uses [Ubuntu 20.04.4 LTS](https://lists.ubuntu.com/archives/ubuntu-announce/2022-February/000277.html).

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   Jenkins instance installed.

Related:[How to Install Jenkins CI/CD in Ubuntu Linux](https://adamtheautomator.com/install-jenkins/)

*   A [GitHub account](https://github.com/join).

## Installing Java on Ubuntu Machine

Before automating Java builds, kick-off this tutorial by installing Java required for Apache Maven. Apache Maven uses the concept of a [project object model (POM)](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html) and is used mainly for building Java projects. POM is an XML file that contains information about a project and configuration details used by Maven to build that project.

You can use multiple Java implementations to work with Apache Maven, but [OpenJDK](https://openjdk.org/) is the most popular, which you will install and use in this tutorial.

1\. Log in to your Ubuntu machine using your favorite SSH client.

2\. Next, run the following commands to create a directory named _~/install\_apache\_maven\_demo_ and switch to that directory.

```bash
# Create the ~/install_apache_maven_demo directory
mkdir ~/install_apache_maven_demo
# Change the working directory
cd ~/install_apache_maven_demo
```

3\. Run the [`apt install`](https://manpages.ubuntu.com/manpages/xenial/man8/apt.8.html) command below to `install` Java (`default-jdk`) on your machine. `\`

```bash
sudo apt install default-jdk
```

![Installing Java](https://adamtheautomator.com/wp-content/uploads/2022/04/image-78.png)

Installing Java

4\. Finally, run the below command to check the version of installed Java on your machine. Doing so ensures that you’ve successfully installed Java.

```bash
java -version
```

Below, you can see the Java version installed is 11.0.13, but yours may be different.

![Checking the Version of Java to Verify Java Installation](https://adamtheautomator.com/wp-content/uploads/2022/04/image-79.png)

Checking the Version of Java to Verify Java Installation

## Install Maven on Ubuntu Machine

Now that you have Java installed on the Ubuntu machine, it’s time to install Maven on Ubuntu. Installing Apache Maven is pretty similar to installing other packages on your machine. But for this tutorial, you’ll run the `wget` command to download the Apache Maven package.

1\. Run the [`apt update`](https://linux.die.net/man/8/apt-get) command below to update your system packages.

```bash
sudo apt update
```

2\. Next, run the `wget` command below to download the latest Apache Maven package in the _~/install\_apache\_maven\_demo_ directory. The latest version of Apache Maven is 3.8.4 as of this writing.

```bash
wget https://downloads.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz
```

![Downloading the Latest Apache Maven Package](https://adamtheautomator.com/wp-content/uploads/2022/04/image-80.png)

Downloading the Latest Apache Maven Package

Related:[How to Download Files with Python Wget](https://adamtheautomator.com/python-wget/)

3\. Run each command below to extract (`tar xf`) the Apache Maven archive (`apache-maven-3.8.4-bin.tar.gz`) and verify the extracted files in the _~/install\_apache\_maven\_demo_ directory.

```bash
# Extract the Apache Maven archive to the working directory.
tar xf apache-maven-3.8.4-bin.tar.gz
# List contents of the working directory.
ls
```

![Extracting the Apache Maven Archive](https://adamtheautomator.com/wp-content/uploads/2022/04/image-81.png)

Extracting the Apache Maven Archive

Related:[How to Manage Zip Files in Linux](https://adamtheautomator.com/unzip-files-in-linux/)

4\. Now, create a file named _maven.sh_ in the _/etc/profile.d/_ directory with your preferred text editor and copy/paste the below lines to the _maven.sh_ file.

In the code below, the [export](https://www.tutorialspoint.com/unix_commands/export.htm) commands set the Java and Apache Maven home directory by defining the path they are installed.

```bash
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=~/install_apache_maven_demo/apache-maven-3.8.4
export MAVEN_HOME=~/install_apache_maven_demo/apache-maven-3.8.4
export PATH=${M2_HOME}/bin:${PATH}
```

5\. Run the below [`source`](https://linuxize.com/post/bash-source-command/) command for the declared variables in step four to affect the environment.

```bash
source /etc/profile.d/maven.sh
```

6\. Finally, run the following command to check the `version`.

```bash
mvn -version
```

As you can see below, the installed Apache Maven version is 3.8.4, but yours may be different.

![Verifying the Apache Maven Version Installed](https://adamtheautomator.com/wp-content/uploads/2022/04/image-82.png)

Verifying the Apache Maven Version Installed

## Integrating Jenkins with Maven Plugin

You’ve completely installed Java and Apache Maven on your machine, and that’s a good start. But to automate your Java builds, you’ll need an automation tool, like [Jenkins](https://www.jenkins.io/). You’ll configure Jenkins to integrate with Apache Maven to run your builds.

Related:[How to Create a Jenkins CI CD Pipeline](https://adamtheautomator.com/jenkins-ci-cd/)

1\. Open your favorite web browser, and navigate to Jenkins URL followed by port 8080 (i.e., _jenkins-ip:8080)._

Click on the Manage Jenkins menu at the left panel to see Jenkins system configuration options (step two).

![Opening the Jenkins URL on the Web Browser](https://adamtheautomator.com/wp-content/uploads/2022/04/image-83.png)

Opening the Jenkins URL on the Web Browser

2\. Next, click on **Manage Plugins**, as highlighted below, to access the **Plugin Manager** page, where you’ll install the [Maven](https://plugins.jenkins.io/maven-plugin/) [](https://plugins.jenkins.io/maven-plugin/)[Integration](https://plugins.jenkins.io/maven-plugin/) plugin (step three). Maven Integration plugin allows you to configure, build, and run Maven-based projects in Jenkins.

![Manage Plugins, ](https://adamtheautomator.com/wp-content/uploads/2022/04/image-84.png)

**Manage Plugins**

3\. Lastly, install the Maven Integration plugin with the following:

*   On the **Plugin Manager** page, click on the **Available** tab.
    
*   Search for **Maven** on the search box. After a few seconds, [Maven Integration](https://plugins.jenkins.io/maven-plugin/) will appear in the list of available plugins.
    
*   Check the box in front of the **Maven Integration** plugin to select the plugin.
    
*   Click on the **Install without restart button** at the bottom of the page to install the selected plugin.
    

![Installing Maven Integration Plugin on the Jenkins](https://adamtheautomator.com/wp-content/uploads/2022/04/image-85.png)

Installing Maven Integration Plugin on the Jenkins

## Creating a Jenkins Job to Build a Java Project

You’ve successfully integrated the Apache Maven plugin with Jenkins, but nothing much is happening unless you configure a Jenkins job with code and build the Java code. So, you’ll create a simple Jenkins job that fetches Java code from Git and build the code using Maven.

1\. Navigate back to the Jenkins dashboard, and click on the **New Item** menu to create a new Jenkins job.

![Creating a new Jenkins Job](https://adamtheautomator.com/wp-content/uploads/2022/04/image-86.png)

Creating a new Jenkins Job

2\. Next, set a job name in the **Enter an item name** field, as shown below, and select **Maven project** as Jenkins job type. For this tutorial, the job name is set as **Java-program**.

Click on OK at the bottom of the page to create the Jenkins job.

![Setting Job name and Job Type](https://adamtheautomator.com/wp-content/uploads/2022/04/image-87.png)

Setting Job name and Job Type

3\. Click on the **Source Code Management** tab and Git as source code manager. Copy/paste the below URL to the Repository URL field. The Git repository contains a java code that runs a “hello world” program.

```bash
https://github.com/jenkins-docs/simple-java-maven-app
```

![Adding the Git Repository URL in the Source Code Management](https://adamtheautomator.com/wp-content/uploads/2022/04/image-88.png)

Adding the Git Repository URL in the Source Code Management

4\. Now, configure the post steps with the following:

*   Click on the **Post Steps** tab, and choose the **Run only if the build succeeds** option.
    
*   Click on **Add post-build step** dropdown button, and select **Execute shell**.
    
*   Copy/paste the below two lines of commands to the **Command** field, then click on **Save** at the bottom of the page to save the settings of the Jenkins job.
    

```bash
# Switch to the directory where Jenkins will 
# compile, build and store the Java code
cd /var/lib/jenkins/workspace/Java-program/target
# Executes the built code
java -jar my-app-1.0-SNAPSHOT.jar
```

![Configuring Jenkins Job Post Steps](https://adamtheautomator.com/wp-content/uploads/2022/04/image-89.png)

Configuring Jenkins Job Post Steps

5\. Navigate back to the **Manage Jenkins** page, and this time, click on **Global Tool Configuration**, as shown below.

![Accessing the Global Tool Configuration Page](https://adamtheautomator.com/wp-content/uploads/2022/04/image-90.png)

Accessing the Global Tool Configuration Page

6\. Run the commands below to see the values of the MAVEN\_HOME and JAVA\_HOME environment variables.

```bash
echo $MAVEN_HOME
echo $JAVA_HOME
```

Note down the values of the MAVEN\_HOME and JAVA\_HOME variables as you’ll need them in the next step.

![Finding the values of MAVEN\_HOME and JAVA\_HOME Environment Variables](https://adamtheautomator.com/wp-content/uploads/2022/04/image-91.png)

Finding the values of MAVEN\_HOME and JAVA\_HOME Environment Variables

7\. Now, set the **Name** and values of **JAVA\_HOME** and **MAVEN\_HOME** under **JDK** and **Maven** sections, as shown below. Doing so ensures that Jenkins can run the Java code successfully and use Apache Maven.

After setting the values, click on the Save button at the bottom of the page to save the settings.

![Setting the JAVA\_HOME Parameter](https://adamtheautomator.com/wp-content/uploads/2022/04/image-92.png)

Setting the JAVA\_HOME Parameter

![Setting the MAVEN\_HOME Parameter](https://adamtheautomator.com/wp-content/uploads/2022/04/image-93.png)

Setting the MAVEN\_HOME Parameter

8\. Return to the Jenkins dashboard, and you’ll see your newly created Jenkins Job (**Java-program**). Click on the build (play) button to build your code. As soon as you click on the build button, Jenkins starts, compiles, and executes the Java program using Apache Maven.

Click on the Java-program Jenkins job to view the build’s status.

![Return to the Jenkins dashboard](https://adamtheautomator.com/wp-content/uploads/2022/04/image-94.png)

Return to the Jenkins dashboard

Below, you can see a build number under the Build History section in the left panel, denoting you ran the Jenkins job once. The green check indicates the build has been successfully finished without any errors.

Click on the successful build (#1), and you will see an option for console output.

![Viewing Build History](https://adamtheautomator.com/wp-content/uploads/2022/04/image-95.png)

Viewing Build History

9\. Finally, click on the **Console Output** menu to view the logs of the Jenkins job that ran successfully.

As you can see below, the Job has run and executed the Java program successfully, printing the Hello World! text in the output.

![Verifying the Output of the Java program in Jenkins job Console Output Logs](https://adamtheautomator.com/wp-content/uploads/2022/04/image-96.png)

Verifying the Output of the Java program in Jenkins job Console Output Logs

## Conclusion

Throughout this tutorial, you’ve learned how to install Maven on Ubuntu. You’ve also installed the Maven Integration tool and set up the Global Tool Configuration on Jenkins to automate Java builds.

Now, what do you plan to deploy using Apache Maven with Jenkins in the future? Perhaps automate web deployments? Or [create a Jenkins CI CD pipeline](https://adamtheautomator.com/jenkins-ci-cd/) to improve your Java projects’ software development process?

Related:[Related: Create a Jenkins Build Job to Automate Web Deployments](https://adamtheautomator.com/jenkins-build-job/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-maven-on-ubuntu%2F&text=Surefire%20Way%20to%20Install%20Maven%20on%20Ubuntu%20\(Step%20by%20Step\))[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Finstall-maven-on-ubuntu%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-maven-on-ubuntu%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/07/install-tensorflow-anaconda.jpg)

### [How to Install Tensorflow with Anaconda on Ubuntu](/install-tensorflow-with-anaconda/)

Get started with machine-learning platforms and install Tensorflow with Anaconda on Ubuntu Linux in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/05/Install-Elasticsearch-on-Ubuntu-for-Next-Level-Searching-1.jpg)

### [Install Elasticsearch on Ubuntu for Next Level Searching](/install-elasticsearch-on-ubuntu/)

Learn how to install Elasticsearch on Ubuntu and take your search ability to the next level with this flexible and fast search engine!

![](https://adamtheautomator.com/wp-content/uploads/2022/03/Speed-and-Simplicity-with-the-Cinnamon-Desktop-Environment.jpg)

### [Speed and Simplicity with the Cinnamon Desktop Environment](/cinnamon-desktop-environment/)

Learn how to install, configure, and remotely connect to the Cinnamon desktop environment for quick files and application management in this step-by-step tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
