Skip to content

Raspotify with PulseAudio as a system service

Jason Gray edited this page Oct 31, 2022 · 7 revisions

This guide is user maintained and may be out of date and/or incomplete.

The PulseAudio backend is very much "batteries not included" and not offically supported by the Raspotify maintainers.

This is a very basic guide on how to set up your Raspberry Pi as a headless unit, running PulseAudio as a system service.

As an extra, we will configure a network audio sink, so you will be able to send audio to your Raspberry Pi and have it play over connected devices.

PulseAudio will be running as a system service, which is not recommended, but should be OK in this use case.

The guide will not cover detailed audio subsystem, sound quality or system security settings.

For this setup we are using a Raspberry Pi with a HifiBerry DAC+ Pro. If you are using a different audio device, consult relevant documentation.

NOTE: With this setup it is possible to play audio using your Linux desktop Spotify client, but have it come out of your Raspberry Pi. In this case, you are not using Raspotify, and audio quality will depend on your local network conditions. If, for example, the Raspberry Pi is on the wired network and the computer is connected over WiFi, audio can drop out or lose sync if you are playing a video file.

For best Spotify playback, always select Raspotify as an output device from inside your Spotify app.

Setup Raspberry OS and install PulseAudio

Setup Raspberry OS, using the lite system image. Remember to enable SSH access (and setup a fixed IP address), as we are running headless.

Update all the packages and go through the basic configuration.

If we have a different audio device, now would be a good time to set it up. In the case of Hifiberry DAC+ Pro, we have to add dtoverlay=hifiberry-dacplus to our /boot/config.txt and reboot. Your preferred audio playback device should be visible in aplay -l

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ Pro HiFi pcm512x-hifi-0 [HiFiBerry DAC+ Pro HiFi pcm512x-hifi-0]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

Next, we need to install PulseAudio and the package required for network discovery.

$ sudo apt install pulseaudio pulseaudio-module-zeroconf

PulseAudio service configuration

We will be following the official instructions and disable per-user loading of PulseAudio:

$ sudo systemctl --global disable pulseaudio.service pulseaudio.socket

and we will set autospawn = no in /etc/pulse/client.conf

Also, remember to add your default user to the pulse-access group.

$sudo usermod -a -G pulse-access pi

Now we need to create a Systemd service file for PulseAudio:

/etc/systemd/system/pulseaudio.service

[Unit]
Description=PulseAudio Daemon

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
PrivateTmp=true
ExecStart=/usr/bin/pulseaudio --system --realtime --disallow-exit --no-cpu-limit

Next, we need to enable the network TCP transport module:

/etc/pulse/system.pa

load-module module-native-protocol-tcp auth-ip-acl=192.168.1.0/24
load-module module-zeroconf-publish

We are using auth-ip-acl to control who can connect to the server. This loads the TCP module for PulseAudio, configuring it to accept connections without authentication and only from the 192.168.1.0/24 subnet. You will want to replace this value with the subnet or IP address from which you will be connecting. It is possible to allow connections from absolutely anywhere, but beware that you should only do so if the nature of your network prevents unauthorized access.

There are other access control options.

Also, in /etc/pulse/system.pa, we will set:

load-module module-native-protocol-unix auth-cookie-enabled=0 auth-anonymous=1

auth-cookie-enabled=0 will disable some annoying messages in the log, and auth-anonymous=1 will allow Raspotify to play through the PulseAudio server.

Now we can sudo systemctl enable pulseaudio.service and sudo systemctl start pulseaudio.service.

At this point, you can go to another device running PulseAudio, start paprefs and enable Make discoverable PulseAudio network sound devices available locally. Your PulseAudio sink on the Raspberry Pi should be visible and available as a sink next to the current ones.

Install Raspotify

We can do the easy Raspotify installation:

curl -sL https://dtcooper.github.io/raspotify/install.sh | sh

Everything should work without any additional configuration.


Sources and further reading:

Running PulseAudio as System-Wide Daemon

Running PulseAudio as system service

systemd definition for pulseaudio in system-mode (example for archlinux).

Pulseaudio as system-wide systemd service