How to Install Apache Maven on Ubuntu 16.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 16.04. We will also learn how to install Java 8 from the PPA Repository.

Prerequisites

  • Ubuntu 16.04
  • Root privileges

What we will do

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

Step 1 – Install Java on Ubuntu 16.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 ‘python-software-properties’ package using the apt command below.

sudo apt install -y python-software-properties

Now add the Java PPA repository and update all repositories.

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

Ubuntu repositories have been updated.

Apache Maven requires the JDK 1.7 or above. And for this guide, we will install the JDK 1.8.

Install java from PPA using the apt command below.

sudo apt install -y oracle-java8-installer

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

Accept Oracle Binary License

Accept the Oracle License by choosing the ‘YES’ button.

License ok

And when the installation is complete, check the java version using the command below.

java -version

You will see a result similar to the following.

Check java version

Java 8 installed on the Ubuntu 16.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 ‘/usr/local/src’ directory as the Maven home directory.

Go to the ‘/usr/local/src’ directory and download the Apache Maven Binary code.

cd /usr/local/src
wget http://www-us.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

Extract the maven.tar.gz file, then delete the compressed file.

tar -xf apache-maven-3.5.2-bin.tar.gz
rm -f apache-maven-3.5.2-bin.tar.gz

And you will get new directory apache-maven-version – rename the directory.

mv apache-maven-3.5.2/ apache-maven/

Apache Maven is now downloaded in the ‘/usr/local/src/apache-maven’ directory.

Unpack 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 the 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 there.

# 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=/usr/local/src/apache-maven
export MAVEN_HOME=/usr/local/src/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.

Setup Apache Maven

Step 4 – Testing Maven

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

mvn –version

And you should get the result as below.

Test Apache Maven setup

Apache Maven 3.2 installation has been completed. It’s running under Linux 64-bit, with Java 1.8 installed, and the Maven home directory is ‘/usr/local/src/apache-maven’.

Reference

Share this page:

نوشته های مشابه