Connecting to a remote REPL with a Clojure aware editor

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.

How to set up a f1-micro VPS on Google Cloud

Services vCore RAM SSD Transfer Monthly
Google Cloud f1-micro VPS
Ionos 1 512Mb 10Gb £1
Amazon Lightsail 1 512Mb 20Gb $3.50
Dedipath 1 512Mb 10Gb 1Gbps unmetered $1.67
Time4VPS 1 x 2.6GHz 2Gb 20Gb 2TB E3.99

Configure local SSH connection

Save the .pem file to ~/.ssh/ directory (or your preferred location).

Edit the ~/.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 HostName.

Use the LocalForward for the port used by the nREPL server.

Host remote-clojure-server
  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. :middleware/cider-clj from practicalli/clojure-deps-edn

Edit your Emacs ~/.emacs.d/init.el file and add the following configuration.

For Spacemacs, edit ~/.spacemacs and add the following code to dotspacemacs/user-config

  (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 :project/new alias

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.

Clojure Terminal REPL UI with Rebel readline

An .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.

clojure -M:repl/rebel-nrepl

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

Clojure Terminal REPL UI with Rebel readline

Confirm the host name, typically localhost when running the REPL locally

Clojure Terminal REPL UI with Rebel readline

Confirm the port number the nREPL server is listening too

Clojure Terminal REPL UI with Rebel readline

Spacemacs is now connected to the REPL and Clojure code can be evaluated in the source code buffers.

results matching ""

    No results matching ""