This article will show you how to install Maven on Ubuntu 16.04(Xenial Xerus). There are 3 approaches to install Maven on Ubuntu.
- Install with apt-get
- Install manually
- Install with SDKMAN
We will try both approaches in this tutorial.
1. Install Maven on Ubuntu 16.04 with Apt-get
We will use apt-get to install Maven on Ubuntu 16.04. This is the simplest way.
1.1. Open terminal and run below command
1 2 |
sudo apt-get update sudo apt-get install maven |
1.2. Verification
We can verify whether Maven is installed successfully or not by type command:
1 |
mvn -v |
The output will be similar to below:
1 2 3 4 5 6 |
Apache Maven 3.3.9 Maven home: /usr/share/maven Java version: 1.8.0_101, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-oracle/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.4.0-36-generic", arch: "amd64", family: "unix" |
Note that the output also show us the Maven’s home, which is: /usr/share/maven
2. Install Maven on Ubuntu 16.04 Manually
2.1. Download Maven
You can download Maven distribution from the Apache Maven website. Currently, the latest release of Maven is version 3.3.9.
In the following command, I will download the binary only version of Maven:
1 |
wget http://mirrors.viethosting.vn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz |
2.2. Unpack Maven
1 |
sudo tar -xf apache-maven-3.3.9-bin.tar.gz -C /usr/local |
The above command will unpack the Maven distribution to /usr/local/apache-maven-3.3.9
Next, we will create a symbolic link to the Maven distribution:
1 2 |
cd /usr/local sudo ln -s apache-maven-3.3.9 maven |
2.3. Create MAVEN_HOME Environment Variables
Create a maven.sh file at /etc/profile.d folder (you can use vi with below command)
1 |
sudo vi /etc/profile.d/maven.sh |
Enter the follow content to the file:
1 2 |
export M2_HOME=/usr/local/maven export PATH=${M2_HOME}/bin:${PATH} |
Save the file.
We will need to activate the above environment variables. We can do that by log out and log in again or simply run below command:
1 |
source /etc/profile |
2.4. Verification
We can verify whether Maven is installed successfully or not by type command:
1 |
mvn -v |
3. Install Maven on Ubuntu 16.04 With SDKMAN
SDKMAN is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems. We can leverage SDKMAN to install Maven on Ubuntu 16.04.
3.1. Install SDKMAN
3.1.1. Open terminal and enter
1 |
curl -s "https://get.sdkman.io" | bash |
Note that, SDKMAN may ask you to install unzip package as the following:
1 2 3 4 5 6 7 8 |
Looking for a previous installation of SDKMAN... Looking for unzip... Not found. ======================================================================================== Please install unzip on your system using your favourite package manager. Restart after installing unzip. ======================================================================================== |
In that case, you simply need to install unzip by issuing command:
1 |
sudo apt install unzip |
3.1.2. Enter the following code snippet on the terminal
1 |
source "$HOME/.sdkman/bin/sdkman-init.sh" |
3.1.3. Lastly, verify that installation succeeded
1 |
sdk version |
The output will be similar to:
1 |
SDKMAN 5.1.3+69 |
3.2. Install Maven By SDKMAN
3.2.1. List versions of Maven currently supported by SDMMAN
1 |
sdk list maven |
Here is the sample output on my PC:
1 2 3 4 5 |
============================================================== Available Maven Versions ============================================================== 3.3.9 3.3.3 |
3.2.2. Install Maven 3.3.9 on mine
1 |
sdk install maven 3.3.9 |
3.2.3.Verification
We can verify whether Maven is installed successfully or not by type command:
1 |
mvn -v |
Sample output on my terminal:
1 2 3 4 5 6 |
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00) Maven home: /home/ubuntu/.sdkman/candidates/maven/current Java version: 1.8.0_101, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-oracle/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.4.0-36-generic", arch: "amd64", family: "unix" |
Note that Maven’s home directory is: /home/ubuntu/.sdkman/candidates/maven/current
4. Summary
We have just tried to install Maven on Ubuntu 16.04 LTS (Xenial Xerus). There are 3 installation approaches for you to select. Both of them are simple. However, the first and third ones seems to be more simple and be easier to switch between different version of Maven. If you’re looking for Gradle installation tutorial, you can refer to my recent post: Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)