Step-by-step guide to installing Clojure CLI tools and Leiningen on Windows, macOS, and Linux, with verification through simple Clojure programs.
As a Java developer venturing into the world of Clojure, setting up your development environment is a crucial first step. This section provides a detailed guide to installing Clojure CLI tools and Leiningen across different operating systems: Windows, macOS, and Linux. We will also verify the installation by running simple Clojure programs, ensuring your setup is ready for development.
Clojure CLI tools provide a robust command-line interface for managing Clojure projects. They facilitate tasks such as running REPLs, managing dependencies, and executing scripts. The CLI tools are designed to be lightweight and integrate seamlessly with existing development workflows.
Prerequisites: Ensure you have Java Development Kit (JDK) installed. Clojure requires Java to run. You can download the latest JDK from Oracle’s official website.
Download and Install Scoop: Scoop is a command-line installer for Windows, which simplifies the installation of Clojure CLI tools.
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
iwr -useb get.scoop.sh | iex
Install Clojure: With Scoop installed, you can now install Clojure by running:
scoop install clojure
Verify Installation: To verify the installation, open a new terminal window and run:
clojure -M -e "(println \"Hello, Clojure!\")"
You should see the output Hello, Clojure!
.
Prerequisites: Ensure you have Homebrew installed. Homebrew is a package manager for macOS that simplifies software installation. If not installed, you can do so by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Clojure: With Homebrew, installing Clojure is straightforward:
brew install clojure/tools/clojure
Verify Installation: Open a terminal and execute:
clojure -M -e "(println \"Hello, Clojure!\")"
The output should be Hello, Clojure!
.
Prerequisites: Ensure your system has Java installed. You can install OpenJDK using your package manager. For example, on Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk
Install Clojure: Use the following script to install Clojure CLI tools:
curl -O https://download.clojure.org/install/linux-install-1.10.3.1029.sh
chmod +x linux-install-1.10.3.1029.sh
sudo ./linux-install-1.10.3.1029.sh
Verify Installation: Run the following command in your terminal:
clojure -M -e "(println \"Hello, Clojure!\")"
You should see Hello, Clojure!
as the output.
Leiningen is a build automation tool for Clojure, akin to Maven for Java. It simplifies project management, dependency resolution, and builds processes. Leiningen is essential for Clojure developers as it streamlines the creation and management of Clojure projects.
Download Leiningen Script: The installation process for Leiningen is similar across all platforms. Start by downloading the Leiningen script:
curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o lein
Make the Script Executable: Change the permissions to make the script executable:
chmod +x lein
Move to a Directory in Your PATH: Move the script to a directory that’s in your system’s PATH, such as /usr/local/bin
:
sudo mv lein /usr/local/bin/
Run Leiningen: Execute the following command to download Leiningen’s self-installing package:
lein
This command will download the necessary files and set up Leiningen for use.
Verify Installation: To verify, create a new Clojure project:
lein new app my-clojure-app
cd my-clojure-app
lein run
You should see output indicating that the application is running, typically printing “Hello, World!”.
Once Clojure and Leiningen are installed, it’s beneficial to run a few simple programs to ensure everything is functioning correctly.
Create a file named arithmetic.clj
with the following content:
(ns arithmetic)
(defn add [a b]
(+ a b))
(println "Sum of 5 and 3 is:" (add 5 3))
Run the program using the Clojure CLI:
clojure -M arithmetic.clj
You should see the output Sum of 5 and 3 is: 8
.
Navigate to your project directory and edit the src/my_clojure_app/core.clj
file:
(ns my-clojure-app.core)
(defn -main
"A simple program that prints a greeting."
[& args]
(println "Welcome to Clojure with Leiningen!"))
Run the program using Leiningen:
lein run
The output should be Welcome to Clojure with Leiningen!
.
Installing Clojure and Leiningen sets the foundation for your journey into functional programming with Clojure. With these tools, you can efficiently manage projects, dependencies, and build processes, leveraging Clojure’s powerful features for scalable data solutions. As you continue, explore more advanced topics and integrate Clojure with NoSQL databases to build robust applications.