Apache Maven is a very useful open-source project management tool based on POM (project object model). Primarily, it is used to manage Java projects and manage a project’s build, reporting, and documentation. This tutorial show you how to install and configure Apache Maven on Ubuntu 20.04 LTS.
Prerequisites
Make sure you are logged in as root or user with sudo privileges.
Installing Apache Maven on Ubuntu 20.04 with apt
The official Ubuntu repositories contain Maven packages that can be installed with the apt
package manager.
Maven packages included in the official Ubuntu repositories but the version may be older than the latest version of Maven. It is very a simple and straightforward process to install Maven on Ubuntu.
At first, update the package index:
sudo apt update
Run the below command install Maven:
sudo apt install maven
Verify the installation by typing:
mvn -version
It will show you something like this:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-26-generic", arch: "amd64", family: "unix"
It’s done. Maven is installed on your system, and ready to start using it.
Installing the Latest Release of Apache Maven
Perform the following steps to download and install the latest version of Apache Maven on Ubuntu 20.04.
1. Install OpenJDK
JDK 1.7 or above need to be installed for Maven 3.3+ version. To install OpenJDK 11, type:
sudo apt update
sudo apt install default-jdk
Verify the installation by running the following command:
java -version
It should show output like below:
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)
2. Downloading Apache Maven
Visit the Maven download page to see the latest version of Maven. Currently, at the time of writing this article, the latest version of Apache Maven is 3.6.3
.
We will download the Apache Maven in the /tmp
directory:
wget https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp
After complete the download, extract the archive in the /opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
To control the Maven versions and updates, create a symbolic link for maven that will point to the Maven installation directory:
sudo ln -s /opt/apache-maven-3.6.3 /opt/maven
To upgrade your Maven installation, unpack the newer version and change the symlink and point to it.
3. Setup environment variables
Now we will set up environment variables, to do so open your text editor and create a new file named mavenenv.sh in the /etc/profile.d/
directory.
sudo nano /etc/profile.d/maven.sh
Put the following following code lines in to it:
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 and close the file.
We should make this script executable using chmod command:
sudo chmod +x /etc/profile.d/maven.sh
At last, load the environment variables using the source command:
source /etc/profile.d/maven.sh
4. Verify the installation
To verify the installation, use the mvn -version
command to check the Maven version:
mvn -version
It should print like the following:
Maven home: /opt/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-26-generic", arch: "amd64", family: "unix"
It’s done. The latest version of Maven is now installed on your Ubuntu system.
Conclusion
You have learned how to install Apache Maven on Ubuntu 20.04. To learn more about Apache Maven visit the official Apache Maven Documentation page.
If you have any question or feedback, don’t hesitate to leave a comment below.