How to Install Maven in Ubuntu 16.04 LTS (Xenial)

Maven aims to remove the complexity and confusion which normally arises when building or managing a java-based project.  Nowadays, it’s a major part of most Java developers day-to-day workflow.

Here I’ll run you through how to install Maven on Ubuntu 16.04 (Xenial), the process start to finish should take less than 5 minutes.

Let’s get straight to it.

Apply Apt Sense

It’s a good idea to always update your repositories before embarking on an installation, and good practice to check which version of Maven we’ll be installing, run the following:

# freshen up your repositories
[email protected]:~$ sudo apt-get update
# confirm which version is provided by the repo
[email protected]buntu-xenial:~$ sudo apt-cache show maven
Package: maven
Priority: optional
Section: universe/java
Installed-Size: 119
Maintainer: Ubuntu Developers [email protected]
Original-Maintainer: Debian Java Maintainers [email protected]
Architecture: all
Version: 3.3.9-3
...
# trimmed output

Here we can see we’ll be installing Maven version 3.3.9-3 from the Ubuntu Developers repository, this looks good at time of writing so we’ll proceed with the installation.

Maven and Friends

As this is a fresh system, apt is going to install Maven as well as a whole bunch of dependencies, including Java.  As a result, in this guide I’ll be running you through how to configure our $JAVA_HOME variable afterwards, though if you’re installing Maven onto a machine you’ve already been developing on then chances are you already have Java installed.

Main point, don’t get copy-pasta happy, apply common sense.

[email protected]:~$ sudo apt-get install maven
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  ant ant-optional ca-certificates-java default-jre-headless fontconfig-config fonts-dejavu-core java-common junit junit4 libaopalliance-java libapache-pom-java libasm4-java libatinject-jsr330-api-java libavahi-client3 libavahi-common-data libavahi-common3 libbsh-java
  libcdi-api-java libcglib3-java libclassworlds-java libcommons-cli-java libcommons-codec-java libcommons-httpclient-java libcommons-io-java libcommons-lang-java libcommons-lang3-java libcommons-logging-java libcommons-net-java libcommons-net2-java
  libcommons-parent-java libcups2 libdom4j-java libdoxia-core-java libeasymock-java libeclipse-aether-java libfontconfig1 libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java libhamcrest-java libhttpclient-java libhttpcore-java libjaxen-java
  libjaxp1.3-java libjdom1-java libjetty-java libjpeg-turbo8 libjpeg8 libjsch-java libjsoup-java libjsr305-java liblcms2-2 liblog4j1.2-java libmaven-parent-java libmaven2-core-java libmaven3-core-java libnspr4 libnss3 libnss3-nssdb libobjenesis-java libpcsclite1
  libplexus-ant-factory-java libplexus-archiver-java libplexus-bsh-factory-java libplexus-cipher-java libplexus-classworlds-java libplexus-classworlds2-java libplexus-cli-java libplexus-component-annotations-java libplexus-component-metadata-java
  libplexus-container-default-java libplexus-container-default1.5-java libplexus-containers-java libplexus-containers1.5-java libplexus-interactivity-api-java libplexus-interpolation-java libplexus-io-java libplexus-sec-dispatcher-java libplexus-utils-java
  libplexus-utils2-java libqdox2-java libservlet2.5-java libservlet3.1-java libsisu-inject-java libsisu-plexus-java libslf4j-java libwagon-java libwagon2-java libxalan2-java libxbean-java libxerces2-java libxi6 libxml-commons-external-java
  libxml-commons-resolver1.1-java libxom-java libxpp2-java libxpp3-java libxrender1 libxtst6 openjdk-8-jre-headless x11-common
Suggested packages:
  ant-doc ant-gcj default-jdk | java-compiler | java-sdk ant-optional-gcj antlr javacc jython libbcel-java libbsf-java libgnumail-java libjdepend-java liboro-java libregexp-java default-jre junit-doc libaopalliance-java-doc libatinject-jsr330-api-java-doc
  libclassworlds-java-doc libcommons-httpclient-java-doc libcommons-io-java-doc libcommons-lang-java-doc libcommons-lang3-java-doc libavalon-framework-java libcommons-logging-java-doc libexcalibur-logkit-java libcommons-net-java-doc libcommons-net2-java-doc cups-common
  libdom4j-java-doc libeasymock-java-doc libcglib-java libjaxp1.3-java-gcj libjdom1-java-doc jetty libjetty-java-doc libjsoup-java-doc libjsr305-java-doc liblcms2-utils liblog4j1.2-java-doc libobjenesis-java-doc pcscd libplexus-cipher-java-doc
  libplexus-classworlds-java-doc libplexus-classworlds2-java-doc libplexus-cli-java-doc libplexus-container-default-java-doc libplexus-interactivity-api-java-doc libplexus-interpolation-java-doc libplexus-sec-dispatcher-java-doc libplexus-utils-java-doc
  libplexus-utils2-java-doc libqdox2-java-doc testng libwagon-java-doc libxalan2-java-doc libxsltc-java groovy2 libequinox-osgi-java libosgi-compendium-java libosgi-core-java libqdox-java libspring-beans-java libspring-context-java libspring-core-java
  libspring-web-java libxerces2-java-doc libxerces2-java-gcj libxml-commons-resolver1.1-java-doc libxom-java-doc openjdk-8-jre-jamvm libnss-mdns fonts-dejavu-extra fonts-ipafont-gothic fonts-ipafont-mincho ttf-wqy-microhei | ttf-wqy-zenhei fonts-indic
