How to Install Gradle on Ubuntu 24.04

Updated on January 31, 2025
How to Install Gradle on Ubuntu 24.04 header image

Gradle is an open-source build automation tool that streamlines multi-language development workflows, especially for large-scale Java applications. To get started, you can install Gradle on Ubuntu 24.04, which automates tasks such as compiling, testing, packaging, and deploying applications using Java, Kotlin, Scala, Android, Groovy, C++, or Swift. It also integrates with other tools and platforms to simplify the build, test, and deployment processes.

This article explains how to install Gradle on Ubuntu 24.04 and set it up to run applications on a server.

Install Gradle on Ubuntu 24.04

You can install Gradle using the latest release file, APT or Snap on Ubuntu 24.04. Follow the steps below to use a specific installation method to install Gradle on your server.

Install Gradle on Ubuntu 24.04 Using the Latest Release File

The Gradle release ZIP archive enables you to install the latest or a specific version from source. This ensures compatibility and access to up-to-date features for your applications. Follow the steps below to install the required dependency packages and download the latest Gradle release file to install on your server.

  1. Update the server's package index.

    console
    $ sudo apt update
    
  2. Install the required Java Development Kit (JDK) package.

    console
    $ sudo apt install default-jdk -y
    
  3. View the installed Java version.

    console
    $ java --version
    
  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, download the Gradle 8.12 version.

    console
    $ wget https://services.gradle.org/distributions/gradle-8.12-bin.zip
    
  5. Extract the Gradle ZIP archive contents to a system-wide directory such as /opt

    console
    $ sudo unzip -d /opt/gradle gradle-8.12-bin.zip
    

Setup Environment Variables

Setting up a global environment variable with your Gradle installation directory allows you to execute the Gradle binary as a global system-wide command. Follow these steps to create a script in the /etc/profile.d directory for the new global environment variable

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

    console
    $ sudo nano /etc/profile.d/gradle.sh
    
  2. Add the following directives to the gradle.sh file.

    sh
    export GRADLE_HOME=/opt/gradle/gradle-8.12 
    export PATH=${GRADLE_HOME}/bin:${PATH}
    

    Save and close the file.

  3. Enable execute permissions on the script.

    console
    $ sudo chmod +x /etc/profile.d/gradle.sh
    
  4. Load the Gradle environment configuration in your active shell.

    console
    $ source /etc/profile.d/gradle.sh
    
  5. Verify the installed Gradle version.

    console
    $ gradle --version
    

    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
    ------------------------------------------------------------

Create a Basic Java Application Using Gradle

