Raspberry Pi

How to Install Apache Maven on Raspberry Pi

Apache Maven is an open-source automation tool designed especially to develop Java-based projects. However, it can also be utilized to build and manage projects written in different languages such as Ruby, C# and more. It automates the project-building tasks by minimizing the risk of humans making errors while manually building the software. Thus, you can expect a better user-friendly environment for developing your projects at a quick pace.

In this article, you will learn how to install Apache Maven on a Raspberry Pi so let’s begin:

How to Install Apache Maven on Raspberry Pi

There are two methods to install Apache Maven on Raspberry Pi, which are as follows:

  • Installing Apache Maven Through Raspberry Pi Source List
  • Installing Apache Maven Through tar.gz File

1. Installing Apache Maven Through Raspberry Pi Source List

The installation of Apache Maven on your Raspberry Pi can be done directly from the Raspberry Pi source list because its repository is already included in the list. However, through this process, you won’t be able to install the latest version of Apache Maven on your device.

To perform the Apache Maven installation through the official Raspberry Pi source list, you must update packages using the following command:

$ sudo apt update -y

In case your packages are not updated, you can replace “update” with “upgrade” in the above command to install packages update on your Raspberry Pi.

After the update, you can install Apache Maven on your Raspberry Pi device using the following command:

$ sudo apt install maven -y

To check the Apache Maven version, you can apply the following command:

$ mvn --version

Remove Apache Maven From Raspberry Pi

To remove Apache Maven from your Raspberry Pi, you can issue the following command:

$ sudo apt remove maven -y

2. Installing Apache Maven Through tar.gz File

You can also install Apache Maven’s latest version on your Raspberry Pi by downloading the tar.gz file. However, before that, you first need to install the default Java JDK on your device through the following command:

$ sudo apt install default-jdk -y

After successfully downloading the Java package, follow the below-mentioned steps to install the latest version of Apache Maven on your Raspberry Pi device.

Step 1: Download Apache Maven tar.gz File on Raspberry Pi
To download the latest version of Apache Maven tar.gz file in the /tmp directory of your Raspberry Pi, use the following command:

$ wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz -P /tmp

Step 2: Extract Apache Maven tar.gz File
Next, you will need to extract the Apache Maven tar.gz file into the “/opt” directory by executing following command:

$ sudo tar xf /tmp/apache-maven-3.8.6-bin.tar.gz -C /opt

Step 3: Set Up Environment Variables for Apache Maven
Now, we need to setup the environment variables by creating a script file with the name “maven.sh” and to do so, we have to run the following command:

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

Inside the “maven.sh” file, you will need to add the following text and then save the file using “CTRL+X”.

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Step 4: Make the “maven.sh” File Executable
You will also need to make the “maven.sh” file executable via applying the below-mentioned command:

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

Step 5: Load Environment Variables
After successfully making the “maven.sh” file executable, you have to load the environment variables inside the file using the following command:

$ source /etc/profile.d/maven.sh

Once the variables are loaded, you can execute the following command to confirm the installation of the latest version of Apache Maven on your Raspberry Pi device:

$ mvn --version

At this step, you have successfully installed the latest version of Apache Maven on your Raspberry Pi device. Although Method 1 seems to be the easiest method, if you want to use the latest version of Apache Maven, then you have to follow Method 2.

Conclusion

Apache Maven is a perfect application to develop Java projects with ease and there are two simplest methods to install this application on your device. You can use the Raspberry Pi source list to install the application on your device directly or follow the second method to download and extract the “tar.gz” file from the Apache Maven official website. To follow the second method, you need to create a file “maven.sh” and the load environment variables inside this file to complete the installation of the latest version of Apache Maven on Raspberry Pi.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.