# Sonic Pi for standalone installations

**URL:** <https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225>\
**Category:** Feature Requests\
**Created:** [October 11, 2017, 9:39am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225 "2017-10-11T09:39:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![guardabrazo](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/guardabrazo/32/170_2.png) [@guardabrazo](https://in-thread.sonic-pi.net/u/guardabrazo)\
**Post date:** [October 11, 2017, 9:39am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/1 "2017-10-11T09:39:16Z")

</div>

Hi there!

I’m working on a headless sound installation and I would love to use Sonic Pi for it.  
The ideal situation would be having an option to save a Sonic Pi project as an executable that auto runs when you open it.

Has anyone tried something similar? Is this something you’d like to be able to do?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [October 11, 2017, 9:56am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/2 "2017-10-11T09:56:48Z")

</div>

Hi @guardabrazo,

this is definitely something I’d like to see supported in the future 🙂

Currently we have an init file you can write which will auto-execute on launch. If you navigate into your `~/.sonic-pi/` folder and open up the `init.rb` file you’ll see something like this:

```ruby
# Sonic Pi init file
# Code in here will be evaluated on launch.

```

If you place Sonic Pi code in there, it will be automatically executed when you start Sonic Pi.

Does this help you get started?

---

<div class="post-metadata">

**Author:** ![guardabrazo](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/guardabrazo/32/170_2.png) [@guardabrazo](https://in-thread.sonic-pi.net/u/guardabrazo)\
**Post date:** [October 11, 2017, 10:20am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/3 "2017-10-11T10:20:40Z")

</div>

Hi @samaaron! Thanks for the quick reply…

It definitely looks promising! I’ll try it later and get back here to share the results.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [October 11, 2017, 2:49pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/4 "2017-10-11T14:49:29Z")

</div>

I have used Sonic Pi in a headless mode quite a lot, and also auto-booting into a running sonic pi music file. I have used the following techniques to achieve this.

First, one of the easiest ways to get it to autoplay something, [as Sam has pointed out](http://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/2?u=robin.newman), is to put your code in the init.rb file. This is useful, but ALWAYS runs every time you boot Sonic Pi, and you have to remove the code to stop this.

To start Sonic Pi automatically there are two techniques, which depend upon whether you want to run it without starting the GUI environment or want to start it from the (normal) gui environment.  
In the latter case setup autoboot to Sonic Pi as follows:  
Open a terminal window:

```ruby
cd ~
mkdir -p .config/autostart
cp /usr/share/applications/sonic-pi.desktop .config/autostart

```

Make sure that the Raspberry Pi is set to boot to the desktop (logged in) (you can change this using the Raspberry Pi Configuration program in Preferences on the main menu if necessary). Reboot your Raspberry PI and it should boot into Sonic Pi and run the code in the init.rb file.

The second method is a little more involved, but allows Sonic Pi to autoboot and run without having to start the desktop from a command line environment, and so uses fewer resources. Also why start the desktop if you are going to run headless, which is where I use this technique.

To get this to work, you first need to install xvfb. This is a package which lets you run a virtual frame buffer to fool the program (in this case Sonic Pi) into thinking it has a normal desktop environment.

```ruby
sudo apt-get update
sudo apt-get install xvfb

```

Now you set up a script which will start sonic-pi running using xvfb, which can be run from the command line. You can create the script using the nano editor from the command line. I set mine up in Desktop folder, but you can place it anywhere you like.

```ruby
cd ~/Desktop
nano startsp.sh

```

Type in the contents of the file:

```ruby
#!/bin/sh
Xvfb :1 & xvfb-run sonic-pi 2 >/dev/null &

```

Then quit and save the file typing **ctrl+X** followed by **Y**. If you check the file typing `cat startsp.sh` you should see

```ruby
#!/bin/sh
Xvfb :1 & xvfb-run sonic-pi 2 >/dev/null &

```

on the screen. Now set this executable by typing `chmod +x startsp.sh`

This generates the file to start Sonic PI. To get it to execute use crontab as follows: type `crontab -e`

(If this is the first time you have used this, select nano as the editor when prompted)  
scroll down to the end of the file and add the line:  
`@reboot /home/pi/Desktop/startup.sh &`

Then exit the file using **ctrl+X** followed by **Y** to resave the file

If you now set your Raspberry Pi to boot to the CLI logged in as user pi, then it will automatically boot into a running version of Sonic Pi. However you have no means of directly accessing the normal buffers in Sonic Pi which store your programs. There are two solutions to this:

1. Use the init.rb file to hold the code you want to run.
2. install the sonic-pi-cli gem which will let you send commands to sonic pi including using the run\_file command which you can utilise to play any sonic pi file.

I use this in my Sonic Pi JukeBox program which utilises the technique above and on which you can get more details [here](https://rbnrpi.wordpress.com/a-touchosc-jukebox-for-sonicpi/)

Finally you can inhibit either of the techniques used here to run Sonic Pi as follows for the Desktop autoboot just remove the sonic-pi.desktop file from the autostart folder for the cli boot, edit the crontab file using `crontab -e` and just put a `#` comment symbol before the @reboot line you added. You can remove the `#` again if you want to reinstate it.

---

<div class="post-metadata">

**Author:** ![guardabrazo](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/guardabrazo/32/170_2.png) [@guardabrazo](https://in-thread.sonic-pi.net/u/guardabrazo)\
**Post date:** [October 12, 2017, 5:29pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/5 "2017-10-12T17:29:57Z")

</div>

Wow @robin.newman , super useful answer! I can’t wait to test everything you said.

I’ll get back here to show you the results!

---

<div class="post-metadata">

**Author:** ![Thndr](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/thndr/32/121_2.png) [@Thndr](https://in-thread.sonic-pi.net/u/Thndr)\
**Post date:** [October 12, 2017, 9:43pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/6 "2017-10-12T21:43:02Z")

</div>

I’m not sure if it runs it better or not, but [Sonic-Pi-Tool](https://github.com/lpil/sonic-pi-tool) allows you to boot sonic-pi headless via `sonic-pi-tool start-server` so you wouldn’t have to make your own shortcut.

Only thing that I know may be an issue is you have to install Rust/Cargo and compile it

---

<div class="post-metadata">

**Author:** ![enkerli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/enkerli/32/54_2.png) [@enkerli](https://in-thread.sonic-pi.net/u/enkerli)\
**Post date:** [October 18, 2017, 2:28am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/7 "2017-10-18T02:28:51Z")

</div>

Tried both methods. The sonic-pi-tool one does work but it throws a number of errors and never completes so, with a `sonic-pi-tool start-server; sonic-pi-tool eval-file script.rb` (going from memory on this syntax, but it should be right), it never reaches the file. Naming my file `init.rb` and placing it in the right folder does work. And adding the `sonic-pi.desktop` file to the `.config/autostart` does work.

My script makes Sonic Pi into a synthesizer (takes MIDI signal to `control` a really long note). Especially useful in headless mode for a compact setup to bring to a jam or some such. Added two little beeps at the start of that script so that, when it’s done loading, Sonic Pi warns me and it’s then possible to just play without worrying about a thing.

But, then, the experience is even more seamless with the MODEP emulation of the MOD Duo, that Blokes has made for the pisound HAT. This is a real “it just works” experience and the synths, though limited, are quite decent.

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [June 23, 2018, 3:59pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/8 "2018-06-23T15:59:57Z")

</div>

I’ve made a python port of Sonic-Pi-Tool (available [here](https://github.com/emlyn/sonic-pi-tool)) in case you want to use it without installing Rust.

I think the reason `sonic-pi-tool start-server; sonic-pi-tool eval-file script.rb` doesn’t work is that `start-server` runs in the foreground, so it never completes (until you kill the Sonic Pi server). You’d need to run it in the background with something like `sonic-pi-tool start-server & sonic-pi-tool eval-file script.rb` (maybe with a sleep in between the two commands to give the server time to start up).

Another option might be to run them both as separate autostart apps (probably including a sleep at the start of the `eval-file` one again to give Sonic Pi time to start up).

---

<div class="post-metadata">

**Author:** ![jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jonny](https://in-thread.sonic-pi.net/u/jonny)\
**Post date:** [September 15, 2023, 4:29pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/9 "2023-09-15T16:29:43Z")

</div>

Hi, I just tried my first init.rb test

```ruby
# Sonic Pi init file
# Code in here will be evaluated on launch.
#user\init.rb
puts 'Welcome'

```

I relaunched sp after saving init.rb, then updated the correct file (in user dir, not progfiles), but still not seeing the message.

Does the code need to be wrapped in a block, or is the “puts stream” not open at initiliasation / boottime, or should I be looking somewhere else?

Thanks!

---

<div class="post-metadata">

**Author:** ![drmindflip](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/drmindflip/32/2627_2.png) [@drmindflip](https://in-thread.sonic-pi.net/u/drmindflip)\
**Post date:** [October 8, 2023, 3:36pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/11 "2023-10-08T15:36:27Z")

</div>

Had some confusion getting this working myself until I found the correct place to put my init.rb code:

**Windows:**

```ruby
C:/Users/<username>/.sonic-pi/config/init.rb

```

**Raspberry Pi:**

```ruby
/home/<username>/.sonic-pi/config/init.rb

```

---

<div class="post-metadata">

**Author:** ![xavibove](https://avatars.discourse-cdn.com/v4/letter/x/8baadc/32.png) [@xavibove](https://in-thread.sonic-pi.net/u/xavibove)\
**Post date:** [November 19, 2023, 11:08am UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/12 "2023-11-19T11:08:00Z")

</div>

> [@drmindflip](#):
>
> `/.sonic-pi/config/init.rb`

Hi @drmindflip , any clue where to put init.rb in Mac OS? Thanks!

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [November 19, 2023, 2:26pm UTC](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/13 "2023-11-19T14:26:43Z")

</div>

it goes in `~/,sonic-pi/config/init.rb` This expands to `/Users/<username>/.sonic-pi/config/init.rb`  
The easiest way to get there on a mac is to use the GoToFolder… entry on the mac Go menu and type in `~/.sonic-pi/config`  
there. You can toggle hidden files/folders on the Mac holding down Shift + Command and typing a full stop .
