Using a Raspberry Pi as Spotify receiver

With Bluetooth and Airplay being around for quite a few years by now, plugging in your mobile phone via AUX cables in order to listen to your favorite Spotify playlist is a memory of the past for most of us. But what to do if your sound system predates the era of wireless connectivity?

For me, this was a real head-scratcher. The combination of an older model SHARP sound system and an iPhone XS left me with the worst of both worlds - a sound system offering USB and AUX as the only means of connecting and a mobile phone without an AUX jack. And this is where my Raspberry Pi 4b and spotifyd enter the scene.

Spotifyd is a UNIX daemon running an open-source Spotify client, written completely in Rust. Except for one or two setbacks, such as requiring Spotify premium in order to use it, it offers the perfect solution to my problem: Allowing me to run a Spotify client on a Raspberry Pi connected to my sound system which I can connect to using Spotify’s Listening on function as long as I am connected to the same network.

Depending on the operating system you are using on your Raspberry Pi, the installation is either very simple or requires a bit of tinkering. I tried two OSes - Raspbian 11, based on Debian Bullseye, and Ubuntu 20.04 - and summarized the setup process for you.

For Raspbian 11, installing Spotifyd is very straightforward and rather a matter of configuration than of getting spotifyd to work. First, you need to get the spotifyd precompiled binary from the project’s release page. Raspbian 11 is built on the armv7l architecture, which is 32-bit, so you want to download the release of the armvh version. After downloading, decompress the archive and move the binary to a directory of your choice.

1
2
3
daniel@raspi:~$ wget -O spotifyd-armvh.tar.gz https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armhf-full.tar.gz
daniel@raspi:~$ tar -xzvf spotifyd
daniel@raspi:~$ mv spotifyd /usr/bin

Installing spotifyd on Ubuntu 20.04, another popular distribution on Raspberry Pis due to its 64-bit architecture, requires a few more steps. The spotifyd project does not provide prebuilt binaries for Ubuntu’s aarch64 architecture, so the binary needs to be built on the Raspi first. For doing so, some additional dependencies need to be installed on the way, too.

Therefore, instead of downloading the binary, we will clone the repository using git and install all needed dependencies for building spotifyd from source ourselves.

1
2
3
daniel@raspi:~$ cd /tmp
daniel@raspi:~$ git clone https://github.com/Spotifyd/spotifyd --branch=v0.3.3
daniel@raspi:~$ sudo apt-get install build-essential pkgconf libasound2-dev libssl-dev rustc cargo

After installing needed drivers, development libraries and the needed Rust components, we can now build spotifyd ourselves - this might take quite some time, around 45 minutes on my Raspi 4b. After building the binary, we can move it to a more suitable location, just as we did on Raspbian 11.

1
2
3
daniel@raspi:~$ cd spotifyd
daniel@raspi:~$ cargo build --release
daniel@raspi:~$ sudo mv target/release/spotifyd /usr/bin/

Next step is to create a systemd configuration file which will be needed for handling the UNIX daemon. The spotifyd documentation puts this file in the directory /etc/systemd/user/ and configures the daemon to run in a user’s context - since I don’t run any applications in a personal user’s context on my Raspi, I decided to configure spotifyd to run in the system’s context, too, and put the configuration file at /etc/systemd/system/spotifyd.service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[Unit]
Description=A Spotify playing daemon
Documentation=https://github.com/Spotifyd/spotifyd
Wants=sound.target
After=sound.target
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/bin/spotifyd --no-daemon --config-path /etc/spotifyd/spotifyd.conf
Restart=always
RestartSec=12

[Install]
WantedBy=default.target

After creating the service, you can go ahead and enable it:

1
2
daniel@raspi:~$ sudo systemctl daemon-reload
daniel@raspi:~$ sudo systemctl enable spotifyd.service

Before starting spotifyd for the first time, the configuration file needs to be created at the location defined in spotify.service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[global]
username              = "username"               # username, NOT email
password              = "password"
backend               = "alsa"
device                = "default"                # List all devices via `aplay -L`
mixer                 = "PCM"                    # or alsa_linear/softvol
volume-controller     = "alsa"
device_name           = "spotify@raspi"          # Must NOT contain whitespace
bitrate               = 320                      # highest possible, alternatives are 96, 160
cache_path            = "/opt/spotifyd/cache"    # wherever you want spotifyd to cache music
volume-normalisation  = true
normalisation-pregain = -10

Lastly, start spotifyd and check the status. If everything worked as expected, you should be able to find your Raspberry Pi in the Listening on menu of your Spotify apps.

1
daniel@raspi:~$ sudo systemctl start spotifyd

All in all, setting up spotifyd works quite well without any real trouble and the resulting connectivity between your mobile phone and the Raspi client works just as fine as you might know it from your mobile phone and your notebook. Just remember - you’ll be needing a Spotify premium account and the client does not like switching connections so much, so you might want to stick to one mobile phone connecting to it on a regular basis.