Introduction
Apache Maven is an open-source project management tool primarily used to develop Java applications. It incorporates a POM (Project Object Model) approach, which stores information about projects, configurations, and dependencies in an XML file.
This tutorial explains how to install Maven on Ubuntu using the official repository or the installation file on the Maven website.
Prerequisites
- A system running Ubuntu.
- Command-line access to the system.
- A user with administrative privileges.
Install Maven on Ubuntu with APT
The apt package manager provides a straightforward way to install Maven on Ubuntu. However, the user is limited to the Maven version currently available in the repository.
Follow these steps to install Maven with APT:
1. Update the local package repository index:
sudo apt update
2. Install Maven from the official Ubuntu repository:
sudo apt install maven -y
3. Check the current Maven version to verify the installation:
mvn -version
If successful, the output shows the program version.
Download and Install Maven's Recent Version on Ubuntu
Installing Maven by downloading the installation file from the official website is more complex than using the apt
command, but it enables users to install the latest available version. Follow the steps below to install Maven manually.
Step 1: Install OpenJDK
OpenJDK is an open-source Java implementation that is a Maven dependency on Ubuntu. Proceed with the following steps to install OpenJDK on the system:
1. Update the system's package repository index:
sudo apt update
2. Install the latest OpenJDK version by running:
sudo apt install default-jdk -y
3. Verify the installation by checking the current OpenJDK version:
java -version
Step 2: Download and Install Maven
The latest Maven version is listed in the Files section of the official website. Earlier versions are available in the Previous Releases section. Follow the procedure below to download and install Maven on an Ubuntu system:
1. Visit the Maven download page.
2. Right-click the version of Maven you want to install and copy the link.
3. Download the Maven installation file to the /tmp directory using the wget command. The syntax is:
wget [link] -P /tmp
Replace [link]
with the link to the Maven version you copied in the previous step.
4. Once the download is complete, extract the installation file to the /opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
5. Create a symbolic link leading to the Maven installation directory:
sudo ln -s /opt/apache-maven-[version] /opt/maven
Replace [version]
with the Maven version you downloaded. For example:
sudo ln -s /opt/apache-maven-3.9.6 /opt/maven
Step 3: Set Up Environment Variables
1. Use a text editor like Nano to create and open the maven.sh script file in the /etc/profile.d/ directory:
sudo nano /etc/profile.d/maven.sh
2. Add the following lines to the maven.sh file:
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save the file and exit.
3. Use the chmod command to make the maven.sh file executable:
sudo chmod +x /etc/profile.d/maven.sh
4. Execute the maven.sh script file with the source command to set up the new environment variables:
source /etc/profile.d/maven.sh
Step 4: Verify Maven Installation
Check the current version of Maven to verify the installation:
mvn -version
The example output below shows the 3.9.6 version of Maven installed on the system.
Conclusion
After following this tutorial, you should have a copy of Maven installed and ready to use on your Ubuntu system. The article explained the steps for installing Maven via the official Ubuntu APT repository and the official website.
If you want to try Maven in a different environment, check out our guides on installing Maven on Debian 9 and installing Maven on Windows.