Follow the steps below to create a basic Java application and run it using Gradle on your server.

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

    console
    $ mkdir sample_project
    
  2. Switch to the sample_project directory.

    console
    $ cd sample_project
    
  3. Initialize the Gradle project to create a basic Gradle project structure.

    console
    $ gradle init --type java-application
    

    Output:

    .........
    BUILD SUCCESSFUL in 6s
    2 actionable tasks: 2 executed
    • Press Enter to use the default Java version in your project.

      Starting a Gradle Daemon (subsequent builds will be faster)
      
      Enter target Java version (min: 7, default: 21): 
    • Verify your project name and press Enter.

      Project name (default: sample_project): 
    • Enter 1 to set up a single application project.

      Select application structure:
        1: Single application project
        2: Application and library project
      Enter selection (default: Single application project) [1..2] 
    • Select your desired build language. For example, enter 1 to select Kotlin as the build script language.

      Select build script DSL:
        1: Kotlin
        2: Groovy
      Enter selection (default: Kotlin) [1..2] 
    • Select the target test framework. For example, enter 1 to select JUnit 4.

      Select test framework:
        1: JUnit 4
        2: TestNG
        3: Spock
        4: JUnit Jupiter
      Enter selection (default: JUnit Jupiter) [1..4] 
    • Press yes to use new APIs with Gradle and initialize your project.

      Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] 

    Your output should be similar to the one below when the build process is successful.

    BUILD SUCCESSFUL in 1m 5s
    1 actionable task: 1 executed
  4. Create the src/main/java directory to store your application files.

    console
    $ mkdir -p src/main/java
    
  5. Create a new App.java Java application file in the src/main/java directory using a text editor such as nano.

    console
    $ nano src/main/java/App.java
    
  6. Add following contents to the file.

    java
    public class App {
        public static void main(String[] args) {
            System.out.println("Greetings from Vultr!");
        }
    }
    

    Save and close the file.

    The above Java program outputs a Greetings from Vultr message when executed.

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

    console
    $ nano build.gradle
    
  8. Add the following contents to the file.

    ini
    plugins {
        id 'application'
    }
    
    application {
        // Define the main class
        mainClass = 'App'
    }
    
    jar {
        manifest {
            attributes(
                'Main-Class': 'App'
            )
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        // Add dependencies here if needed
    }
    

    Save and close the file.

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

    console
    $ gradle build
    

    The above command compiles the Java application code, runs tests, and packages the application 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.
  10. Run the application using Gradle.

    console
    $ gradle run
    

    Output:

    > Task :run
    Greetings from Vultr!
    
    > Task :app:run
    Hello World!
    
    BUILD SUCCESSFUL in 1s
    4 actionable tasks: 2 executed, 2 up-to-date
  11. Run the bundled .jar file using Java.

    console
    $ java -jar build/libs/sample_project.jar App
    

    Output:

    Greetings from Vultr!

Add Web Server Dependencies for Gradle

Gradle supports multiple dependencies to perform specific application functions. Follow the steps below to install the Spark Java web server dependency to enable Gradle to run web applications using dedicated ports on your server.

  1. Open the build.gradle file.

    console
    $ nano build.gradle
    
  2. Modify the dependencies section to include a new SparkJava directive.

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

    Save and close the file.

    Your modified dependencies section should look like the one below.

    ini
    dependencies {
        // Add dependencies here if needed
        // Add Spark Java for lightweight HTTP server
        implementation 'com.sparkjava:spark-core:2.9.4'
    }
    
  3. Back up the App.java application file.

    console
    $ mv src/main/java/App.java src/main/java/App.ORIG
    
  4. Create the App.java file again.

    console
    $  nano src/main/java/App.java
    
  5. Add the following code to the App.java file to enable a basic HTTP server using Spark Java.

    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>Greetings from Vultr!</h1>";
            });
    
            System.out.println("Server is running at http://localhost:8080/");
        }
    }
    

    Save and close the file.

    The above modified Java application code imports the Spark Java package, runs the application on port 8080 and outputs a Greetings from Vultr message when accessed.

  6. Allow connections to port 8080 through the default firewall.

    console
    $ sudo ufw allow 8080
    
  7. Reload UFW to apply the firewall changes.

    console
    $ sudo ufw reload
    
  8. Start the application using Gradle.

    console
    $ gradle run
    

    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
  9. Access your server's IP address on port 8080 using a web browser such as Firefox and verify that your web application displays.

    Application default page

Uninstall Gradle on Ubuntu 24.04

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

  1. Delete the Gradle binary to disable it on your server.

    console
    $ sudo rm -rf /opt/gradle/gradle-8.12/bin/gradle
    
  2. Remove the Gradle environment variable script.

    console
    $ sudo rm /etc/profile.d/gradle.sh
    
  3. Run the following command to uninstall Gradle if you installed it using APT.

    console
    $ sudo apt autoremove gradle -y
    
  4. Uninstall Gradle if installed using Snap.

    console
    $ sudo snap remove gradle
    

Conclusion

You have installed Gradle on Ubuntu 24.04 and run sample applications on your server. Gradle is highly scalable and integrates with multiple frameworks to manage and build applications. For more information and configuration options, visit the Gradle documentation.