Dev Blog

Home Development PicoSystem Dev Server Rust Game Development Misc Pi Microcontrollers Embedded Web Dev

</div>


Last.fm & Linux with VLC

If like me, you primarily use Linux but want to scrobble music to Last.fm, you've probably hit a brick wall as there is no App for last.fm on Linux. However, I've solved it. If you follow this guide through you will also be able to scrobble music to Last.fm on Linux.

VLC
VLC

Why Last.fm?

I find tracking my music difficult. I have so many files and different genres that I decided to get back onto last.fm after over a decade of not using it. It's nice to see not much has changed and there's a few new features, like the song you're currently obsessed with. Anyways, I have faith that last.fm will track my music better, give me recommendations based on my tastes and give me the ability to see what music I listen most to.

Non-Native

Last.fm isn't native to Linux, which means there is no official application for it and getting it to work on Linux requires some tweaking. VLC is supposed to support Last.fm natively, however when I tried setting it up in VLCs preferences I received module access errors and 403 bad requests in the message log. No matter what I tried nothing worked so I figured out how to use MPRIS which will scrobble my music in the background and send it to Last.fm, so let's get it set up.

Before we get started, let's get an API key that we will need from Last.fm and all the packages we need to get going.

Retrieving API Key & Install

API keys can be generated at last.fm/api, sign in with your details, fill in the boxes (just write test in the app name, it's not important, neither is the description) and click Generate, it will generate an API key & API secret for you. Save this in a text file somewhere you can find it again easily, and copy/paste it without any typos. Now let's go back to our terminal.

We will be using VLC here, it's a great little media player and the only one I've found that has similar settings to AIMP in regards to EQ and Compression, I use both of these when listening to music so it's very important to me the Music player I use has those features. Let's get it installed.

sudo apt install vlc

Start up VLC, go to Preferences, click ALL Settings on the bottom left then find the CONTROL INTERFACES section and toggle the "Submission of played song" checkbox. All other settings aren't relevant, as MPRIS will handle your username, password and the URL.

Python, pip and Virtual Ennvrionments

Now, let's go back to our terminal as we will need Python and pip:

sudo apt install python3 python3-pip

If, like me you use Ubuntu 24+, pip will not work for you. This is because APT is the system package manager, not pip. You will simply have to run it in a virtual environment like so:

python3 -m venv myenv
venv
python3 virtual environment

This will create a virtual enviroment so that you can still use pip alongside APT. The venv will be stored in your /home directory (/home/username/myenv) and can be executed with: source myenv/bin/activate

Now, you should see that your terminal prefix has changed and it should say "myenv", this indicates you're now running in your virtual envrionment and can install pip packages without any errors.

Next we will install MPRIS using PIP in your VENV:

pip install lastfm-mpris2-scrobbler

Now, leave your virtual environment in the terminal by pressing CTRL+C together, this command kills a running process in the terminal quickly. You can also enter the venv again if you need to with source myenv/bin/activate but we only needed it to install MPRIS.

Configuring MPRIS using YAML

Sorted. Now let's configure MPRIS with a config YAML. Create this in your home dir in a directory named configs, it will keep all your configs nice and neat and easy to find.

cd ~
mkdir scripts && cd configs
touch config.yaml && nano config.yaml

Before we edit this config, we need to convert your last.fm password to MD5 so that it can encode your password in a format the API can use. We can do this in the terminal, to generate an MD5 of your password use this command, replacing PASSWORD with your own:

echo -n 'PASSWORD' | md5sum

Copy the output from the terminal, or save it to copy into your config file that we are going to edit in a moment. Knowing how to do this can very useful, because sometimes you might be working with databases running older versions of MySQL that use password hashing. MySQL8 doesn't use it anymore for safety concerns but it's still useful to know how to MD5 a string.

Note how I link commands with the AND ( && ) operator, this means if the first command is successful, it will execute the next command. Very useful little tip there.

I'll use nano to edit the config, as I appreciate most people can't get on with Vim. Opening the file you will see that YAML is fairly easy to work with and your script should resemble mine:

Config YAML
YAML config

Save the config as scrobble.yaml in your /configs directory that we created earlier.

Now we can try running the scrobbler, with the config we created earlier. Replace USERNAME with your own username that you use on your machine.

lastfm-mpris2-scrobbler -c /home/USERNAME/configs/scrobble

It should run, and say that it's not yet time to scrobble or that it can't locate a media player. Try loading up VLC and playing a song to see if it scrobbles, it should work perfectly and your latest listened track should be viewable on Last.fm after it scrobbles, which is usually 50% of the tracks overall playtime or 4 minutes if the track is long.

Scripting with Bash

Now, let's make this execute on startup, saving you from going into a venv on every boot and triggering the script. Let's create another directory, called scripts, where you will keep not only this script but others you may create in the future. Keeping configs and scripts in directories this way saves time and you know where they all are, plus they can be backed up easily.

In your terminal, leave your venv with CTRL+C if you're still in it, then:

cd ~
mkdir scripts && cd scripts
touch scrobble.sh

note the ~ command here after cd (change dir), the Tilde (~) will send you straight to your /home folder in Linux.

Now lets write the bash script to enable scrobbling on boot:

#!/bin/bash

source /home/sysadmin/myenv/bin/activate</p>

exec lastfm-mpris2-scrobbler -c /home/sysadmin/configs/scrobble.yaml

A lovely, little bash script there. I love working with bash scripts!

scrobble.sh
scrobble.sh

Setting as a System Service

Now with that sorted, we can set up a system service so that this bash script executes when the system boots. Don't fret, this is quite easy to do. Let's get it done:

First we need to locate our system daemon dir, it's here:

cd ~/.config/systemd/user

Inside this dir, create a file named "scrobbler.service" and open it with nano:

touch scrobbler.service && nano scrobbler.service

then, we edit this file to tell the system to launch this service on boot:

service
scrobble.service

Done! Now let's enable the service and check it works:

systemctl --user enable scrobbler.service

systemctl --user start scrobbler.service

systemctl --user status scrobbler.service

You should see output that fills the terminal, look for Loaded: loaded (/home/yourusername/.config/systemd/user/scrobbler.service), if you see it that means it's up and running! If you see errors, you have typos in your .service file. fix the typos and reload the daemon with:

systemctl --user daemon

Then try starting it again and check the status, if you've done everything correctly your music will now scrobble in the background automatically, on Linux too! Neat.

service status
service status

If you notice any errors in this tutorial, get stuck, or can't figure it out please message me on GitHub, StackOverflow or LinkedIn, my profiles can be found at the bottom of this page.

I hope you got it working and can now scrobble music on your Linux machine!

Until next time, Dru


</div>

Blog GitHub LinkedIn ORCID Stack Overflow