Getting Maven set up on Linux requires downloading the binary archive and extracting it to a directory like `/opt`. If you have been wondering how to install maven on linux, you are in the right place. This guide walks you through every step, from checking prerequisites to verifying your installation. Whether you are a developer setting up a build environment or a sysadmin automating deployments, this tutorial covers all major Linux distributions.
Maven is a powerful build automation tool used primarily for Java projects. It manages dependencies, compiles code, and packages applications. Installing it on Linux is straightforward once you know the right commands. Let us get started without any unnecessary fluff.
Prerequisites For Installing Maven On Linux
Before you begin, ensure your system meets the basic requirements. Maven requires Java Development Kit (JDK) installed on your machine. Without Java, Maven cannot run.
- Java JDK version 8 or higher (version 11 or 17 recommended)
- A user account with sudo privileges
- Internet connection to download Maven archive
- Basic familiarity with terminal commands
Check if Java is already installed by running this command in your terminal:
java -version
If you see version information, you are good to go. If not, install OpenJDK using your package manager. For Ubuntu or Debian, use:
sudo apt update && sudo apt install default-jdk -y
For CentOS, RHEL, or Fedora, use:
sudo dnf install java-11-openjdk -y
Once Java is ready, move on to the actual installation process.
How To Install Maven On Linux
This section covers the manual installation method, which works on all Linux distributions. Using the official binary archive ensures you get the latest version and full control over the installation path.
Step 1: Download The Latest Maven Binary Archive
Visit the official Apache Maven download page or use wget to fetch the archive directly. As of this writing, the latest stable version is 3.9.6. Replace the version number with the current one if needed.
wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
If wget is not installed, install it first using your package manager. For Ubuntu:
sudo apt install wget -y
For CentOS:
sudo yum install wget -y
Step 2: Extract The Archive To /Opt Directory
Once downloaded, extract the tar.gz file to the /opt directory. This is the standard location for manually installed software.
sudo tar -xzf apache-maven-3.9.6-bin.tar.gz -C /opt
This creates a folder named apache-maven-3.9.6 inside /opt. You can rename it to maven for simplicity:
sudo mv /opt/apache-maven-3.9.6 /opt/maven
Step 3: Set Up Environment Variables
For Maven to work from any terminal session, you need to set the MAVEN_HOME and update the PATH variable. Create a new file in /etc/profile.d/:
sudo nano /etc/profile.d/maven.sh
Add the following lines to the file:
export MAVEN_HOME=/opt/maven
export PATH=$MAVEN_HOME/bin:$PATH
Save the file and make it executable:
sudo chmod +x /etc/profile.d/maven.sh
Load the environment variables into your current session:
source /etc/profile.d/maven.sh
Step 4: Verify The Installation
Check that Maven is installed correctly by running:
mvn -version
You should see output similar to this:
Apache Maven 3.9.6
Maven home: /opt/maven
Java version: 11.0.20, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-11-openjdk-amd64
...
If you see the version information, congratulations! You have successfully installed Maven.
Alternative Installation Methods
While manual installation gives you full control, there are other ways to install Maven on Linux. Each method has its pros and cons.
Using Package Managers (Ubuntu/Debian)
You can install Maven using apt, but the version might be older than the latest release.
sudo apt update
sudo apt install maven -y
Check the version:
mvn -version
This method is simpler but may not provide the newest features. If you need a specific version, stick with the manual approach.
Using Package Managers (CentOS/RHEL/Fedora)
On Red Hat-based systems, use dnf or yum:
sudo dnf install maven -y
Or for older systems:
sudo yum install maven -y
Again, the repository version might be outdated. For production environments, manual installation is recommended.
Using SDKMAN (For Developers)
SDKMAN is a tool for managing multiple versions of SDKs. Install SDKMAN first:
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
Then install Maven:
sdk install maven
This method is great if you switch between different Maven versions frequently.
Configuring Maven For Your Project
After installation, you might want to customize Maven settings. The global configuration file is located at /opt/maven/conf/settings.xml. The user-specific settings file is at ~/.m2/settings.xml.
Setting Up Local Repository Path
By default, Maven stores downloaded dependencies in ~/.m2/repository. To change this, edit the settings.xml file:
nano ~/.m2/settings.xml
Add the following inside the
/path/to/your/repo
This is useful if you have limited space on your home partition.
Configuring Proxy Settings
If your network uses a proxy, add this to the settings.xml:
example-proxy
true
http
proxy.example.com
8080
Replace the host and port with your actual proxy details.
Adding Custom Repositories
For private dependencies, add repository entries under the
my-repo
https://myrepo.example.com/releases
Common Issues And Troubleshooting
Even with a perfect installation, you might encounter some problems. Here are the most common ones and how to fix them.
Mvn Command Not Found
If you get “command not found” after installation, the PATH variable is not set correctly. Re-source the profile file or log out and back in.
source /etc/profile.d/maven.sh
Also check that the file exists and is executable.
Java Home Not Set
Maven requires JAVA_HOME to be set. Add this to your ~/.bashrc or /etc/profile.d/maven.sh:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Adjust the path based on your Java installation location.
Permission Denied When Extracting
If you get permission errors when extracting to /opt, use sudo. The /opt directory requires root privileges for writing.
sudo tar -xzf apache-maven-3.9.6-bin.tar.gz -C /opt
Outdated Version From Package Manager
If you installed via apt or dnf and got an older version, remove it and use the manual method:
sudo apt remove maven -y
sudo apt autoremove -y
Then follow the manual steps above.
Uninstalling Maven From Linux
If you need to remove Maven, the process depends on how you installed it.
Removing Manual Installation
Delete the Maven directory and the profile file:
sudo rm -rf /opt/maven
sudo rm /etc/profile.d/maven.sh
Also remove any environment variables from your shell configuration files.
Removing Package Manager Installation
For apt-based systems:
sudo apt remove maven -y
For dnf-based systems:
sudo dnf remove maven -y
Clean up residual files with:
sudo apt autoremove -y
Best Practices For Maven On Linux
To keep your Maven installation healthy and efficient, follow these tips.
- Always use the latest stable version for security patches
- Keep your settings.xml file backed up
- Use a dedicated user for build automation if possible
- Regularly clean the local repository to free up space
- Set up a local repository manager like Nexus or Artifactory for team use
These practices will save you time and prevent headaches down the road.
Frequently Asked Questions
Do I Need To Install Java Before Maven?
Yes, Maven requires Java JDK version 8 or higher. Install Java first, then proceed with Maven installation.
Can I Install Maven Without Root Access?
Yes, you can extract the archive to your home directory and set PATH accordingly. However, you will need to configure environment variables manually each time you open a terminal.
What Is The Difference Between Maven And Gradle?
Both are build automation tools, but Maven uses XML for configuration while Gradle uses Groovy or Kotlin. Maven has a steeper learning curve but is widely adopted in enterprise environments.
How Do I Update Maven To A Newer Version?
Download the new binary archive, extract it to /opt, and update the MAVEN_HOME variable. You can keep multiple versions and switch between them by changing the symlink.
Is Maven Available In The Default Repositories?
Yes, but the version is often outdated. For the latest features and security fixes, manual installation is recommended.
Final Thoughts
Now you know exactly how to install maven on linux. The manual method gives you the most control and ensures you have the latest version. Whether you choose apt, dnf, or SDKMAN, the core steps remain similar.
Remember to verify your installation with mvn -version after setup. If you run into issues, refer to the troubleshooting section above. Maven is a reliable tool that will streamline your Java development workflow once configured properly.
Take a few minutes to set up your settings.xml file for proxy and repository configurations. This upfront work will pay off when you start building complex projects. Happy coding!