Install Maven on Linux [Step by Step Configuration Guide]

Introduction

In this tutorial, we will learn about how to install Maven on Linux. Maven is an open-source powerful tool used for simplifying and streaming the build and dependency management process in software development, particularly for Java projects. Its conventions and standardization make it a popular choice in the Java ecosystem.

 

Install Maven on Linux [Step by Step Configuration Guide]

Maven Overview

Apache Maven is a widely used build automation and project management tool primarily used for Java projects, although it can be used for projects in other programming languages as well. It is part of the Apache Software foundation and is open-source. Maven helps developers in managing the build process, handling project dependencies and structuring projects in standarized way.

 

Install Maven on Linux [Step by Step Configuration Guide]

Prerequisite

  • CentOS 7 Operating System Installed
  • Basic understanding of Linux CLI commands

Also read: How to Install Apache Web Server on Linux: [6 Easy Steps]

 

Step-1: Install JDK

In this step, check if Java is pre installed in your machine using any of the commands given below. If you get the similar output as shown below, that means Java is not installed in your machine.

[root@linuxnasa~]# rpm -qa | grep java

[root@linuxnasa~]# java -version
-bash: /usr/bin/java: No such file or directory

[root@linuxnasa~]# which java
/usr/bin/which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin)

 

Next, install openJDK binary from  jdk.java.net using below command. At the time of writing this article, JDK21 is the latest version available for download.

[root@linuxnasa~]# wget https://download.java.net/java/GA/jdk21/fd2272bbf8e04c3dbaee13770090416c/35/GPL/openjdk-21_linux-x64_bin.tar.gz
--2023-09-26 14:52:09-- https://download.java.net/java/GA/jdk21/fd2272bbf8e04c3dbaee13770090416c/35/GPL/openjdk-21_linux-x64_bin.tar.gz
Resolving download.java.net (download.java.net)... 2.20.0.103
Connecting to download.java.net (download.java.net)|2.20.0.103|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 203348211 (194M) [application/x-gzip]
Saving to: ‘openjdk-21_linux-x64_bin.tar.gz’

100%[========================================================================================>] 203,348,211 84.2MB/s in 2.3s

2023-09-26 14:52:12 (84.2 MB/s) - ‘openjdk-21_linux-x64_bin.tar.gz’ saved [203348211/203348211]

 

Next, untar the Maven archive using below command. You will see a new folder created  in the current directory after extracting from the archive.

[root@linuxnasa~]# tar -xzf openjdk-21_linux-x64_bin.tar.gz

[root@linuxnasa~]# ll
drwxr-xr-x. 8 root root 4096 Sep 26 14:58 jdk-21
-rw-r--r--. 1 root root 203348211 Aug 11 22:08 openjdk-21_linux-x64_bin.tar.gz

 

I have moved the jdk-21 folder under /tmp path for my convenience using below command. You can place it anywhere. This path will be used in the next step to set the environment variables.

[root@linuxnasa~]# mv jdk-21 /tmp

 

Step-2: Set JAVA_HOME and PATH Variable

In this step, after installing the openJDK, set the java path in JAVA_HOME and PATH environment variables. To do this, open the .bash_profile file in the home directory path and add below lines in the file.

[root@linuxnasa~]# vi ~/.bash_profile

JAVA_HOME='/tmp/jdk-21'
PATH="$JAVA_HOME/bin:$PATH"
export PATH

 

Next, to apply the configuration changes, execute below command.

[root@linuxnasa~]# source ~/.bash_profile

 

Step-3: Verify Java Installation

In this step, verify if java is successfully installed in the machine using below command. If you get the similar output as shown below that means Java is installed in your machine.

[root@linuxnasa~]# java -version
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

 

NOTE:

Java can also be installed using yum package manager on RHEL/CentOS 7 machines. To install Maven using package manger, please follow the steps from here.

 

Step-4: Install Maven

In this step, after installing the Java successfully, we will install the Maven. To do so, install the  Maven from maven.apache.org using below command. I have installed Maven version 3.9.4.

[root@linuxnasa~]# wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
--2023-09-26 15:29:46-- https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9336368 (8.9M) [application/x-gzip]
Saving to: ‘apache-maven-3.9.4-bin.tar.gz’

100%[========================================================================================>] 9,336,368 --.-K/s in 0.05s

2023-09-26 15:29:46 (194 MB/s) - ‘apache-maven-3.9.4-bin.tar.gz’ saved [9336368/9336368]

 

Next, untar the Maven archive using below command. A new folder will be created in the current directory.

[root@linuxnasa~]# tar -xzf apache-maven-3.9.4-bin.tar.gz

[root@linuxnasa~]# ll
drwxr-xr-x. 6 root root 4096 Sep 26 15:30 apache-maven-3.9.4
-rw-r--r--. 1 root root 9336368 Aug 3 13:26 apache-maven-3.9.4-bin.tar.gz
drwxr-xr-x. 8 root root 4096 Sep 26 14:58 jdk-21

drwxr-xr-x. 8 root root 4096 Sep 26 14:58 openjdk-21_linux-x64_bin.tar.gz

 

Next, move the apache-maven-3.9.4 folder under /tmp (you can place it under any path) path using below command.

[root@linuxnasa~]# mv apache-maven-3.9.4 /tmp/

 

Step-5: Set M2_HOME and PATH Variable

In this step, we will set the M2_HOME and PATH environment variable to make  Maven binary  system wide accessible. We will add below lines in the .bash_profile file under home directory as shown below.

[root@linuxnasa~]# vi ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
JAVA_HOME='/tmp/jdk-21'
M2_HOME='/tmp/apache-maven-3.9.4'
PATH="$JAVA_HOME/bin:$PATH"
PATH="$M2_HOME/bin:$PATH"
export PATH

 

Next, execute below command to apply the configuration changes.

[root@linuxnasa~]# source ~/.bash_profile

 

Step-6: Verify Maven 

In this step, we will verify the if Maven is installed successfully by executing below command. If you get similar output that means Maven is now installed in your machine.

[root@linuxnasa~]# mvn -version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /tmp/dump/apache-maven-3.9.4
Java version: 21, vendor: Oracle Corporation, runtime: /tmp/dump/jdk-21
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1127.el7.x86_64", arch: "amd64", family: "unix"

 

Summary

Reference – Best Steps to Install Apache Maven on Ubuntu 20.04

 

 

Leave a Comment