Apache Maven on Ubuntu Steps to install it

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform software installations tasks.

In this context we shall look into the steps to install Apache Maven on Ubuntu.

How to Install Apache Maven on Ubuntu?

Apache Maven can be installed either using apt or from the official website. Let us look at each of these methods in detail.

1. Install Apache Maven via Apt

The default Ubuntu package repository includes a package for Maven, and we can install it easily with the apt command. Prior to the package install, it is a good idea to update the existing packages in the system.

root@ubuntu:~# apt update -y

After the update, we can install Maven with the command below:

root@ubuntu:~# apt install maven -y

Now, to verify the installation, run the following command to check the version of Maven on the system:

root@ubuntu:~# mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-91-generic", arch: "amd64", family: "unix"

2. Install Apache Maven via Download

Often the version of Maven within the Ubuntu repository is not the latest one. To obtain the most up-to-date version of Maven, we can install it after downloading the installation file from the Apache Maven website.

This method includes the following tasks:

i. Install OpenJDK.

ii. Download Maven.

iii. Extract Maven and Symlink.

iv. Configure Environment Variables.

v. Verify Maven Installation.

To proceed with this method, we need to install OpenJDK as Maven 3.3+ requires JDK 1.7 or above.

To install java, initially, we need to update the packages as we did earlier. Once the packages are up to date, install the OpenJDK package by typing:

root@ubuntu:~# apt install default-jdk

Now, verify the installation by running the following command:

root@ubuntu:~# java -version

How to Download Maven ?

The second task in this method is to download Maven into the /tmp directory utilizing the following command:

wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp

Next, extract the newly downloaded Maven archive to the /opt directory:

root@ubuntu:~# tar xf /tmp/apache-maven-3.6.3-bin.tar.gz -C /opt

To make it more straightforward to upgrade in the future, create a symlink to the Maven installation directory:

root@ubuntu:~# ln -s /opt/apache-maven-3.6.3 /opt/maven

How to Configure Environment Variables for Maven ?

To configure environment variables, initially, we need to create a maven.sh file inside the /etc/profile.d/ directory:

root@ubuntu:~# vim /etc/profile.d/maven.sh

Now, copy the following configuration out and paste it into the maven.sh 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}

Then, make the script executable with chmod:

root@ubuntu:~# chmod +x /etc/profile.d/maven.sh

Finally, to get these variables into play in the environment, run the source command on the file:

root@ubuntu:~# source /etc/profile.d/maven.sh

How to Verify Maven Installation ?

Finally to verify that Maven is installed properly, run the version command and check the output:

root@ubuntu:~# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-91-generic", arch: "amd64", family: "unix"

[Need urgent assistance in installing Apache Maven on Ubuntu? – We're available 24*7. ]


This article will guide you on how to #install #Apache #Maven on Ubuntu. Basically, Apache Maven is a popular build management tool for #Java projects. 

To test The Maven Installation in #Ubuntu:

Run the command mvn -version to check if the maven is installed properly. 

You will see the below description on the command prompt. Maven is installed successfully.

Some Maven #Commands:

1. mvn clean. This command cleans the maven project by deleting the target directory. 

2. mvn compiler:compile. This command compiles the java source classes of the maven project. 

3. mvn compiler:testCompile. 

4. mvn package. 

5. mvn install. 

6. mvn deploy. 

7. mvn validate. 

8. mvn dependency:tree.

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com

Related Posts

Focus on your business, not your servers.

Click Here to Learn More

Recent Post