Introduction
Apache Maven is a powerful project management tool that is based on POM (project object model). maven is a tool that can be used for building and managing any Java-based project. Maven contains XML files also referred to as pom.xml which include configuration details, project dependencies, and other data.
This article will show two different ways to install Apache Maven on Ubuntu 18.04 and other versions of Ubuntu OS.
I. Install Maven on Ubuntu
- Installation of Java: Updating the package index
$ sudo apt-get update
- Install the OpenJDK package:
$ sudo apt install default-jdk
- Verifying the java installation:
$ java -version
Output
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu218.04, mixed mode, sharing)
Apache Maven Download
- Download the stable version of Apache maven from the below command:
$ sudo wget https://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
- The file will be downloaded as a .tar.gz file.
$ sudo tar -xvf apache-maven-3.6.3-bin.tar.gz
- Move the
apache-maven
file by executing the below command:$
sudo mv apache-maven-3.6.3 /opt/
- To have more control over Maven versions and updates. we need to create the symbolic link with the Maven installation directory:
$ sudo ln -s /opt/apache-maven-3.6.3 /opt/maven
Setup the Environment for Maven
You need to configure a few environment variables, including JAVA_HOME, M3_HOME, and MAVEN_HOME.
- Create a new file named
maven.sh
inside the/etc/profile.d/
directory.$ sudo nano /etc/profile.d/maven.sh
- The configuration below needs to be added to this newly created file:
Save and close the file.export JAVA_HOME=/usr/lib/jvm/default-java export M2_HOME=/opt/maven export MAVEN_HOME=/opt/maven export PATH=${M2_HOME}/bin:${PATH}
- Provide the required privileges to the file using the command below:
$ sudo chmod +x /etc/profile.d/maven.sh
- Load the environment variables using the
source
command:$ source /etc/profile.d/maven.sh
Verify the maven installation
- Use the
mvn -version
command which will print the Maven version:$ mvn -version
Output
Apache Maven 3.6.3 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-358.el8.x86_64", arch: "amd64", family: "unix"
II. Install Apache Maven Using Apt
- Easy and simple way to install maven using apt. Update the package index by using the following command.
$ sudo apt-get update
- Install Maven.
$ sudo apt install maven
- Verify the installation by running the
mvn -version
command.$ mvn -version