How to install Apache Maven on Ubuntu

How to install Apache Maven on Ubuntu

Apache Maven is free and open source Project Management tool which can be used for Java projects. It provides a framework to automate the project’ s build infrastructure. This tutorial explains the installation of Apache Maven on Ubuntu.

Installation procedure

Before installing Apache Maven, update your system repositories by running the following command.

root@linuxhelp1:~# apt-get update
Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease
Reading package lists... Done

The Apache Maven needs the Java Development Kit (JDK) to be installed first . Now use the below command to add the repository to install Java 8

root@linuxhelp1:~# add-apt-repository ppa:webupd8team/java
 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7 / JDK8 / JDK9). There are no actual Java files in this PPA.
More info (and Ubuntu installation instructions):
- for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html
Debian installation instructions:
- Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
Oracle Java 9 (for both Ubuntu and Debian): http://www.webupd8.org/2015/02/install-oracle-java-9-in-ubuntu-linux.html
For JDK9, the PPA uses standard builds from: https://jdk9.java.net/download/ (and not the Jigsaw builds!).
Important!!! For now, you should continue to use Java 8 because Oracle Java 9 is available as an early access release! You should only use Oracle Java 9 if you explicitly need it, because it may contain bugs and it might not include the latest security patches! Also, some Java options were removed in JDK9, so you may encounter issues with various Java apps. More information and installation instructions (Ubuntu / Linux Mint / Debian): http://www.webupd8.org/2015/02/install-oracle-java-9-in-ubuntu-linux.html
 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmplii8t4x4/secring.gpg'  created
