Using Spotify with ncmpcpp and Mopidy in Linux

Deepjyoti Barman @deepjyoti30
Apr 16, 2020 12:00 AM UTC
Post cover

Back when I started using Linux, I really built a liking for ncmpcpp. However, that was when I mostly used to listen to songs that are stored locally on my machine. After a few months, I started using Spotify and so I had to stop using ncmpcpp because I did not have a Spotify Premium account to use with ncmpcpp.

I have a Premium account now and I was restlessly looking for a CLI based Spotify client when I found a comment mentioning ncmpcpp with mopidy-spotify and my memory hit back at once. We can use ncmpcpp with Spotify, here's how to do it properly.

Requirements

We will need to install a few apps in order to use Spotify.

The above apps can be found in the respective app stores. In my case I used AUR and PyPI to get them. Check installation instructions in the GitHub pages.

ncmpcpp

ncmpcpp is a frontend for MPD (Music Player Daemon) and it's a fork of ncmpc but it's better in many ways than ncmpc and offers a lot of extra features.

mopidy

Mopidy is an extensible music server which is written in Python but the good thing is it also supports MPD clients, thus we can use it with ncmpcpp

mopidy-spotify

mopidy-spotify is just an extension of mopidy which lets us use Spotify with Mopidy given we have a Premium Spotify account. A Premium account is required because Spotify puts restrictions in their API for free accounts. Thus free accounts cannot get proper access of all the Spotify endpoints.

Getting Started

We need to use all the above apps so install them before going along with this article.

Here's how we will get it to work. We will use Mopidy along with Mopidy Spotify to get our Spotify data and play songs etc. So we have our backend ready. Once that's done, we will configure ncmpcpp to work with mopidy in order to make our frontend work as well. So we have our frontend ready as well.

How Spotify CLI works with Mopidy

The above image sums it up nicely. How the whole thing is going to work.

Once the installation is done, we will have to configure ncmpcpp, and mopidy. Each one of them has a config file that will be used while the app runs. This config files are important in order to make sure everthing works. Please follow the steps properly.

Setting up Mopidy

The mopidy config is stored in the ~/.config/mopidy directory. The name of the config is mopidy.conf. Here's the mopidy.conf

[core]
restore_state = true

[mpd]
enabled = true
hostname = 127.0.0.1
port = 6600

[spotify]
enabled = true
username = your_username
password = your_pw
client_id = your_client_id
client_secret = your_client_secret
bitrate = 320

The client_id and secret can be fetched from here. Put the values you get in the config file above.

Put your Spotify username and password above as well.

The MPD section takes the values hostname and port. The default value of hostname is 127.0.0.1 and port is 6600

Note that we need to keep the hostname and port same through all the files in order for them to work together. It's simple how they'll work. Mopidy will serve the data in the hostname and port we specify and ncmpcpp will read the data from them, thus we need to keep em same throughout all the config files.

enabled option tells mopidy if the current extension is enabled or not. The default values are false. So we need to change them in order to tell mopidy that we want to use mopidy-spotify.

Setting up ncmpcpp

The ncmpcpp config is stored in the ~/.config/ncmpcpp directory. The name of the file config. Only three values would suffice to get it up and running.

mpd_host = "127.0.0.1"
mpd_port = 6600
mpd_music_dir = ~/Music

As I mentioned earlier, the host and port need to be the same in this config as well. The mpd_music_dir is required by ncmpcpp.

A sample configuration file is provided by ncmpcpp. It can be copied from /usr/share/doc/ncmpcpp/config to the config directory. This file would have a proper explanation of all the possible values along with other information. Check the wiki for more details.

Using ncmpcpp

Once the above configurations are done, we are good to start using Spotify from the CLI. Just run mopidy in order to start the daemon/backend.

ncmpcpp playlist window

Once mopidy is running, open ncmpcpp in another window or push mopidy to background and open in the same window. You might see an empty window, this would probably because ncmpcpp loads up by default the now playing screen and since we haven't played anything yet, it would be empty.

Click the number 2 and you'll see all your playlists, play some playlists and you should be good to go. Check the cheatsheet for more.

I've using this setup for about a week now and so far I haven't faced any issues. Feel free to DM me with your querries.

Discussion