// Copyright (C) 2023 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[installation]]
= Installing Gradle

[[gs:installation]]
== Gradle Installation

If all you want to do is run an existing Gradle project, then you don't need to install Gradle if the build uses the <<gradle_wrapper#gradle_wrapper,Gradle Wrapper>>.
This is identifiable by the presence of the `gradlew` or `gradlew.bat` files in the root of the project:

[listing,subs=+macros]
----
.   // <1>
├── gradle
│   └── wrapper // <2>
├── gradlew         // <3>
├── gradlew.bat     // <3>
└── ⋮
----
<1> Project root directory.
<2> <<gradle_wrapper.adoc#gradle_wrapper,Gradle Wrapper>>.
<3> Scripts for executing Gradle builds.

If the `gradlew` or `gradlew.bat` files are already present in your project, *you do not need to install Gradle*.
But you need to make sure your system <<installation#sec:prerequisites,satisfies Gradle's prerequisites>>.

You can follow the steps in the <<upgrading_version_8.adoc#upgrading_version_8,Upgrading Gradle section>> if you want to update the Gradle version for your project.
Please use the <<gradle_wrapper.adoc#sec:upgrading_wrapper,Gradle Wrapper>> to upgrade Gradle.

Android Studio comes with a working installation of Gradle, so you *don't need to install Gradle separately when only working within that IDE*.

If you do not meet the criteria above and decide to install Gradle on your machine, first check if Gradle is already installed by running `gradle -v` in your terminal.
If the command does not return anything, then Gradle is not installed, and you can follow the instructions below.

You can install Gradle Build Tool on Linux, macOS, or Windows.
The installation can be done manually or using a package manager like https://sdkman.io/[SDKMAN!] or https://brew.sh/[Homebrew].

You can find all Gradle releases and their checksums on the link:{website}/releases[releases page].

[[sec:prerequisites]]
== Prerequisites
Gradle runs on all major operating systems. It requires link:{jdkDownloadUrl}[Java Development Kit] (JDK) version {minJdkVersion} or higher to run. You can check the <<compatibility.adoc#compatibility,compatibility matrix>> for more information.

To check, run `java -version`:

----
❯ java -version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment Homebrew (build 11.0.18+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.18+0, mixed mode)
----

----
❯ java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
----

Gradle uses the JDK it finds in your path, the JDK used by your IDE, or the JDK specified by your project.
In this example, the $PATH points to JDK17:

----
❯ echo $PATH
/opt/homebrew/opt/openjdk@17/bin
----

You can also set the `JAVA_HOME` environment variable to point to a specific JDK installation directory.
This is especially useful when multiple JDKs are installed:

----
❯ echo %JAVA_HOME%
C:\Program Files\Java\jdk1.7.0_80
----

----
❯ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
----

Gradle supports link:https://kotlinlang.org/[Kotlin] and link:https://groovy-lang.org/[Groovy] as the main build languages.
Gradle ships with its own Kotlin and Groovy libraries, therefore they do not need to be installed.
Existing installations are ignored by Gradle.

<<compatibility.adoc#compatibility, See the full compatibility notes for Java, Groovy, Kotlin, and Android.>>

== Linux installation

.Installing with a package manager
[%collapsible]
====

link:http://sdkman.io[SDKMAN!] is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD).
Gradle is deployed and maintained by SDKMAN!:

----
❯ sdk install gradle
----

Other package managers are available, but the version of Gradle distributed by them is not controlled by Gradle, Inc.
Linux package managers may distribute a modified version of Gradle that is incompatible or incomplete when compared to the official version.
====

.Installing manually
[%collapsible]
====

**Step 1 - link:{website}/releases[Download] the latest Gradle distribution**

The distribution ZIP file comes in two flavors:

- Binary-only (bin)
- Complete (all) with docs and sources

We recommend downloading the bin file; it is a smaller file that is quick to download (and the latest documentation is available online).

**Step 2 - Unpack the distribution**

Unzip the distribution zip file in the directory of your choosing, e.g.:

[subs="attributes"]
----
❯ mkdir /opt/gradle
❯ unzip -d /opt/gradle gradle-{gradleVersion}-bin.zip
❯ ls /opt/gradle/gradle-{gradleVersion}
LICENSE  NOTICE  bin  README  init.d  lib  media
----

[[sec:linux_macos_users_2]]

**Step 3 - Configure your system environment**

To install Gradle, the path to the unpacked files needs to be in your Path.
Configure your `PATH` environment variable to include the `bin` directory of the unzipped distribution, e.g.:

[subs="attributes"]
----
❯ export PATH=$PATH:/opt/gradle/gradle-{gradleVersion}/bin
----

Alternatively, you could also add the environment variable `GRADLE_HOME` and point this to the unzipped distribution.
Instead of adding a specific version of Gradle to your `PATH`, you can add `$GRADLE_HOME/bin` to your `PATH`.
When upgrading to a different version of Gradle, simply change the `GRADLE_HOME` environment variable.

