As a developer, you typically look for ways to enhance application features. If you mainly work on Java-based applications, befriend and install Groovy on your machine. The Groovy language integrates smoothly with any Java program and adds powerful features, including scripting capabilities for Java programs.
And in this tutorial, you’ll learn how to install Groovy, regardless if you’re on an Ubuntu or Windows machine.
Ready? Jump in and get started with Groovy for better application development!
Prerequisites
This tutorial contains step-by-step demonstrations. To follow along, be sure you have the following:
- An Ubuntu machine – This tutorial uses Ubuntu 22.04.
- A Windows 7 or 7+ machine with Java installed.
Installing Groovy on Ubuntu
Before you install Groovy on your Ubuntu machine, you first need to install Java since Groovy runs on its built-in Java servlet container server. You can use multiple Java implementations to work with Groovy, but OpenJDK is the most popular.
To install Java on your Ubuntu machine:
1. Log in to your Ubuntu machine using your favorite SSH client.
2. Next, run the apt update command below to update your system packages.
sudo apt update
3. Once updated, run the following commands to create a directory (mkdir) named ~/install_groovy_demo and switch (cd) to that directory.
These commands don’t provide output, but the ~/install_groovy_demo directory is where you’ll install Java.
mkdir ~/install_groovy_demo
cd ~/install_groovy_demo
4. Now, run the apt install command below to install Java (default-jdk) on your machine.
# Installing Java Version: Java SE 11 (LTS)
sudo apt install default-jdk
5. After installing Java, run the below java command to check the -version of installed Java on your machine.
# Checking the Version of Java to verify Java installation.
java -version
The output below ensures you’ve successfully installed Java.
6. Next, run the below curl command to download the Groovy package (get.sdkman.io).
curl -s get.sdkman.io | bash
If the download is successful, you’ll see an output similar to the one below.
7. Once downloaded, run each command below on the bin directory of the Groovy installation folder (~/.sdkman/bin/) to prepare Groovy scripts (sdksdkman-init.sh) to run successfully.
Note that these commands don’t provide output, but you’ll see the outcome in the following step.
cd ~/.sdkman/bin/
source sdksdkman-init.sh
8. Now, run the sdk install command below to install Groovy.
sdk install groovy
9. Finally, run the below groovy command to verify your Groovy installation.
groovy -version
Executing a Groovy Script on Ubuntu
Great! You’ve successfully installed Groovy on your Ubuntu machine. But how do you know Groovy actually works? In this example, you’ll run a test by executing a simple script to verify if Groovy is appropriately installed.
Within the ~/install_groovy_demo directory, create a script named test.sh and populate the below line, which prints (printLn
) all the numbers starting from 0
to 4
.
0.upto(4) {println "$it"}
Now, run the below groovy
command to execute the Groovy script (*test.sh*
)
groovy test.sh
Installing Groovy on Windows
Do you mainly work on a Windows machine? Worry not. Groovy works on Windows too! But like in Ubuntu, you first have to install Groovy on your Windows machine.
1. Open your favorite web browser, navigate Groovy’s official download page, and click the Download tab, as shown below.
2. Next, scroll down to the latest stable release, Groovy 4.0, at this time of writing, and click on Windows Installer (community artifact).
Your browser redirects to a page where you can get the download link for the Windows installer (step three).
3. Click on the .msi installer’s link (rightmost) to download Groovy.
4. Once downloaded, run Groovy’s Windows installer.
5. Now, click Next when you see the “welcome” page below.
6. Tick the checkbox at the bottom to accept the End-User License Agreement (EULA), and click Next to continue.
7. Further, select the Groovy setup as Typical, which installs only the common features.
As shown below, the installation begins and may take a few minutes to complete.
8. Lastly, click on Finish once Groovy has been installed successfully. And at this point, the command line tools Groovy shell and Groovy console are installed by default.
Running a Groovy Command on Windows
You now have Groovy installed on your Windows machine, which should work as expected. But to be safe, you’ll have to test if Groovy works correctly. How?
Similar to what you did in Ubuntu, you’ll run a Groovy command on your Windows machine. But instead of Windows PowerShell, you’ll use the Groovy Shell to run commands.
Launch Groovy shell from your Desktop or the Start menu, as shown below.
Now, run the below command to print all numbers (printLn
) between 0
and 7
with an increment of 2
.
0.step(7,2){println "$it"}
Conclusion
In this tutorial, you learned how to install Groovy, a multi-faceted language for the Java platform, on Ubuntu and Windows. You touched on executing a Groovy script and running a Groovy command with similar functions.
After realizing how Groovy works, why not automate web deployments using Groovy and create a Jenkins CI CD pipeline? Drastically improve your software development process today!