gpg: keyring `/tmp/tmplii8t4x4/pubring.gpg'  created
gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmplii8t4x4/trustdb.gpg: trustdb created
gpg: key EEA14886: public key " Launchpad VLC"  imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

Once the new repository is added use the apt-get update command to update the recently made changes to the system.

root@linuxhelp1:~# apt-get update
Get:1 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease [17.6 kB]
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease                    
Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease                                        
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease     
Hit:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease   
Get:6 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages [2,864 B]
Get:7 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main i386 Packages [2,864 B]
Get:8 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main Translation-en [1,260 B]
Fetched 24.6 kB in 1s (24.0 kB/s)
Reading package lists... Done

Now install Oracle Java installer version 8 and press y to continue.

root@linuxhelp1:~# apt-get install oracle-java8-installer -y
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following additional packages will be installed:
  gsfonts-x11 java-common oracle-java8-set-default
Suggested packages:
  binfmt-support visualvm ttf-baekmuk | ttf-unfonts | ttf-unfonts-core ttf-kochi-gothic | ttf-sazanami-gothic ttf-kochi-mincho | ttf-sazanami-mincho ttf-arphic-uming
The following NEW packages will be installed:
  gsfonts-x11 java-common oracle-java8-installer oracle-java8-set-default
0 upgraded, 4 newly installed, 0 to remove and 469 not upgraded.
Need to get 54.6 kB of archives.
After this operation, 272 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 oracle-java8-installer all 8u121-1~webupd8~2 [32.9 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 java-common all 0.56ubuntu2 [7,742 B]
.
.
.
Selecting previously unselected package oracle-java8-set-default.
(Reading database ... 176757 files and directories currently installed.)
Preparing to unpack .../oracle-java8-set-default_8u121-1~webupd8~2_all.deb ...
Unpacking oracle-java8-set-default (8u121-1~webupd8~2) ...
Selecting previously unselected package gsfonts-x11.
Preparing to unpack .../gsfonts-x11_0.24_all.deb ...
Unpacking gsfonts-x11 (0.24) ...
Processing triggers for fontconfig (2.11.94-0ubuntu1) ...
Setting up oracle-java8-set-default (8u121-1~webupd8~2) ...
Setting up gsfonts-x11 (0.24) ...

After installing Java, verify it’ s version by running the following command.

root@linuxhelp1:~# java -version
java version " 1.8.0_121" 
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Now to install the apache maven, move to /opt/ directory for the installation and from there download package for Apache Maven.

root@linuxhelp1:~# cd /opt/
root@linuxhelp1:/opt# wget http://www-eu.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
--2017-04-07 15:00:24--  http://www-eu.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
Resolving www-eu.apache.org (www-eu.apache.org)... 88.198.26.2, 2a01:4f8:130:2192::2
Connecting to www-eu.apache.org (www-eu.apache.org)|88.198.26.2|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8491533 (8.1M) [application/x-gzip]
Saving to: ‘ apache-maven-3.3.9-bin.tar.gz’ 
apache-maven-3.3.9-bin.tar.gz                 100%[===============================================================================================> ]   8.10M  70.7KB/s    in 73s    
2017-04-07 15:01:38 (113 KB/s) - ‘ apache-maven-3.3.9-bin.tar.gz’  saved [8491533/8491533]

Now extract the Maven package using tar command.

root@linuxhelp1:/opt# tar -xvzf apache-maven-3.3.9-bin.tar.gz
apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar
apache-maven-3.3.9/lib/maven-embedder-3.3.9.jar
apache-maven-3.3.9/lib/maven-settings-3.3.9.jar
apache-maven-3.3.9/lib/plexus-utils-3.0.22.jar
apache-maven-3.3.9/lib/maven-core-3.3.9.jar
apache-maven-3.3.9/lib/maven-model-3.3.9.jar
apache-maven-3.3.9/lib/commons-lang3-3.4.jar
apache-maven-3.3.9/lib/maven-settings-builder-3.3.9.jar
apache-maven-3.3.9/lib/maven-builder-support-3.3.9.jar
apache-maven-3.3.9/lib/plexus-interpolation-1.21.jar
apache-maven-3.3.9/lib/plexus-component-annotations-1.6.jar
.
.
.
apache-maven-3.3.9/bin/mvnDebug
apache-maven-3.3.9/bin/mvnyjp
apache-maven-3.3.9/conf/
apache-maven-3.3.9/conf/logging/
apache-maven-3.3.9/conf/logging/simplelogger.properties
apache-maven-3.3.9/conf/settings.xml
apache-maven-3.3.9/conf/toolchains.xml
apache-maven-3.3.9/lib/ext/
apache-maven-3.3.9/lib/ext/README.txt

The package has been extracted succesfully. Now rename the newly extracted directory.

root@linuxhelp1:/opt# mv apache-maven-3.3.9 maven

Next we need to setup Environment Variables for Apache Maven. In order to do that create new script file under /etc/profile.d/ directory.

root@linuxhelp1:/opt# vim /etc/profile.d/mavenenv.sh

Create the following entries in mavenenv.sh file. Save and exit from the file.

export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

After creating environment variables, we need to load the environment variables by executing the following command.

root@linuxhelp1:/opt# source /etc/profile.d/mavenenv.sh

Now verify the version of apache maven.

root@linuxhelp1:/opt# mvn -V
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5  2015-11-10T22:11:47+05:30)
Maven home: /opt/maven
Java version: 1.8.0_121, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: " linux" , version: " 4.4.0-21-generic" , arch: " amd64" , family: " unix" 

Wasn' t that an easy installation process? Apache Maven is based on the project object model (POM).

Comment
parthiban
Aug 27 2018
Thanks for your Feedback and keep in touch for further tutorials
ayushgarg
Jul 03 2018
AWESOME ARTICLE. Thank you so much.
Add a comment
FAQ
Q
What is Apache Maven?
A
Apache Maven is free and open source Project Management tool which can be used for Java projects.
Q
Where you have downloaded latest link of Apache Maven?
A
To download the latest releases of Apache Maven see this link "http://www-eu.apache.org/dist/maven/"
Q
What for you have created /etc/profile.d/mavenenv.sh?
A
To setup Environment Variables for Apache Maven.
Q
What is the essential purpose of using this ?
A
Maven is a build automation tool used primarily for Java projects
Q
I can't seem to get JUnit to show up in the classpath. What gives in Apache Maven?
A
Must have some unit tests for your project, otherwise JUnit is ignored.