How to install Maven on Ubuntu Server


Installing Maven is something that we do very often as Mulesoft Architects and Developers. For that reason, it’s very important that we consistently install it in the same way. 

Here are the steps I follow every time I install Maven on an Ubuntu Server.

Prerequisites

To follow this tutorial we will need the following:
  • A VM with Ubuntu Server - in this example we will be using Ubuntu Server 24.04 LTS
  • Java installed - in this example, we’ve got OpenJDK 17 installed on the server. Check out this post to see How to install Java on Ubuntu Server
  • The JAVA_HOME environment variable set up

Download and Install Maven

Download Maven

Go to https://maven.apache.org/download.cgi and copy the link to the Binary tar.gz archive. At the time of writing this tutorial, Maven 3.9.9 is the latest version. If you need a previous version scroll down the page and find the version you need. Just make sure the Maven version is compatible with the Java JDK. For example, Maven versions 3.8+ require JDK 1.7 or above.



Go to the command line of your Ubuntu Server and use wget to download the link you’ve just copied

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


Unzip the file

tar -xvf apache-maven-3.9.9-bin.tar.gz 

Do an 
ls and you’ll see there’s a new folder. 

Install Maven

Create a folder for the Maven installation. I like to create a folder  /opt/maven and paste the Maven installation in there. This way, if we need to work with more than one Maven version we can have all the Maven installations under /opt/maven. For that, create the folder with the command:

sudo mkdir /opt/maven

Copy the extracted folder to the maven folder:

sudo mv apache-maven-3.9.9 /opt/maven 


Set up the M2_HOME and PATH environment variables

To set those env variables permanently we will define them in the user profile file (.profile). For that, edit that file with the command:

vi ~/.profile

Add the following lines at the end of the file:

export M2_HOME="/opt/maven/apache-maven-3.9.9"
PATH="$PATH:$M2_HOME/bin"
export PATH

Relaunch the shell session or reload the profile file to apply the changes with the source command:

source ~/.profile

Verify the env variables have been correctly set up:

env


Verify the Installation

To verify the Maven installation was successful run the command:

mvn -version


And with that, you’re ready to Mavenize the world! 
Previous Post Next Post