Connect to a remote REPL with a Clojure aware editor
Using a remote REPL can provide a collaborative coding environment as well as a means for working within remote environments (docker images, cloud servers, etc).
Running a remote REPL can also provide longevity to the process, as many clients (editors, ssh terminals) can connect to the REPL over time, disconnecting from the running process without interupting the process. This provides a very resilient way of running a REPL.
practicalli/clojure-deps-edn provides many aliases to configure a REPL to work with other community tools. These aliases are easier to manage when running a REPL outside of an editor which automatically injects its own configuration. This configuration can be installed into the remote environment and ideally added into the server build process.
Establish a remote environment
A small Virtual Private Server (VPS) is enough for a remote Clojure environment.
- Install Clojure CLI tools and practicalli/clojure-deps-edn configuration
- Git Client
- SSH server
- Set up user account
- Generate a permissions file,
.pemfrom the server (TODO: how to generate .pem files)
|Google Cloud f1-micro VPS|
|Time4VPS||1 x 2.6GHz||2Gb||20Gb||2TB||E3.99|
Configure local SSH connection
.pem file to
~/.ssh/ directory (or your preferred location).
~/.ssh/config file (create the file if needed) and add a host configuration for the remote server.
Add the IP address of the remote server to
LocalForward for the port used by the nREPL server.
Host remote-clojure-server HostName 22.214.171.124 IdentityFile ~/.ssh/remote-server.pem User ubuntu PasswordAuthentication yes LocalForward 7888 localhost:7888 Port 22
Configure Emacs to listen to nREPL port
Emacs can be used to connect to a running Clojure project that has been run with the nREPL and Cider middleware, e.g.
Edit your Emacs
~/.emacs.d/init.el file and add the following configuration.
For Spacemacs, edit
~/.spacemacs and add the following code to
(setq nrepl-use-ssh-fallback-for-remote-hosts t)
Start a REPL on a remote server
The majority of Clojure aware editors can connect to an external REPL using the nREPL protocol. Emacs CIDER, VSCode Calva and NeoVim Conjure all use nREPL and the Cider middleware.
practicalli/clojure-deps-edn defines the
:middleware/clj alias that includes several libraries required for the nREPL connection between CIDER and the REPL.
SSH into the remote server.
Clone an existing project with Git, or create a new project with
Change to a Clojure project
Start the REPL using the cider middleware on the same port as defined in
LocalForward in the SSH configuration, using the
-p to set the port number.
Open a terminal in the root directory of a Clojure project and run a non-interactive REPL process
clojure -M:middleware/cider-clj -p 7888
The REPL will start along with an nREPL server, showing the connection details.
.nrepl-port file is created in the root of the Clojure project, containing the port number the nREPL server is listening upon.
Using a REPL terminal UI
For a rich terminal REPL experience, start the REPL with rebel readline as well as nREPL and CIDER libraries.
Connect to REPL from Spacemacs
Tramp enables Spacemacs to be used to open remote files as if they were local files, using an SSH connection to the remote server.
SPC f f to open a file and enter an ssh address to access the Clojure project files on the remote server
, ' to call
sesman-start and choose
cider-connect-clj which should find the remote host and port
Confirm the host name, typically
localhost when running the REPL locally
Confirm the port number the nREPL server is listening too
Spacemacs is now connected to the REPL and Clojure code can be evaluated in the source code buffers.