Explain Codes LogoExplain Codes Logo

How to install Maven 3 on Ubuntu by using apt-get?

java
prompt-engineering
best-practices
tools
Anton ShumikhinbyAnton Shumikhin·Sep 30, 2024
TLDR

Get Maven rocking on your Ubuntu with just two commands:

sudo apt update # Ask Ubuntu politely for the latest news and updates sudo apt install maven # Command Ubuntu like a pro to install Maven

Check the installation with mvn -version.

Installing on older Ubuntu versions

Previous Ubuntu versions (17.04 and earlier) require a little more love. Before Maven can be installed, a Personal Package Archive (PPA) must be added.

PPA: How to add it

  1. Install the software-properties-common package, because some common software properties can be uncommonly useful:
sudo apt-get install software-properties-common
  1. Add the Maven PPA (be sure to use something reputable, or Maven's own PPA if available):
sudo add-apt-repository ppa:YOURCHOICE/maven3
  1. Update the package list, because freshness matters:
sudo apt-get update # Update like there's no tomorrow

Maven: Let's install

With the PPA nicely tucked in, command the install:

sudo apt-get install maven # Summon Maven into your machine

After the magic

  1. Prove that Maven answered your summon with mvn -version.
  2. If the PPA was merely for the Maven installation, consider letting it go for a cleaner system.

When Maven family reunions go wrong

If your system still houses older Maven residents, you'll want to evict them to make room for Maven 3:

sudo apt-get remove maven maven2 # Politely ask older Mavens to leave sudo apt-get purge maven maven2 # Ensure they didn't leave belongings behind

After the clean-up, feel free to introduce Maven 3 to your system.

Manual installation for the adventurous

Fancy a specific Maven version or dealing with a repository rejection? Manual installation is your adventure.

  1. Hunt down the Maven binaries from the official Maven forest.
  2. Set up camp by extracting the archive to a directory of your choice.
  3. Edit your ~/.bashrc or ~/.profile map, leading to the bin directory of the new camp.
  4. Link Maven’s JAVA_HOME to your JDK installation: add export JAVA_HOME=/path/to/your/jdk in your ~/.bashrc or ~/.profile. Don't forget to replace the /path/to/your/jdk with the real path.
  5. Refresh your system with the new modifications using source ~/.bashrc or source ~/.profile.

Making it work (tips and gotchas)

Dependencies covered?

Ensure all dependencies are installed, particularly JDK (Java Development Kit).

Paths and environment

The bin directory of Maven should be in the system's $PATH variable. They must see eye-to-eye for Maven to work smoothly.

Check, double check

Verify Maven installation with mvn -version, and ensure Java is in sync with it.

Repositories: Yes, they need cleaning too

PPAs aren't self-cleaning. After Maven installation, decide whether to keep or discard the PPA.