Learn how to set up a Clojure development environment with step-by-step instructions for installing Java, Leiningen, and configuring popular IDEs like IntelliJ IDEA with Cursive, Emacs with CIDER, and VSCode with Calva.
As a Java professional venturing into the world of Clojure, setting up your development environment is a crucial first step. This guide will walk you through the process of installing the necessary tools and configuring your environment to maximize productivity. We’ll cover the installation of Java, Leiningen (Clojure’s build tool), and setting up popular IDEs or text editors such as IntelliJ IDEA with Cursive, Emacs with CIDER, and VSCode with Calva. Additionally, we’ll provide tips for configuring the REPL (Read-Eval-Print Loop) for interactive development.
Clojure runs on the Java Virtual Machine (JVM), so having Java installed is a prerequisite. Here’s how you can set it up:
Before installing Java, check if it’s already installed on your system:
java -version
If Java is installed, this command will display the version number. Ensure you have Java 8 or later.
JAVA_HOME
with the path to your JDK installation.%JAVA_HOME%\bin
to the Path
variable.Using Homebrew: Open Terminal and run:
brew install openjdk@11
Set JAVA_HOME: Add the following to your .bash_profile
or .zshrc
:
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
Verify Installation:
java -version
Using APT (Debian/Ubuntu):
sudo apt update
sudo apt install openjdk-11-jdk
Using YUM (CentOS/RHEL):
sudo yum install java-11-openjdk-devel
Set JAVA_HOME: Add the following to your .bashrc
or .bash_profile
:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Verify Installation:
java -version
Leiningen is the build automation tool for Clojure, similar to Maven or Gradle for Java. It simplifies project management and dependency handling.
Download the Leiningen Script: Open a terminal and run:
curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o lein
Make the Script Executable:
chmod +x lein
Move the Script to a Directory in Your PATH:
sudo mv lein /usr/local/bin/
Run Leiningen: Execute the following command to download the Leiningen self-install package:
lein
This command will download the necessary files and set up Leiningen.
Choosing the right IDE or text editor can significantly enhance your productivity. Here, we’ll cover setup instructions for IntelliJ IDEA with Cursive, Emacs with CIDER, and VSCode with Calva.
IntelliJ IDEA is a popular choice among Java developers, and the Cursive plugin adds excellent support for Clojure.
Install IntelliJ IDEA: Download and install IntelliJ IDEA.
Install the Cursive Plugin:
File > Settings > Plugins
.Create a New Clojure Project:
File > New > Project
.Configure the REPL:
Run
menu and select Edit Configurations
.Emacs is a powerful text editor favored by many developers for its extensibility. CIDER is the Clojure Interactive Development Environment that integrates with Emacs.
Install Emacs: Follow the instructions on the GNU Emacs website to install Emacs on your system.
Install CIDER:
Open Emacs and add the following to your .emacs
or init.el
file:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
Install CIDER by running M-x package-refresh-contents
followed by M-x package-install RET cider RET
.
Create a Clojure Project:
Use Leiningen to create a new project:
lein new app my-clojure-app
Open the project in Emacs.
Start the REPL:
M-x cider-jack-in
to start the REPL.Visual Studio Code is a lightweight, versatile editor with robust support for Clojure through the Calva extension.
Install Visual Studio Code: Download and install VSCode.
Install the Calva Extension:
Ctrl+Shift+X
).Create a Clojure Project:
Use Leiningen to create a new project:
lein new app my-clojure-app
Open the project folder in VSCode.
Configure the REPL:
Ctrl+Shift+P
) to run Calva: Start a Project REPL and Connect
.The REPL (Read-Eval-Print Loop) is a powerful tool for interactive development in Clojure. Here’s how to configure it for optimal use:
The REPL allows you to evaluate Clojure expressions interactively, providing immediate feedback. It’s an essential tool for exploring code, testing functions, and debugging.
lein repl
in your project directory to start the REPL.To further enhance your Clojure development experience, consider the following tools and tips:
Setting up a Clojure development environment involves installing Java, Leiningen, and configuring your preferred IDE or text editor. By following the steps outlined in this guide, you’ll be well-equipped to start developing Clojure applications efficiently. Remember to leverage the REPL for interactive development and explore additional tools to enhance your productivity.
With your environment set up, you’re ready to dive deeper into Clojure and explore the functional programming paradigms that set it apart from traditional object-oriented languages like Java.