How to Install Maven on Ubuntu

Apache Maven is a standardization framework that works as a build automation tool, dependency manager, and project management tool for Java-based projects. Furthermore, it creates a uniform build system that makes it easy to navigate to multiple projects. This guide will discuss its installation on Ubuntu.

3 Ways To Install Apache Maven on Ubuntu

Maven comes with several features, and the most prominent one is that it’s extensible, by creating custom plugins using scripting languages. This guide will cover three ways to install it on Ubuntu.

1: Through the Ubuntu Default Package Manager

The first method to install Maven-Apache on Ubuntu is via its default package manager; however, before that, installing OpenJDK is necessary, as Maven runs on Java. So, to install OpenJDK, use the command below:

sudo apt install openjdk-17-jre -y

Installing OpenJDK on Ubuntu for installing Maven

Here, I have installed OpenJDK 17; however, its available version can be checked using the following command:

apt search openjdk

Searching for available version of OpenJDK in apt package manager in Ubuntu

After installing OpenJDK, install Apache Maven by executing the command below:

sudo apt install maven -y

Installing maven on Ubuntu via apt Package manager

To verify the Maven installation via apt execute the version command:

maven --version

verifying the installation of maven on Ubuntu by checking its version

2: Through Maven Tar File

The second method to install Maven is to use its binary file, which is precompiled and an official release provided by Apache Maven. To download the binary file for Maven, visit its official download page:

Downloading Maven binary file on Ubuntu via its download page

Alternatively, use the download link for the file and download it via terminal by using the command below:

wget https://dlcdn.apache.org/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.tar.gz

Downloading Maven binary file on Ubuntu via its download link

Next, extract the tar file via the tar utility:

sudo tar xzf apache-maven-3.9.11-bin.tar.gz

Extracting maven binary tar file on Ubuntu

Now, move the extracted file to the operation software directory (opt), which is primarily used to manually download and install software on Linux:

sudo mv apache-maven-3.9.11 /opt/maven

Moving the Maven extracted directory to opt directory in Ubuntu

Next, create a bash file to add the path for the Maven and Java home environment variables:

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

Creating a script file for declaring java variable path for Maven On Ubuntu

Use the lines of code below for the path declaration, and after that, save the file:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export M3_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M3_HOME}/bin:${PATH}

 

adding the path for the Maven and Java home environment variables in Ubuntu

Next, make this script file executable using +x and chmod command:

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

making the maven script file executable on Ubuntu

Now apply the changes instantly without logging out and back in, in the current shell using the source command:

source /etc/profile.d/maven.sh

Applying the changes in the Maven script file in current shell on Ubuntu

To give Maven multiple access points, or in simpler terms, to make it accessible from anywhere in the system, create a symbolic link by executing:

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

Creating a symbolic link for Maven centralized access on Ubuntu

Now verify its installation by checking its version:

mvn -version

verifying the installation of maven on Ubuntu by checking its version

Here, I have installed version 3.9.11, but Maven has already released its 4.x version, which is still in development. To download the 4.0.0-tc-4 version of Maven, visit its download page:

Downloading Maven latest version tar file on Ubuntu

The process for its installation is the same:

mvn -version

verifying the installation of latest version maven on Ubuntu by checking its version

Note: It is important to note here that once the version command is executed, there might be an error of the JAVA_HOME environment variable is not defined correctly, as in the image below:

JAVA_HOME environment variable is not defined correctly error on Ubuntu.

To find the correct path for OpenJDK installation on Linux/Ubuntu, use the below command:

readlink -f $(which java) | sed "s:/bin/java::"

Searching for correct path for OpenJDK installation on Linux/Ubuntu

Paste the path of the JAVA_HOME environment variable in the bash file for Maven and save it:

Fixing JAVA_HOME environment variable is not defined correctly error on Ubuntu.

3: Through Maven Source File

The third and final method to install Maven on Ubuntu is via its source file, but this is only recommended for developers who want to modify or change its code. To download the source file, visit the Apache Maven download page:

Downloading source code file for Maven on Ubuntu via Maven download page

Or use the below command to download it via terminal:

wget https://dlcdn.apache.org/maven/maven-3/3.9.11/source/apache-maven-3.9.11-src.tar.gz

Downloading source code file for Maven on Ubuntu via terminal

Next, extract the Maven source file via the tar utility:

sudo tar xzf apache-maven-3.9.11-src.tar.gz

Extracting Maven source code file on Ubuntu

Now, navigate to the Maven extracted directory and create a directory named ‘target’. Afterwards, assign it permissions by executing the below command:

sudo mkdir -p target
sudo chown -R $USER:$USER ~/apache-maven-3.9.11

Creating a target directory for building Maven from its source code and assigning it permissions

Next, build the source code of Maven by executing the command below:

mvn clean install

Building maven from its source file on Ubuntu

Once the build process is completed, there will be a message of build success, as in the image below:

Successfully build Maven from its source file on Ubuntu

Note: To use the source file, the binary file might need to be installed, as there is a possibility of getting the error of mvn command not found.

Conclusion

There are three ways to install Maven on Ubuntu discussed in this guide: using the apt package manager, using the Apache Maven binary, and source file. The source file method is for developers who want to modify its code to their needs.

 

Index
Scroll to Top