How to Create Your Own Spotify Enabled Music Server on the Raspberry Pi with Mopidy

by | May 25, 2018 | Audiophile, Technology

Having a nice home music server is definitely a bonus for anyone who has a music collection. This guide will show you how to set up a home music player with Spotify on the Raspberry Pi using Mopidy. Mopidy is a Music Server which also has many other extensions. This guide will help you set up your local library and Spotify, but you can also add Google Play Music, Sound Cloud and many others as well!

First lets get a copy of Raspian Lite.  At the time of writing this, we are using Raspian Stretch lite.  You can download a copy of Stretch here.

Once downloaded, go through the initial setup with

sudo raspi-config

and set your default password, locale, etc.  Once your initial config is completed, lets get started configuring.

Lets update our Raspberry PI

Run each of these commands one by one:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get rpi-update
sudo reboot

These commands will update our Raspberry Pi to the latest software currently available.  Each one of these commands will take a little time to complete.

Next, lets install a nice light desktop environment

For simplicity, lets install a desktop environment.  XFCE is lightweight, includes Pulse-Audio and will simplify some of our automation tasks.  If you use a DAC or multiple audio outputs, Pulse-Audio will make things much easier.

sudo apt-get update
sudo apt-get install xfce4

Lets also install xscreensaver, so we can ensure no processor heavy screen saver will come up while idle.

sudo apt-get install xscreensaver

And finally, we want to have the Raspberry Pi automatically log into the desktop environment.

sudo raspi-config

Now head to:

3 Boot Options
B1 Desktop / CLI
B4 Desktop Autologin

This will log us directly into XFCE on reboot.  Now go to <Finish> and select Yes when prompted to Reboot.

Lets configure XFCE

Now we should have rebooted into XFCE.  You will be prompted for a Panel layout.  Select Default.  Next, lets configure our screen saver.  Go to:

Applications (top left)
Settings
Screensaver
Under Mode, select Blank Screen Only

Lets install Mopidy

Now, lets install Mopidy.  We can do this by opening a terminal window or by connecting via SSH.  To open a terminal window, go to:

Applications (top left)
System
XTerm

Now lets add Mopidy’s GPG Key, Repository, and install.

wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/stretch.list
sudo apt-get update
sudo apt-get install mopidy
In addition, lets add Spotify support, and sqlite support as well for Album covers, etc.
sudo apt-get install mopidy-spotify
sudo apt-get install mopidy-local-sqlite

Next lets install our web interface

sudo apt-get install python-pip
pip install Mopidy-Iris

Now if there were no errors we should have Mopidy Installed.

Lets Configure Mopidy

First we need to create our Mopidy configuration file.  We can do this by starting Mopidy for the first time.

mopidy

This will launch Mopidy and create a default configuration file.  Now to stop Mopidy Press CTRL + C

Now lets edit our configuration file.

sudo nano ~/.config/mopidy/mopidy.conf

Scroll down and make the following changes in the configuration file. The sections to modify will be in the square brackets, the modification will show the default and what you need to change the option to.

[mpd]
#enabled = true TO enabled = false

[http]
#hostname = 172.0.0.1 TO hostname = (your current ip address)

[stream]
#enabled = true TO enabled = false

[local]
#library = json TO library = sqlite

[iris]
#country = NZ TO country = (Your two digit country code)
#locale = en_NZ TO locale = (Your two digit language preference)_(Your two digit country code)

Now for Spotify we need to log into our account and get a client_id and client_secret code for authentication to our account.

Head on over to https://www.mopidy.com/authenticate/#spotify to get the authorization information.  As stated earlier, this does require you to have a Spotify Premium account.

[spotify]
#username = TO username = (Your username)
#password = TO password = (Your password)
#client_id = TO client_id = (Your client id)
#client_secret = TO client_secret = (Your client secret)
#bitrate = TO bitrate = 320
#toplist_countries = TO toplist_countries = (Two digit country code of countries you would like seperated by comma’s)

Optionally, if you would also like to add your local or network music files change the following

[file]
#media_dirs = TO media_dirs = (Location of your music files)

This location can be on your Raspberry Pi, or a network location if it has been mounted to your system.

This should be enough to get us started.  Press CTRL + X, then press Y to confirm save, and Enter to save the file.

Lets set Mopidy to AutoStart

Back on our desktop, lets add Mopidy to autostart with our Raspberry Pi.  Do this by going to:

Applications
Settings
Session and Startup
Go to the Application Autostart Tab
Click the +Add button
Name: Mopidy
Description: Mopidy Music Server

Now click the browse button to browse for the Mopidy program.  Next click on:

File System (Left)
usr
bin
mopidy

Now click Okay, then Close.  Mopidy will now autostart with XFCE under the pi user.  Lets give a reboot and test to see if things are working.

sudo reboot

Lets Connect to our new Mopidy server

Next lets test to make sure we are up and running.  Open your web browser on phone/tablet/desktop and enter in the following URL:

http://(raspberrypi-IP-Address):6680

If all went according to plan you should be greeted by a nice looking interface. If your on your smart phone, you can add this to your home screen and have a nice new icon for your music server!