DEV Community

Cover image for Installing Gradle on Ubuntu 24.04
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Installing Gradle on Ubuntu 24.04

Gradle is an open-source build automation tool that streamlines multi-language development workflows, especially for large-scale Java applications. It automates tasks such as compiling, testing, packaging, and deploying applications using Java, Kotlin, Scala, Android, Groovy, C++, or Swift, and integrates with other tools and platforms to simplify build, test, and deployment processes. This guide installs Gradle on Ubuntu 24.04 using three different methods — the latest release file, APT, and Snap — then builds a sample Java application, adds a web server dependency, and covers uninstalling Gradle. By the end, you'll have Gradle installed and a working Java web application built and running with it.

Prerequisites: an Ubuntu 24.04 server and a non-root user with sudo privileges.


Install Gradle on Ubuntu 24.04

You can install Gradle using the latest release file, APT, or Snap on Ubuntu 24.04. Pick one of the three methods below.

Method 1: Install Gradle Using the Latest Release File

The Gradle release ZIP archive lets you install the latest or a specific version from source, ensuring compatibility and access to up-to-date features.

1. Update the server's package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install the required Java Development Kit (JDK) package:

$ sudo apt install default-jdk -y
Enter fullscreen mode Exit fullscreen mode

3. View the installed Java version:

$ java --version
Enter fullscreen mode Exit fullscreen mode

4. Visit the Gradle release page, identify the latest version, and copy its binary direct download link. Then use wget to download the file. For example, Gradle 8.12:

$ wget https://services.gradle.org/distributions/gradle-8.12-bin.zip
Enter fullscreen mode Exit fullscreen mode

5. Extract the Gradle ZIP archive contents to a system-wide directory such as /opt:

$ sudo unzip -d /opt/gradle gradle-8.12-bin.zip
Enter fullscreen mode Exit fullscreen mode

Set up environment variables. Setting up a global environment variable with your Gradle installation directory lets you execute the Gradle binary as a global system-wide command.

6. Create a new gradle.sh script in the /etc/profile.d directory:

$ sudo nano /etc/profile.d/gradle.sh
Enter fullscreen mode Exit fullscreen mode

7. Add the following directives to the gradle.sh file:

export GRADLE_HOME=/opt/gradle/gradle-8.12 
export PATH=${GRADLE_HOME}/bin:${PATH}
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

8. Enable execute permissions on the script:

$ sudo chmod +x /etc/profile.d/gradle.sh
Enter fullscreen mode Exit fullscreen mode

9. Load the Gradle environment configuration in your active shell:

$ source /etc/profile.d/gradle.sh
Enter fullscreen mode Exit fullscreen mode

10. Verify the installed Gradle version:

$ gradle --version
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to Gradle 8.12!
Here are the highlights of this release:
 - Enhanced Error and Warning Messages
 - IDE Integration Improvements
 - Daemon JVM Information
For more details see https://docs.gradle.org/8.12/release-notes.html
------------------------------------------------------------
Gradle 8.12
------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Method 2: Install Gradle Using APT

Gradle is available in the default APT package repositories on Ubuntu 24.04, though the packaged version isn't the latest.

1. Update the server's package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install Gradle:

$ sudo apt install gradle -y
Enter fullscreen mode Exit fullscreen mode

3. View the installed Gradle version:

$ gradle --version
Enter fullscreen mode Exit fullscreen mode

Output:

openjdk version "21.0.5" 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)
------------------------------------------------------------
Gradle 4.4.1
------------------------------------------------------------
Build time:   2012-12-21 00:00:00 UTC
Revision:     none
Groovy:       2.4.21
Ant:          Apache Ant(TM) version 1.10.14 compiled on September 25 2023
JVM:          21.0.5 (Ubuntu 21.0.5+11-Ubuntu-1ubuntu124.04)
OS:           Linux 6.8.0-48-generic amd64
Enter fullscreen mode Exit fullscreen mode

Method 3: Install Gradle Using Snap

Gradle is available in the Snap Store, but the included version might not be the latest. Snap offers a direct installation procedure with faster updates.

1. Install the Snap daemon if it's not installed:

$ sudo apt install snapd -y
Enter fullscreen mode Exit fullscreen mode

2. Search the available Gradle version in the Snap Store:

$ sudo snap search gradle
Enter fullscreen mode Exit fullscreen mode

Output:

Name           Version   Publisher      Notes    Summary
gradle         7.2       snapcrafters✪  classic  An open-source build automation tool
gum            0.13.0    aalmiray       classic  Gum is a Gradle/Maven/Ant/Bach/JBang wrapper written in Go
jreleaser      1.0.0-M1  aalmiray       -        Release Java projects quickly and easily with JReleaser
om26er-gradle  4.7       om26er         -        Accelerate developer productivity
jrel-test      1.0.0-M9  aalmiray       -        Release projects quickly and easily with JReleaser
Enter fullscreen mode Exit fullscreen mode

Gradle 7.2 is the available version in the Snap Store based on the above output.

3. Install Gradle using Snap:

$ sudo snap install gradle --classic
Enter fullscreen mode Exit fullscreen mode

4. View the installed Gradle version:

$ sudo snap run gradle --version
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to Gradle 7.2!
Here are the highlights of this release:
 - Toolchain support for Scala
 - More cache hits when Java source files have platform-specific line endings
 - More resilient remote HTTP build cache behavior
For more details see https://docs.gradle.org/7.2/release-notes.html
------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Create a Basic Java Application Using Gradle

1. Create a new sample_project directory to use with Gradle:

$ mkdir sample_project
Enter fullscreen mode Exit fullscreen mode

2. Switch to the sample_project directory:

$ cd sample_project
Enter fullscreen mode Exit fullscreen mode

