How to Fix 'gradlew: Permission denied'
In this tutorial, you'll learn about How to Fix 'gradlew: Permission denied. We cover key concepts, practical examples, and best practices.
The Problem
You try to run the Gradle wrapper and get:
bash: ./gradlew: Permission denied
The shell cannot execute the gradlew script because it lacks the executable permission.
Quick Fix
1. Make gradlew executable
The most common fix is adding the executable bit:
chmod +x gradlew
./gradlew build
Verify the permission changed:
ls -l gradlew
# Output should show -rwxr-xr-x (the x bits are set)
2. Fix Windows line endings (CRLF)
If the project was cloned on Windows or a ZIP was extracted, gradlew may contain CRLF line endings. The shebang (#!/bin/sh) fails with a carriage return appended:
# Check for CRLF endings
file gradlew
# Output shows "CRLF line terminators"
# Convert to Unix line endings
sed -i 's/\r$//' gradlew
# or use dos2unix
dos2unix gradlew
# Then make executable
chmod +x gradlew
After conversion, the script should show LF instead of CRLF in the file output.
3. Verify Java is installed and in PATH
The Gradle wrapper requires Java. If Java is not found, the script exits silently or with a confusing error:
# Check Java version
java -version
# If Java is not found, install it
# Ubuntu/Debian
sudo apt install default-jdk
# macOS
brew install openjdk@17
# Set JAVA_HOME if needed
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
./gradlew build
Add JAVA_HOME to your shell profile (~/.bashrc or ~/.zshrc) to make it permanent.
4. Run from the correct directory
The gradlew script must be run from the project root (the directory containing gradlew, settings.gradle, and build.gradle or build.gradle.kts):
# Correct — project root
cd /home/user/myproject/
./gradlew build
# Wrong — subdirectory
cd /home/user/myproject/app/
../gradlew build # works but not standard
Check that gradlew is in the current directory:
ls -la gradlew
5. Fix the Gradle wrapper JAR
If gradle/wrapper/gradle-wrapper.jar is missing or corrupted (sometimes GitHub LFS or .gitignore issues cause this), the wrapper cannot download Gradle:
# Check if the JAR exists
ls -la gradle/wrapper/gradle-wrapper.jar
# If missing, regenerate the wrapper
gradle wrapper --gradle-version 8.5
If you do not have gradle installed globally, download the JAR from the Gradle distribution or re-clone the repository.
6. Check the filesystem mount options
On shared drives or containers, the filesystem may be mounted with noexec, preventing execution of any binary or script:
# Check mount options
mount | grep $(df . --output=target | tail -1)
# If noexec is present, run from a different location
cp -r /project /home/user/project
cd /home/user/project
chmod +x gradlew
./gradlew build
Prevention
- Add
gradlewto version control with the executable bit set (git add --chmod=+x gradlew). - Configure Git to handle line endings: create a
.gitattributesfile withgradlew text eol=lf. - Use a consistent JDK version across the team with the Gradle toolchain or
gradle/wrapper/gradle-wrapper.properties. - Run
git check-attr eol gradlewto verify the line ending attribute is applied.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro