Linux

How to Install Apache Maven on Ubuntu 20.04

How to Install Apache Maven on Ubuntu 20.04

Apache Maven is an open source project management and understanding tool that is mainly used for Java projects. Maven uses the Project Object Model (POM), which is basically an XML file that contains information about the project, configuration details, project dependencies, and more.

In this tutorial, we will show you two different ways to install Apache Maven on Ubuntu 20.04.

The official Ubuntu repository contains the Maven package which can be installed with the apt package manager. This is the easiest way to install Maven on Ubuntu. However, the version included in the repository 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 which shows how to download the binary distribution archive from their official website.

Choose the installation method that is most suitable for your settings and environment.

Precondition

The instructions assume that you are logged in as root or as a user with sudo privileges.

Install Apache Maven on Ubuntu 20.04 with apt

Installing Maven on Ubuntu using apt is a simple and easy process.

Update the package index and install Maven by entering the following command:
sudo apt update
sudo apt install maven

To verify installation, run mvn-version:

mvn -version

The output will look like this:

Output :

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"

There she is. Maven is now installed on your system, and you can start using it.

Install the Latest Release from Apache Maven

In this section, we will provide step-by-step instructions on how to download and install the latest version of Apache Maven on Ubuntu 20.04.

1. Install OpenJDK

Maven 3.3+ requires JDK 1.7 or newer to install.

Install OpenJDK 11, by typing:
sudo apt update
sudo apt install default-jdk

Verify the installation by running the following command:

java -version

The output will look like this:

Output :

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. Download Apache Maven

At the time of writing this article, the latest version of Apache Maven is 3.6.3. Before proceeding with the next step, visit the Maven download page to see if a newer version is available.

Download 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 the download is complete, extract the archive in the / opt directory:

sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt

To have more control over the versions and updates of Maven, we will create a symbolic link that will point to the Maven installation directory:

sudo ln -s /opt/apache-maven-3.6.3 /opt/maven

When a new version is released, you can update your Maven installation, by uninstalling the newer version and changing the symlink to point to it.

3. Set the environment variable

Next, we need to set the environment variable. To do this, open your text editor and create a new file called mavenenv.sh in the /etc/profile.d/ directory.

sudo nano /etc/profile.d/maven.sh

Paste the following code:

/etc/profile.d/maven.sh
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. This script will be sourced from the startup shell.

Make the script executable with chmod:

sudo chmod +x /etc/profile.d/maven.sh

Finally, load the environment variable using the source command:

source /etc/profile.d/maven.sh

4. Verify the installation

To verify that Maven is installed, use the mvn-version command which will print a version of Maven:

mvn -version

You should see something like the following:

Output :

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"

There she is. The latest version of Maven is now installed on your Ubuntu system.

Conclusion

We have shown you how to install Apache Maven on Ubuntu 20.04. You should now visit the official Apache Maven Documentation page and learn how to get started with Maven.

If you experience problems or get feedback, leave a comment below.

Related posts

How to delete a Terminal on Ubuntu and other Linux distributions

Linux

How to create files in Python and more

Linux

How to Install MariaDB on Ubuntu 20.04

Linux

How to Install the ElkArte Forum Software on Ubuntu 20.04

Linux

How to Use Sudo Commands on Linux

Linux

How to Install and Use PHP Composer on Ubuntu 20.04

Linux

How to Install Tomcat 9 on Ubuntu 20.04

Linux

How to Upgrade From Windows 7 to Linux

Howto

How to Set Up an FTP Server on Ubuntu

Linux