3. Initialize the Gradle project to create a basic Gradle project structure:

$ gradle init --type java-application
Enter fullscreen mode Exit fullscreen mode

Output:

.........
BUILD SUCCESSFUL in 6s
2 actionable tasks: 2 executed
Enter fullscreen mode Exit fullscreen mode

During initialization, press Enter to use the default Java version, verify the project name, select 1 for a single application project, select your build script DSL (for example, 1 for Kotlin), select your test framework (for example, 1 for JUnit 4), and answer yes to use the new APIs. A successful build looks like:

BUILD SUCCESSFUL in 1m 5s
1 actionable task: 1 executed
Enter fullscreen mode Exit fullscreen mode

4. Create the src/main/java directory to store your application files:

$ mkdir -p src/main/java
Enter fullscreen mode Exit fullscreen mode

5. Create a new App.java Java application file in the src/main/java directory using a text editor such as nano:

$ nano src/main/java/App.java
Enter fullscreen mode Exit fullscreen mode

6. Add the following contents to the file:

public class App {
    public static void main(String[] args) {
        System.out.println("Hello from Gradle!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Save and close the file. The program prints a greeting message when executed.

7. Open the build.gradle file to ensure it includes the Java plugin and specifies the main class:

$ nano build.gradle
Enter fullscreen mode Exit fullscreen mode

8. Add the following contents to the file:

plugins {
    id 'application'
}

application {
    // Define the main class
    mainClass = 'App'
}

jar {
    manifest {
        attributes(
            'Main-Class': 'App'
        )
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // Add dependencies here if needed
}
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

9. Build and compile the Java application into a single file using Gradle:

$ gradle build
Enter fullscreen mode Exit fullscreen mode

This compiles the application, runs tests, and packages it into a single .jar file.

Output:

Calculating task graph as no cached configuration is available for tasks: build

BUILD SUCCESSFUL in 1m 12s
12 actionable tasks: 12 executed
Configuration cache entry stored.
Enter fullscreen mode Exit fullscreen mode

10. Run the application using Gradle:

$ gradle run
Enter fullscreen mode Exit fullscreen mode

Output:

> Task :run
Hello from Gradle!

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 1s
4 actionable tasks: 2 executed, 2 up-to-date
Enter fullscreen mode Exit fullscreen mode

11. Run the bundled .jar file using Java:

$ java -jar build/libs/sample_project.jar App
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from Gradle!
Enter fullscreen mode Exit fullscreen mode

Add Web Server Dependencies for Gradle

Gradle supports multiple dependencies to perform specific application functions. Install the Spark Java web server dependency to run web applications on a dedicated port.

1. Open the build.gradle file:

$ nano build.gradle
Enter fullscreen mode Exit fullscreen mode

2. Modify the dependencies section to include a new SparkJava directive:

dependencies {
    // Add Spark Java for lightweight HTTP server
    implementation 'com.sparkjava:spark-core:2.9.4'
}
Enter fullscreen mode Exit fullscreen mode

Save and close the file. Your modified dependencies section should look like this:

dependencies {
    // Add dependencies here if needed
    // Add Spark Java for lightweight HTTP server
    implementation 'com.sparkjava:spark-core:2.9.4'
}
Enter fullscreen mode Exit fullscreen mode

3. Back up the App.java application file:

$ mv src/main/java/App.java src/main/java/App.ORIG
Enter fullscreen mode Exit fullscreen mode

4. Create the App.java file again:

$  nano src/main/java/App.java
Enter fullscreen mode Exit fullscreen mode

5. Add the following code to the App.java file to enable a basic HTTP server using Spark Java:

import static spark.Spark.*;

public class App {
    public static void main(String[] args) {
        port(8080); // Set the port number

        // Define a route for the root URL
        get("/", (req, res) -> {
            res.type("text/html");
            return "<h1>Hello from Gradle!</h1>";
        });

        System.out.println("Server is running at http://localhost:8080/");
    }
}
Enter fullscreen mode Exit fullscreen mode

Save and close the file. This imports the Spark Java package, runs the application on port 8080, and prints a greeting message when accessed.

6. Allow connections to port 8080 through the firewall:

$ sudo ufw allow 8080
Enter fullscreen mode Exit fullscreen mode

7. Reload UFW to apply the firewall changes:

$ sudo ufw reload
Enter fullscreen mode Exit fullscreen mode

8. Start the application using Gradle:

$ gradle run
Enter fullscreen mode Exit fullscreen mode

Output:

> Task :run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Server is running at http://localhost:8080/
<====---------> 37% EXECUTING [49s]
> :run
Enter fullscreen mode Exit fullscreen mode

9. Access your server's IP address on port 8080 using a web browser and verify that your web application displays.

Uninstall Gradle on Ubuntu 24.04

Follow the steps below to uninstall Gradle depending on your installation method.

1. Delete the Gradle binary to disable it:

$ sudo rm -rf /opt/gradle/gradle-8.12/bin/gradle
Enter fullscreen mode Exit fullscreen mode

2. Remove the Gradle environment variable script:

$ sudo rm /etc/profile.d/gradle.sh
Enter fullscreen mode Exit fullscreen mode

3. Uninstall Gradle if you installed it using APT:

$ sudo apt autoremove gradle -y
Enter fullscreen mode Exit fullscreen mode

4. Uninstall Gradle if installed using Snap:

$ sudo snap remove gradle
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Integrate Gradle with a CI/CD pipeline to automate builds, tests, and deployments
  • Explore Gradle plugins for Kotlin, Android, or Spring Boot projects
  • Configure the Gradle wrapper (gradlew) so your project builds consistently across machines
  • Add unit and integration test suites using JUnit or Spock

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)