Introduction
Apache Maven is an open source project management tool used primarily for developing Java applications. Maven incorporates a POM (Project Object Model) approach, which means that 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
- an account with sudo privileges
- the terminal window. Access to
- a text editor such as Nano.
.
. Access to
. Access to
Install Maven on
Ubuntu with
apt
The apt command provides a simple and straightforward way to install 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 result will look like this
: Install the latest version 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 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 appears in the Files section, and you can access previous versions using the file link in the Previous Versions section.
We are using version 3.8.4, which is the latest version at the time of writing.
2. Download the Maven installation file to the /tmp: wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz
-P /tmp
3 directory. When the download is complete, extract the installation file to the
/opt directory: sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
4. Create a symbolic link named maven that leads to the Maven installation directory
: sudo ln -s /opt/apache-maven-3.8.4 /opt/maven
Step 3: Configure 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 the changes to maven.sh.
4. Make the maven.sh file executable using the command chmod: sudo chmod
+x /etc/profile.d/maven.sh
5. Run the script file maven.sh with the source command to configure 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’re interested in trying Maven on a different Linux distribution, take a look at our guides on installing Maven on Debian 9 and installing Maven on CentOS 7. For Windows users, see our guide on installing Maven on Windows.