How to install Apache Maven on Linux Cloud Servers

2020-08-13 By Ryan 8879 Views linux apache maven development
7 reviews

Apache Maven is a command-line tool written in Java for building Java applications and JVM (Java virtual machine). So, it is necessary to install Java on the system, and commands from Java JDK (Java Development Kit) must be present in the PATH environment variable before using Maven.


See the instructions for installing, configuring and testing the Nginx server on CentOS, Fedora, Debian and Ubuntu below.


Installing Apache Maven on CentOS/Fedora


  1. Install OpenJDK 8, because Apache Maven requires Java 1.7 or greater.

    # yum install java-1.8.0-openjdk-devel
    

    apachemaven1

  2. Run the below command to check the version once the installation is completed.

    # java -version
    

    apachemaven2

  3. Setup the JAVA_HOME environment variable with the below commands.

    # echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | sudo tee -a /etc/profile
    
    # source /etc/profile
    

    apachemaven3

  4. Now proceed with the installation of Apache Maven. First, change the working directory to /opt/ directory and download the latest stable version of Apache Maven from https://maven.apache.org/download.cgi.

    # cd /opt/
    
    # wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
    

    apachemaven4

  5. Extract downloaded archive once the downloading is completed.

    # tar -zxvf apache-maven-3.6.3-bin.tar.gz
    

    apachemaven5

  6. Now rename the extracted directory to maven and set the permission as mentioned below.

    # mv apache-maven-3.6.3 maven
    
    # chown -R root:root /opt/maven
    

    apachemaven6

  7. Add the path /opt/maven to the PATH environment variable with the below commands.

    # echo 'export PATH=$PATH:/opt/maven/bin' | sudo tee -a /etc/profile
    
    # source /etc/profile
    

    apachemaven7

  8. Now check the version of Apache Maven to verify the installation.

    # mvn --version
    

    apachemaven8


Installing Apache Maven on Ubuntu/Debian


  1. Run the below commands to update the system.

    # apt update
    

    apachemaven9

  2. Now install Java with the below command.

    # apt-get install -y default-jdk
    

    apachemaven10

  3. Verify the Java version with the below command.

    # java -version
    

    apachemaven11

  4. Now proceed with the installation of Apache Maven. First, change the working directory to /opt/ directory and download the latest stable version of Apache Maven from (https://maven.apache.org/download.cgi).

    # cd /opt/
    
    # wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
    

    apachemaven12

  5. Extract downloaded archive once the downloading is completed.

    # tar -xvzf apache-maven-3.6.3-bin.tar.gz
    

    apachemaven13

  6. Now rename the extracted directory to maven.

    # mv apache-maven-3.6.3 maven
    

    apachemaven14

  7. Now set up environment variables such as M2_HOME, JAVA_HOME and PATH. It can do by creating a mavenenv.sh file inside of the /etc/profile.d/ directory:

    # vi /etc/profile.d/mavenenv.sh
    

    apachemaven15

  8. Add the below lines and save then close.

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

    apachemaven16

  9. Give the executable permission with the below command.

    # chmod +x /etc/profile.d/mavenenv.sh
    

    apachemaven17

  10. Now load the environment variables and check the version of Apache Maven to verify the installation.

    # source /etc/profile.d/mavenenv.sh
    
    # mvn --version
    

    apachemaven18

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.