Installing Gradle
Before we move forward with running Gradle, we must have it installed on our machine. There are multiple ways through which Gradle can be installed and updated. We will first see a more manual way to install Gradle and then take a quick look at installing it via some commonly used package managers. We can choose any one method that fits the bill. Irrespective of the way we install Gradle, we must meet the following prerequisite.
Gradle needs Java Runtime Environment (JRE) 6 or Java Development Kit (JDK) 1.6 or higher. There is no other dependency. We recommend having JDK installed. To verify this, on the command line, we can check the Java version with the following command:
$ java -version java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
If we don't see the output more or less like the one shown in the preceding command, there is problem with our JDK installation.
Note
The latest JDK can be downloaded from the following URL:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Installing manually
If we want a finer control over the installation then this is a suitable route. This could be the case, when we cannot use the package managers, want very specific binaries to be downloaded and installed, or behind corporate firewalls where automatic downloading by package managers is not allowed. We need to download the Gradle binaries and make them available for use on the command line.
The latest Gradle distribution can be downloaded from http://www.gradle.org/downloads. As of writing the latest version is 2.9.
Gradle binary distribution comes in two flavors as follows:
gradle-2.9-all.zip
: This contains binaries, sources, and documentationgradle-2.9-bin.zip
: This contains binaries only
We can download any of the above depending on what we need. Also, this is an OS-independent zip so the same zip can be extracted on Mac OS X, Windows, and Linux. The next section makes the Gradle command available on the command line. This section is dependent on the OS we use.
Installing on Mac OS X and Linux
Let's say we extracted the downloaded zip as ~/gradle-2.9/
. Now, we just need to add the following two lines at the end of .bashrc
/, .bash_profile
/, or .zshrc
, depending on the OS and the shell that we use:
export GRADLE_HOME=~/gradle-2.9 export PATH=$PATH:$GRADLE_HOME/bin
Restart the terminal or source the modified file to have the change take effect.
Installing on Windows
Let's say we extracted the zip as C:\gradle-2.9
, then perform the following steps:
- Open the Start menu, right click on Computer and select Properties.
- On Advanced system settings, select the Advanced tab, and then select Environment Variables....
- Click on New.
- Create a
GRADLE_HOME
environment variable with the valueC:\gradle-2.9
.Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Tip
In future when we download the later version of Gradle, we would need to change on this value to point to the correct folder.
- Edit (or add if not already there) the
PATH
environment variable. At the end of its value, append;%GRADLE_HOME%\bin
(add a semicolon if multiple path entries exist).
Alternate methods of installing Gradle
Although the manual installation gives absolute control over the installation process, various tasks such as downloading and extracting the right version, upgrading to the latest versions, uninstalling, and editing environment variables quickly become cumbersome and error-prone. That is why many people prefer package managers to control the whole process.
Installing via OS-specific package managers
While installing manually, as mentioned in the previous section, is very easy, we can make it super-easy by using a package manager.
Some Linux distributions like Ubuntu ship with their package manager, Mac OS X, Windows don't have any package manager installed by default. However, luckily, there are multiple package managers available for both platforms. We will see the example of Homebrew on Mac and Chocolatey on Windows.
Mac OS X
Make sure we have Homebrew installed. If it is, installing Gradle is only a matter of using the following command:
$ brew install gradle
Note
More details on Homebrew can be found at http://brew.sh.
Linux (Ubuntu)
Using the built in package manager on Ubuntu, which is called Advanced Packaging Tool (APT), we can install Gradle with the following command:
$ sudo apt-get install gradle
Windows
If we have Chocolatey installed, installing Gradle is just a command away:
c:\> cinst gradle
Note
More details on Chocolatey can be found at https://chocolatey.org.
Installing via SDKMAN
SDKMAN stands for the Software Development Kit Manager. In its own words, the website describes it as: SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems.
The advantage SDKMAN has over other package managers is that we can have multiple Gradle versions installed on a system and select a different version for a given project. If we have it installed, all we need to do is run following command:
$ sdk install gradle
SDKMAN can be installed from http://sdkman.io/.
Verifying the installation
In whichever way we choose to install Gradle, it's a good idea to verify that if it's working before we move ahead. We can do this by simply checking for Gradle's version on the command line:
$ gradle --version ------------------------------------------------------------ Gradle 2.9 ------------------------------------------------------------ Build time: 2015-11-17 07:02:17 UTC Build number: none Revision: b463d7980c40d44c4657dc80025275b84a29e31f Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_25 (Oracle Corporation 25.25-b02) OS: Mac OS X 10.10.5 x86_64
If we see output similar to the above, we have Gradle installed correctly on our machine.
Tip
We can use -v
instead --version
to get the same result.
Setting JVM options
Although it's not required most of the time, but if in case we need to set some global options for the JVM that Gradle will use, Gradle provides us a convenient way to do that. We can set the GRADLE_OPTS
environment variable with acceptable flags to tune the JVM.
Gradle also honors the JAVA_OPTS
environment variable. However, we need to be careful when setting it, as this affects the setting for all the Java programs on a machine. Setting that we want to keep common for all the Java apps should be done via this variable and those that only need to be applied to Gradle should be set via GRADLE_OPTS
.
Tip
Some commonly used options are -Xms
and -Xmx
, which set the minimum and maximum heap size of the JVM.