Dev

Install Apache Groovy on Ubuntu 24.04 / 22.04

Apache Groovy is a dynamic programming language for the Java platform that combines the power of Java with scripting language productivity. It compiles to JVM bytecode and integrates seamlessly with existing Java libraries. Groovy is the scripting language behind Jenkins pipelines, Gradle build scripts, and Spring Boot’s Grails framework.

Original content from computingforgeeks.com - post 5342

This guide covers installing Apache Groovy on Ubuntu 24.04 and 22.04 using SDKMAN (the recommended method) and from the official Apache releases.

Prerequisites

  • Ubuntu 24.04 or 22.04 with sudo access
  • Java JDK 11 or later (Groovy 4.x requires at least Java 11)

Install Java JDK

Groovy runs on the JVM, so Java is required. Install OpenJDK from the Ubuntu repository:

sudo apt update
sudo apt install -y default-jdk

Verify the Java installation:

java -version

Ubuntu 24.04 installs OpenJDK 21:

openjdk version "21.0.x" 2024-xx-xx

SDKMAN is a version manager for JVM-based tools. It handles installation, upgrades, and multiple version management for Groovy, Gradle, Maven, and other JVM tools.

Install SDKMAN:

curl -s "https://get.sdkman.io" | bash

Load SDKMAN into your current shell:

source "$HOME/.sdkman/bin/sdkman-init.sh"

Install the latest stable Groovy version:

sdk install groovy

Verify the installation:

groovy --version

To install a specific version:

sdk install groovy 4.0.24

List available versions:

sdk list groovy

Install Groovy from Apache Releases

If you prefer a manual installation without SDKMAN, download Groovy directly from the Apache website:

GROOVY_VER=4.0.24
curl -sLO "https://groovy.jfrog.io/artifactory/dist-release-local/groovy-zips/apache-groovy-binary-${GROOVY_VER}.zip"
sudo unzip "apache-groovy-binary-${GROOVY_VER}.zip" -d /opt/
sudo ln -s "/opt/groovy-${GROOVY_VER}" /opt/groovy

Add Groovy to your PATH by creating a profile script:

echo 'export GROOVY_HOME=/opt/groovy' | sudo tee /etc/profile.d/groovy.sh
echo 'export PATH=$GROOVY_HOME/bin:$PATH' | sudo tee -a /etc/profile.d/groovy.sh
source /etc/profile.d/groovy.sh

Verify Groovy is accessible:

groovy --version

Test Groovy

Run a quick one-liner to confirm everything works:

groovy -e 'println "Hello from Groovy ${GroovySystem.version} on ${System.getProperty("os.name")}"'

Create a simple script file:

cat > /tmp/hello.groovy << 'EOF'
def name = "Groovy"
def version = GroovySystem.version
println "Running ${name} ${version}"
println "Java version: ${System.getProperty('java.version')}"
println "OS: ${System.getProperty('os.name')} ${System.getProperty('os.version')}"
EOF

Run it:

groovy /tmp/hello.groovy

Launch the Groovy Console

Groovy ships with an interactive console (groovyConsole) for testing code snippets and a REPL shell (groovysh) for interactive sessions:

groovysh

This opens an interactive Groovy shell where you can type expressions and see results immediately. Type :quit to exit.

Conclusion

Apache Groovy is installed and ready for scripting, build automation, and JVM development on your Ubuntu server. SDKMAN makes it easy to manage multiple versions and upgrade when new releases come out. Refer to the official Groovy documentation for language guides, API reference, and integration with frameworks like Grails and Spock.

Related Articles

Monitoring Configure Graylog Nginx proxy with Let’s Encrypt SSL Docker Install Graphite and Graphite-Web on Ubuntu 22.04|20.04 Debian How to configure mpd and ncmpcpp on Linux Prometheus Monitor Linux Server using Prometheus and Grafana in 5 minutes

Leave a Comment

Press ESC to close