There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install Apache Maven on Ubuntu 18.04 LTS

Apache Maven is a software project management tool based on the POM (Project, Object, Model) concept. Maven is a build automation tool used primarily for Java-based projects. It can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.

In this tutorial, I will show you step-by-step how to install and configure Apache Maven on Ubuntu 18.04. We will also learn how to install Java 8 from the PPA Repository.

Prerequisites

  • Ubuntu 18.04 LTS
  • Root privileges

What we will do?

  1. Install Java on Ubuntu 18.04
  2. Download Apache Maven
  3. Configure Apache Maven Environment
  4. Testing

Step 1 - Install Java on Ubuntu 18.04

In this tutorial, we will be using the Java packages from the PPA repository, so we need to add the Java PPA Repository to the system.

Before adding a new repository, install the 'software-properties-common' package using the apt command below.

sudo apt install software-properties-common apt-transport-https -y

Now add the 'webupd8team' PPA repository to the server.

sudo add-apt-repository ppa:webupd8team/java -y

Install Java on Ubuntu 18.04

Note:

  • On Ubuntu 18.04, the 'add-apt-repository' command will automatically update the repository.
  • Apache Maven requires the JDK 1.7 or above. And for this guide, we will install the JDK 1.8.

The 'webupd8team' repository has been added to the server, install the Java 8 installer from the PPA repository using the apt command below.

sudo apt install oracle-java8-installer -y

During the installation, you will be prompted about the Oracle Binary License. Choose 'OK'.

oracle-java8-installer

Accept the Oracle License by choosing the 'YES' button.

Accept the Oracle License

When the installation is complete, check the java version.

java -version

You will see a result similar to the following.

Check Java version

Java has been installed on the Ubuntu 18.04 server.

Step 2 - Download Apache Maven

In this step, we will download the apache maven binary code using the wget command. And we will be using the '/opt/apache-maven' directory as the Maven home directory.

Go to the '/opt' directory and download the Apache Maven Binary code.

cd /opt/
wget http://www-eu.apache.org/dist/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz

Extract the maven.tar.gz file, then rename the maven directory to 'apache-maven'.

tar -xf apache-maven-3.5.3-bin.tar.gz
mv apache-maven-3.5.3/ apache-maven/

Now you get the 'apache-maven' directory that contains apache maven binary files and other.

Download Apache Maven

Step 3 - Configure Apache Maven Environment

In this step, we will configure the environment for Apache Maven. We will define some environment variables that are needed by Apache Maven, including 'JAVA_HOME', M2_HOME, and the PATH environment for the maven binary files.

Go to the '/etc/profile.d' directory and create a new configuration file 'maven.sh'.

cd /etc/profile.d/
vim maven.sh

Paste the following configuration below.

# Apache Maven Environment Variables
# MAVEN_HOME for Maven 1 - M2_HOME for Maven 2
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export M2_HOME=/opt/apache-maven
export MAVEN_HOME=/opt/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Save the changes and exit.

Now make the 'maven.sh' script executable and then apply the configuration by running the 'source' command.

chmod +x maven.sh
source maven.sh

The Apache Maven environment setup has been completed.

Configure Apache Maven Environment

Step 4 - Testing Maven

To verify the maven installation, we can run the following maven command.

mvn --version
mvn --help

And you should get the result as shown below.

Testing Maven

Apache Maven 3.5 installation has been completed. It's running under Linux 64-bit, with Java 1.8 installed, and the Maven home directory is '/opt/apache-maven'.

Reference

Share this page:

2 Comment(s)