For some reason I can’t possibly fathom Linux seems to have a downer on all things Java. Packages are available for most things but they are invariably not the latest version which means performing a manual install – not the end of the world but irritating when you can just install a package for most things. This page gives a quick overview of how to install Maven 3 on Ubuntu 11.10 but the same (or a very similar) process will work on most distributions.
Download and Install
Since I’m installing the software manually I like to keep it away from the other software on the system and install it under my /data folder. I use this folder for anything I install on the system and, as the name suggests, data. Firstly download the latest version of Maven 3 from the Maven site http://maven.apache.org/ and unpack it in /data, for example.
cd /data wget http://apache.mirror.rbftpnetworks.com//maven/binaries/apache-maven-3.0.3-bin.tar.gz tar -xzvf apache-maven-3.0.3-bin.tar.gz
This will leave you with a folder called apache-maven-3.0.3 under the data directory. I now create a link to this called apache-maven as that makes updating easier when new point realeases come out.
ln -s apache-maven-3.0.3/ apache-maven
Setting up the Environment
Before you can use maven at a command prompt you need to tell your system where to find it and provide some other environment variables. There are several places where you can include environment variables (more info: https://help.ubuntu.com/community/EnvironmentVariables) but the correct one in this case is /etc/environment.
Open the environment file
sudo emacs /etc/environment
If you haven’t made any changes since you installed Ubuntu it will probably look something like this.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
Just one line defining your system path. As an aside, if you leave emacs and enter echo $PATH at a command prompt you’ll see the path set above. Now modify your environment file such that it looks like the one below. E.g. add maven to the path variable and add the java and maven environment variables. If you do happen to have more in your environment file don’t touch or remove the other lines as it’s likely that other applications are relying on them.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/data/apache-maven/bin" JAVA_HOME=/usr/lib/jvm/java-6-openjdk M2_HOME=/data/apache-maven MAVEN_HOME=/data/apache-maven M2=/data/apache-maven/bin
Testing
It is necessary to log out and log back in for the new environment variables to take effect because your shell takes a cached copy when you log in. Once you have logged back in execute the command
mvn -version
You should see output something like this
Apache Maven 3.0.3 (r1075438; 2011-02-28 17:31:09+0000) Maven home: /data/apache-maven Java version: 1.6.0_22, vendor: Sun Microsystems Inc. Java home: /usr/lib/jvm/java-6-openjdk/jre Default locale: en_GB, platform encoding: UTF-8 OS name: "linux", version: "2.6.38-11-generic", arch: "amd64", family: "unix"
References
- This page covers the process for an earlier version of Ubuntu: http://lukieb.wordpress.com/2011/02/15/installing-maven-3-on-ubuntu-10-04-lts-server/
- Links to binary packages: http://www.discursive.com/blog/4636
- PPA Binary Packages: https://launchpad.net/~natecarlson/+archive/maven3