Headless Sonic Pi on Raspberry Pi

Hi, I haven’t found much recent information on this on the forum but sorry if I’m missing something obvious.

Is there a way once to launch a script on a headless SonicPi instance? I’m basically looking for a command line version of sonic pi that can take a config file or some command line arguments to pass it the script to load, etc.

I have a Blokas PiSound which I would like to use to run SonicPi script along with some custom midi controllers. Is that possible?

Hello @droussel,

There is no built-in command line access.
Several people have created external tools which allow you to communicate directly with the Sonic Pi server from the command line.
Some options:

(These also may have been ported to/designed with other languages, just have a search on your favourite search engine).
Note: these may or may not be up to date with a change made in Sonic Pi a little while ago, regarding the way the server listened for network messages.

Alternatively, if you’re just after a way to automatically load and run a particular script on Sonic Pi startup, and you don’t mind running the GUI, you can add your code to ~/.sonic-pi/config/init.rb. (or run an external file via run_file from there too for example).

Thanks @ethancrawford!

Ok, so it means I need to have an x server running for SPi to work. I’ll look into how the sonic-pi-tool though, it may be what I need.


Np. As noted on discord, the third party tools are all command line based, it’s only the init.rb approach that needs a GUI.

I have used this technique to run Sonic Pi headless. However it is not compatible with using VNCserver on the same Pi.
However with the latest OS, I boot (for example on a Pi400) with VNCserver enabled, but no monitor attached, and then I can either attach with a vnc client from my mac, or I send signals to SP via the cli gem as Ethan suggests. Note you can set SP to boot automatically by copying the desktop file to the autostart folder
cp usr/share/applications/sonic-pi.desktop ~/.config/autostart
Once you have SP running with your PiSound board, you can use the sonic-pi-cli to send a file of commands to sonic pi using sonic_pi "run_file '/path/to/filename'" The cli has to be running on the same pi, but it is possible to call it from a remote machine using a python script.
4 years ago I did quite a large project involving 2 PiSound boards and an iPad or iPhone running TouchOSC which shows what can be done headless. (the code probably needs slight tweaking to suit the latest OSC protocols in SP 3.3.1)

1 Like