How To Install Apache Maven On Ubuntu

In this tutorial, we will show you how to install Maven on Ubuntu using apt. In Java projects, Maven is primarily used to automate build processes. Java libraries and Maven plug-ins are dynamically downloaded from one or more repositories such as the Maven 2 Central Repository and cached locally. Artifacts created by local projects can also be added to this local cache of downloaded artifacts. Pubic repositories may also be updated.

Prerequisites

Step 1 — Update your package index

In this step, we are going to update our current package index.

Run the following command to update your current package index:


sudo apt update

You'll see the following output:


Hit:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://ppa.launchpad.net/ethereum/ethereum/ubuntu focal InRelease        
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease                         
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]        
Hit:7 https://packages.microsoft.com/repos/ms-teams stable InRelease           
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-updates/main Sources [462 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/restricted Sources [32.9 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,580 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [604 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [304 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [280 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [810 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [115 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [667 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [904 kB]
Get:20 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [391 kB]
Get:21 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:22 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,980 B]
Get:23 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [23.7 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.7 kB]
Get:25 http://archive.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [66.2 kB]
Get:26 http://archive.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464 B]
99% [18 Packages store 0 B]                                         733 kB/s 0s

Step 2 — Install Apache Maven

In this step, we are going to install the package onto our system using apt.

Run the following command to install apache maven:


sudo apt install maven

You'll see output similar to the following output:


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libllvm11 libllvm11:i386 shim
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libaopalliance-java libatinject-jsr330-api-java libcdi-api-java
  libcommons-cli-java libgeronimo-annotation-1.3-spec-java
  libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java
  libhawtjni-runtime-java libjansi-java libjansi-native-java libjsr305-java
  libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java
  libmaven3-core-java libplexus-cipher-java libplexus-classworlds-java
  libplexus-component-annotations-java libplexus-interpolation-java
  libplexus-sec-dispatcher-java libplexus-utils2-java libsisu-inject-java
  libsisu-plexus-java libslf4j-java libwagon-file-java
  libwagon-http-shaded-java libwagon-provider-api-java
Suggested packages:
  libaopalliance-java-doc libatinject-jsr330-api-java-doc libservlet3.1-java
  libasm-java libcglib-java libjsr305-java-doc libmaven-shared-utils-java-doc
  liblogback-java libplexus-cipher-java-doc libplexus-classworlds-java-doc
  libplexus-sec-dispatcher-java-doc libplexus-utils2-java-doc testng
The following NEW packages will be installed:
  libaopalliance-java libatinject-jsr330-api-java libcdi-api-java
  libcommons-cli-java libgeronimo-annotation-1.3-spec-java
  libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java
  libhawtjni-runtime-java libjansi-java libjansi-native-java libjsr305-java
  libmaven-parent-java libmaven-resolver-java libmaven-shared-utils-java
  libmaven3-core-java libplexus-cipher-java libplexus-classworlds-java
  libplexus-component-annotations-java libplexus-interpolation-java
  libplexus-sec-dispatcher-java libplexus-utils2-java libsisu-inject-java
  libsisu-plexus-java libslf4j-java libwagon-file-java
  libwagon-http-shaded-java libwagon-provider-api-java maven
0 upgraded, 29 newly installed, 0 to remove and 59 not upgraded.
Need to get 8,537 kB of archives.
After this operation, 11.2 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Type Y in your terminal input to download the package and all required dependencies

Step 3 - Configure Environment Variables

In this step, we are going to configure environment variables. The applications we run often use the M2_HOME environment variable to determine the maven installation path.

Modify the contents of the following file: $HOME/.profile using your favorite text editor for this example we are using vi:


vi $HOME/.profile

Once the text file opens up in the terminal window, add the following lines to the end of the file:


M2_HOME="/usr/share/maven"

MAVEN_HOME="/usr/share/maven"

export PATH=${M2_HOME}/bin:${PATH}

Save the file contents and quit the text editor.

Load the environment variables using the source command:


source $HOME/.profile

Verify that the environment variable is set by running the following command:


echo $M2_HOME

You'll see output similar to the following output:


/usr/share/maven

Step 4 — Verify Maven Installation

In this step, we are going to verify that our installation was successful.

Run the following command to verify that the package was successfully installed:


mvn -version

You'll see output similar to the following output:


Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-99-generic", arch: "amd64", family: "unix"

Conclusion

In this article, you should have installed the maven package, set the environment variables required for use by other applications, and verified your installation.

Comments