Introduction
Apache Maven is an open-source project management tool primarily used for developing Java applications. Maven incorporates a POM (Project Object Model) approach, which means it uses an XML file to store information about projects, configurations, and dependencies.
This tutorial explains how to install Maven on Ubuntu 20.04, both using the official repository and the installation file on the Maven website.
Prerequisites
- A system running Ubuntu 20.04.
- A working Internet connection.
- Access to an account with sudo privileges.
- Access to the terminal window.
- Access to a text editor such as Nano.
Note: Learn how to install Ubuntu 20.04.
Install Maven on Ubuntu with apt
The apt command provides a simple and straightforward way of installing Maven on Ubuntu. Follow these steps to complete the installation:
1. Update the system repository using:
sudo apt update
2. Install Maven from the official repository:
sudo apt install maven
3. When prompted, type Y and press Enter to confirm the installation.
3. Check the current version of Maven to verify the installation:
mvn -version
If successful, the output will look like this:
Install the Latest Release of Maven on Ubuntu
Manually installing Maven is more complex than using the apt
command but offers more flexibility when choosing which version to install.
Step 1: Install OpenJDK
1. Update the system repository with:
sudo apt update
2. Install the latest version of OpenJDK by using:
sudo apt install default-jdk
3. Type Y and press Enter when prompted to confirm the installation.
4. Verify the installation by checking the current version of OpenJDK:
java -version
Step 2: Download and Install Maven
1. Visit the Maven download page and select the version of Maven you want to install. The latest version is listed in the Files section, and you can access earlier versions by using the archive link in the Previous Releases section.
We are using version 3.8.4, which is the latest version at the time of writing this article.
2. Download the Maven install file in the /tmp directory:
wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz -P /tmp
Note: Learn more about using the Linux wget command.
3. Once the download is complete, extract the installation file in the /opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
4. Create a symbolic link called maven leading to the Maven installation directory:
sudo ln -s /opt/apache-maven-3.8.4 /opt/maven
Step 3: Set Up Environment Variables
1. Use the Nano text editor 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}
3. Press Ctrl + X, then type Y and press Enter to save changes to maven.sh.
4. Make the maven.sh file executable using the chmod
command:
sudo chmod +x /etc/profile.d/maven.sh
5. 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
Conclusion
After reading this tutorial, you should have a copy of Maven installed on your Ubuntu system and ready to use.
If you are interested in trying Maven on a different Linux distribution, have a look at our guides on installing Maven on Debian 9 and installing Maven on CentOS 7. For Windows users, refer to our guide on installing Maven on Windows.