In this blog we will go through step by step Installation of the Latest Release of Apache Maven
Apache Maven is a free and open-source project management and comprehension tool used primarily for Java projects. Maven uses a Project Object Model (POM), which is essentially an XML file containing information about the project, configuration details, the project’s dependencies, and so on.
In this blog, we will show you how to install Apache Maven on Ubuntu 18.04. The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Linux Mint, Kubuntu, and Elementary OS.
The official Ubuntu repositories contain Maven packages that can be installed with the apt package manager. This is the easiest way to install Maven on Ubuntu. However the version included in the repositories may lag behind the latest version of Maven.
To install the latest version of Maven follow the instructions provided in the second part of this article where we will be downloading Maven from their official website.
Choose the installation method that is most appropriate for your environment.
Prerequisites
In order to be able to install packages on your Ubuntu system, you must be logged in as a user with sudo privileges.
Installation of the Latest Release of Apache Maven
The following sections provide a step by step instructions about how to install the latest Apache Maven version on Ubuntu 18.04. We’ll be downloading the latest release of Apache Maven from their official website.
1. Install OpenJDK
Maven 3.3+ requires JDK 1.7 or above to be installed. We’ll install OpenJDK, which is the default Java development and runtime in Ubuntu 18.04.
The installation of Java is simple. Start by updating the package index first
sudo apt update
Install the OpenJDK package by below command
sudo apt install default-jdk
Verify the installation by running the following command
java -version
The output should look something like this:
openjdk version “14.0.1” 2020-05-15
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.2)
OpenJDK 64-Bit Server VM (build 14.0.1+13-Ubuntu-1ubuntu0.18.04.2, mixed mode)
2. Download Apache Maven
At the time of writing this article, the latest version of Apache Maven is 3.6.0. Before continuing with the next step,
Start by downloading the Apache Maven in the /tmp directory using the following wget command:
wget https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp
Once the download is completed, extract the archive in the /opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
To have more control over Maven versions and updates, we will create a symbolic link maven that will point to the Maven installation directory:
sudo ln -s /opt/apache-maven-3.6.0 /opt/maven
Later if you want to upgrade your Maven installation you can simply unpack the newer version and change the symlink to point to the latest version.
3. Setup environment variables
Next, we’ll need to set up the environment variables. To do so, open your text editor and create a new file named mavenenv.sh inside of the /etc/profile.d/ directory.
sudo nano /etc/profile.d/maven.sh
Paste the following configuration:
/etc/profile.d/maven.sh
exportJAVA_HOME=/usr/lib/jvm/default-java
exportM2_HOME=/opt/maven
exportMAVEN_HOME=/opt/maven
exportPATH=${M2_HOME}/bin:${PATH}
Save and close the file. This script will be sourced at shell startup.
Make the script executable with chmod:
sudochmod +x /etc/profile.d/maven.sh
Finally load the environment variables using the source command:
source /etc/profile.d/maven.sh
4. Verify the installation
To validate that Maven is installed properly use the mvn -version command which will print the Maven version:
mvn -version
You should see something like the following:
Apache Maven 3.6.0 (89c234eddfc891bffe498cbd87ef8bc89ff76bg; 2020-05-18T22:30:35Z)
Maven home: /opt/maven
Java version: 10.0.2, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: ISO-8859-1
OS name: "linux", version: "4.15.0-36-generic", arch: "amd64", family: "unix"
That’s it. The latest version of Maven is now installed on your Ubuntu system