The following NEW packages will be installed:
  ant ant-optional ca-certificates-java default-jre-headless fontconfig-config fonts-dejavu-core java-common junit junit4 libaopalliance-java libapache-pom-java libasm4-java libatinject-jsr330-api-java libavahi-client3 libavahi-common-data libavahi-common3 libbsh-java
  libcdi-api-java libcglib3-java libclassworlds-java libcommons-cli-java libcommons-codec-java libcommons-httpclient-java libcommons-io-java libcommons-lang-java libcommons-lang3-java libcommons-logging-java libcommons-net-java libcommons-net2-java
  libcommons-parent-java libcups2 libdom4j-java libdoxia-core-java libeasymock-java libeclipse-aether-java libfontconfig1 libgeronimo-interceptor-3.0-spec-java libguava-java libguice-java libhamcrest-java libhttpclient-java libhttpcore-java libjaxen-java
  libjaxp1.3-java libjdom1-java libjetty-java libjpeg-turbo8 libjpeg8 libjsch-java libjsoup-java libjsr305-java liblcms2-2 liblog4j1.2-java libmaven-parent-java libmaven2-core-java libmaven3-core-java libnspr4 libnss3 libnss3-nssdb libobjenesis-java libpcsclite1
  libplexus-ant-factory-java libplexus-archiver-java libplexus-bsh-factory-java libplexus-cipher-java libplexus-classworlds-java libplexus-classworlds2-java libplexus-cli-java libplexus-component-annotations-java libplexus-component-metadata-java
  libplexus-container-default-java libplexus-container-default1.5-java libplexus-containers-java libplexus-containers1.5-java libplexus-interactivity-api-java libplexus-interpolation-java libplexus-io-java libplexus-sec-dispatcher-java libplexus-utils-java
  libplexus-utils2-java libqdox2-java libservlet2.5-java libservlet3.1-java libsisu-inject-java libsisu-plexus-java libslf4j-java libwagon-java libwagon2-java libxalan2-java libxbean-java libxerces2-java libxi6 libxml-commons-external-java
  libxml-commons-resolver1.1-java libxom-java libxpp2-java libxpp3-java libxrender1 libxtst6 maven openjdk-8-jre-headless x11-common
0 upgraded, 102 newly installed, 0 to remove and 0 not upgraded.
Need to get 58.0 MB of archives.
After this operation, 150 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

...
...
...
Adding debian:VeriSign_Universal_Root_Certification_Authority.pem
Adding debian:Verisign_Class_1_Public_Primary_Certification_Authority_-_G2.pem
Adding debian:Certinomis_-_Root_CA.pem
Adding debian:Verisign_Class_2_Public_Primary_Certification_Authority_-_G3.pem
Adding debian:TC_TrustCenter_Class_3_CA_II.pem
Adding debian:Equifax_Secure_CA.pem
Adding debian:XRamp_Global_CA_Root.pem
Adding debian:TeliaSonera_Root_CA_v1.pem
Adding debian:Certinomis_-_Autorité_Racine.pem
Adding debian:QuoVadis_Root_CA_2.pem
Adding debian:Go_Daddy_Root_Certificate_Authority_-_G2.pem
Adding debian:Verisign_Class_2_Public_Primary_Certification_Authority_-_G2.pem
Adding debian:SecureSign_RootCA11.pem
done.
Setting up default-jre-headless (2:1.8-56ubuntu2) ...
Setting up ant (1.9.6-1ubuntu1) ...
Setting up ant-optional (1.9.6-1ubuntu1) ...
Setting up junit4 (4.12-4ubuntu1) ...
Setting up libeasymock-java (3.3.1+ds-3) ...
Setting up libwagon2-java (2.10-3) ...
Setting up libmaven3-core-java (3.3.9-3) ...
Setting up maven (3.3.9-3) ...
update-alternatives: using /usr/share/maven/bin/mvn to provide /usr/bin/mvn (mvn) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu4) ...
Processing triggers for systemd (229-4ubuntu12) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ca-certificates (20160104ubuntu1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...

done.
done.
[email protected]:~$

Maven should now be installed, verify this by running “mvn -v”:

[email protected]:~$ mvn -v
Warning: JAVA_HOME environment variable is not set.
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-47-generic", arch: "amd64", family: "unix"

Janky Java

Now to get rid of that warning, we need to set Java’s home variable, do that like so:

[email protected]:~$ readlink -f $(which java)
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

JAVA_HOME should be set to then root of “jre”, open “/etc/environment” and define it:

[email protected]:~$ sudo vim /etc/environment
# insert the line:
# JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/"

Now logout of your SSH session and log back in (source /etc/environments doesn’t always work too well) and run the following to confirm Maven is happy:

[email protected]:~$ mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-47-generic", arch: "amd64", family: "unix"
[email protected]:~$

That’s it, Maven is now installed and ready for use.

If you have any problems, be sure to let me know in the comments box below and I’ll get round to giving you a hand as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.