Error Knowledge Base MAVEN LOCAL_REPOSITORY_PERMISSION_DENIED

Could not create local repository

Maven cannot write to the local repository directory (`~/.m2/repository` by default), usually due to permissions or a read-only filesystem.

Affected versions: All supported Apache Maven versions.

What This Error Means

Maven cannot write to the local repository directory (~/.m2/repository by default), usually due to permissions or a read-only filesystem.

How to Fix It

  1. Fix ownership/permissions of the local repo directory (avoid using sudo mvn ...).
  2. If you're in a container/CI, mount a writable volume for ~/.m2 or set a writable local repo path: mvn -Dmaven.repo.local=/tmp/m2 -DskipTests package.
  3. Ensure there is enough disk space and that the filesystem is not read-only.
  4. Retry the original Maven command and confirm Maven can create directories under the local repository path.

Why It Happens

  • The ~/.m2 directory (or filesystem) is owned by a different user (common after running Maven with sudo).
  • The filesystem is read-only (containers, locked-down CI runners, sandboxed environments).
  • Disk is full or quota limits prevent writes.
  • Custom local repository path (-Dmaven.repo.local=...) points to a non-writable location.

How to Verify

  1. Re-run mvn -DskipTests package and confirm Maven downloads artifacts and writes them under the local repository path.
  2. Confirm new directories appear under ~/.m2/repository/ (or your custom maven.repo.local).

Manual filesystem checks

  1. Check the local repository path Maven will use: mvn -q help:evaluate -Dexpression=settings.localRepository -DforceStdout.
  2. Confirm the directory exists and is writable by the current user: ls -la ~/.m2 and ls -la ~/.m2/repository.
  3. Check free disk space on the filesystem containing ~/.m2: df -h.

Common CLI Output

[ERROR] Could not create local repository at /Users/you/.m2/repository
java.nio.file.AccessDeniedException: /Users/you/.m2/repository

How Maven uses the local repository

  1. Maven caches downloaded artifacts and metadata in a local repository (default: ~/.m2/repository).
  2. If Maven cannot create directories or write files there, dependency/plugin resolution fails even if the network and repositories are correct.

Prevention Tips

  • Never run Maven as root on developer machines, fix permissions instead.
  • In CI, explicitly set a writable local repository cache location and persist it between runs.
  • Monitor disk space on CI runners to prevent hidden cache failures.

Where This Can Be Triggered

github.com/apache/maven/blob/maven-3.6.3/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java

Maven fails early if it cannot create the local repository directory and throws "Could not create local repository at ...". - GitHub

if (!localRepositoryBasedir.exists() && !localRepositoryBasedir.mkdirs()) {
    throw new MavenExecutionRequestPopulationException("Could not create local repository at " + localRepositoryBasedir);
}
request.setLocalRepositoryPath(localRepositoryBasedir);

Need help or found a mistake? Contact RepoFlow support for questions.

Join our mailing list