Apache Maven is a free open source project management software that can be used to manage the build, reporting and documentation of a project from one central location. It is based on the conception of a project object model and is used in particular for the deployment of Java-based projects. Apache Maven facilitates the daily work of Java developers and generally helps to understand a Java-based project. You can easily integrate your project with Subversion or Git. Maven can also be used to create and manage projects written in C#, Ruby, Scala, and other languages.

In this tutorial, we will explain how to install Apache Maven on Ubuntu 18.04 server.

Requirements

  • A server running Ubuntu 18.04.
  • A root password is set up on your server.

Getting Started

The commands in this tutorial must be run with root privileges. To become the root user, run this command:

sudo -s

and enter your sudo password when requested.

Before starting, you will need to update your system with the latest version. You can do this by running the following command:

apt-get update -y
apt-get upgrade -y

Once your system is updated, restart the system to apply the changes.

Install Java JDK

Maven requires Java JDK to be installed to your system. By default, the Java Development Kit (JDK) is available in the Ubuntu 18.04 default repository. You can install it by running the following command:

apt-get install default-jdk -y

Once the Java is installed, you can check the version of Java with the following command:

java -version

You should see the following output:

openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3, mixed mode)

Install Apache Maven

First, you will need to download the latest version of Apache Maven from their official website. You can download it with the following command:

cd /tmp
wget https://www-us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf apache-maven-3.6.0-bin.tar.gz

Next, copy the extracted directory to the /opt/ directory with the following command:

cp -r apache-maven-3.6.0 /opt/maven

Next, you will need to set up the environment variables for Java and Maven. You can do this by creating maven.sh file:

nano /etc/profile.d/maven.sh

Add the following lines:

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Next, give proper permission to the maven.sh file with the following command:

chmod 755 /etc/profile.d/maven.sh

Finally, load the environment variables by running the following command:

source /etc/profile.d/maven.sh

You can now check the Maven installation by running the following command:

mvn -version

You should see the following output:

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /opt/maven
Java version: 10.0.2, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-20-generic", arch: "amd64", family: "unix"

Congratulations! you have successfully installed Apache Maven on Ubuntu 18.04 LTS server.

How to install Apache Maven on Ubuntu 18.04 LTS