sorenpoulsen.com header

Install Apache Maven on Ubuntu

Apache Maven is available from Ubuntu's package repository, but it's trailing behind the latest and greatest, so let's download and install Maven directly from https://maven.apache.org/download.cgi.

Grab the latest binary tar.gz. A the time of writing this is apache-maven-3.3.9-bin.tar.gz.

I going to assume that Java is already installed and the JAVA_HOME environment variable is set up. Otherwise it's all described here.

First extract the Tar file:

bob@home:~$ cd Downloads
bob@home:~$ umask 002
bob@home:Downloads$ tar xvzf apache-maven-3.3.9-bin.tar.gz

Next move the installation folder to a location shared by all users on the system and set ownership to root. Only root can modify the installation, other user can read and execute Maven binaries (because we set umask to 002) and user data is stored in their home folders. In particular the user's local Maven repository will be under ~/.m2/repository and the user's settings will be stored in ~/.m2/settings.xml.

bob@home:/Downloads$ sudo mv apache-maven-3.3.9 /usr/local
bob@home:/Downloads$ cd /usr/local
bob@home:/usr/local$ sudo chown -R root:root apache-maven-3.3.9/

Add a symbolic link /usr/local/maven:

bob@home:/usr/local$ sudo ln -s /usr/local/apache-maven-3.3.9 /usr/local/maven

Add Maven's binaries folder /usr/local/maven/bin to PATH.

bob@home:/usr/local$ sudo nano /etc/environment

Add "/usr/local/maven/bin" at the end of PATH. The contents of the file should look something like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/maven/bin"
JAVA_HOME=/usr/local/java

Save the file ctl+o and close it ctl+x.

Now reload the environment variables. The output of the env command should list maven at the end of the PATH environment variable:

bob@home:/usr/local$ source /etc/environment
bob@home:/usr/local$ env | grep ^PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/maven/bin

Let's spin up Maven.

bob@home:/usr/local$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: /usr/local/maven
Java version: 1.8.0_74, vendor: Oracle Corporation
Java home: /usr/local/jdk1.8.0_74/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.2.0-16-generic", arch: "amd64", family: "unix"

Maven is ready for use.

{{model.usr.name}}
{{cmt.user.name}}
{{cmt.user.name}}
{{childcmt.user.name}}
{{childcmt.user.name}}