[subs="attributes"]
----
export GRADLE_HOME=/opt/gradle/gradle-{gradleVersion}
export PATH=${GRADLE_HOME}/bin:${PATH}
----
====

== macOS installation

.Installing with a package manager
[%collapsible]
====

link:http://sdkman.io[SDKMAN!] is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD).
Gradle is deployed and maintained by SDKMAN!:

----
❯ sdk install gradle
----

Using link:http://brew.sh[Homebrew]:

----
❯ brew install gradle
----

Using link:https://www.macports.org[MacPorts]:

----
❯ sudo port install gradle
----

Other package managers are available, but the version of Gradle distributed by them is not controlled by Gradle, Inc.
====

.Installing manually
[%collapsible]
====

**Step 1 - link:{website}/releases[Download] the latest Gradle distribution**

The distribution ZIP file comes in two flavors:

- Binary-only (bin)
- Complete (all) with docs and sources

We recommend downloading the bin file; it is a smaller file that is quick to download (and the latest documentation is available online).

**Step 2 - Unpack the distribution**

Unzip the distribution zip file in the directory of your choosing, e.g.:

[subs="attributes"]
----
❯ mkdir /usr/local/gradle
❯ unzip gradle-{gradleVersion}-bin.zip -d /usr/local/gradle
❯ ls /usr/local/gradle/gradle-{gradleVersion}
LICENSE	NOTICE	README	bin	init.d	lib
----

**Step 3 - Configure your system environment**

To install Gradle, the path to the unpacked files needs to be in your Path.
Configure your `PATH` environment variable to include the `bin` directory of the unzipped distribution, e.g.:

[subs="attributes"]
----
❯ export PATH=$PATH:/usr/local/gradle/gradle-{gradleVersion}/bin
----

Alternatively, you could also add the environment variable `GRADLE_HOME` and point this to the unzipped distribution.
Instead of adding a specific version of Gradle to your `PATH`, you can add `$GRADLE_HOME/bin` to your `PATH`.
When upgrading to a different version of Gradle, simply change the `GRADLE_HOME` environment variable.

It's a good idea to edit `.bash_profile` in your home directory to add `GRADLE_HOME` variable:

[subs="attributes"]
----
export GRADLE_HOME=/usr/local/gradle/gradle-{gradleVersion}
export PATH=$GRADLE_HOME/bin:$PATH
----
====

== Windows installation

.Installing manually
[%collapsible]
====

**Step 1 - link:{website}/releases[Download] the latest Gradle distribution**

The distribution ZIP file comes in two flavors:

- Binary-only (bin)
- Complete (all) with docs and sources

We recommend downloading the bin file.

**Step 2 - Unpack the distribution**

Create a new directory `C:\Gradle` with **File Explorer**.

Open a second **File Explorer** window and go to the directory where the Gradle distribution was downloaded. Double-click the ZIP archive to expose the content.
Drag the content folder `gradle-{gradleVersion}` to your newly created `C:\Gradle` folder.

Alternatively, you can unpack the Gradle distribution ZIP into `C:\Gradle` using the archiver tool of your choice.

**Step 3 - Configure your system environment**

To install Gradle, the path to the unpacked files needs to be in your Path.

In **File Explorer** right-click on the `This PC` (or `Computer`) icon, then click `Properties` -> `Advanced System Settings` -> `Environmental Variables`.

Under `System Variables` select `Path`, then click `Edit`.
Add an entry for `C:\Gradle\gradle-{gradleVersion}\bin`.
Click `OK` to save.

Alternatively, you can add the environment variable `GRADLE_HOME` and point this to the unzipped distribution.
Instead of adding a specific version of Gradle to your `Path`, you can add `__%GRADLE_HOME%__\bin` to your `Path`.
When upgrading to a different version of Gradle, just change the `GRADLE_HOME` environment variable.
====

[[sec:running_and_testing_your_installation]]
== Verify the installation

Open a console (or a Windows command prompt) and run `gradle -v` to run gradle and display the version, e.g.:

[subs="attributes"]
----
❯ gradle -v

------------------------------------------------------------
Gradle {gradleVersion}
------------------------------------------------------------

Build time:   2023-03-03 16:41:37 UTC
Revision:     7d6581558e226a580d91d399f7dfb9e3095c2b1d

Kotlin:       1.8.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.6 (Homebrew 17.0.6+0)
OS:           Mac OS X 13.2.1 aarch64

----

If you run into any trouble, see the <<troubleshooting.adoc#sec:troubleshooting_installation,section on troubleshooting>>.

You can verify the integrity of the Gradle distribution by downloading the SHA-256 file (available from the link:{website}/releases[releases page]) and following these <<gradle_wrapper.adoc#sec:verification,verification instructions>>.
