How to Play Music from the Linux Command Line

Playing music from the command line isn’t the right option for everyone. MPD is a command-line option for music playback that requires manual configuration. Some people will love that. It’ll drive others insane. It all depends on how you prefer to use your Linux PC.

MPD and NCMPCPP are a lightweight super combo for playing music with loads of fine-grained control direct from the command line (NCMPCPP is ncurses-based, so technically a text-based user interface).

MPD is also a service that can be accessed by a multitude of other programs that you can use to do some cool customization. It’s also accessible over the network, so you can control it with a compatible client on your phone. In this tutorial we’ll look at how to play music from the Linux terminal with MPD and NCMPCPP.

Both programs are available in the repositories of most distributions. If you’re on Ubuntu, they’re definitely there. Install them with apt.

sudo apt install mpd ncmpcpp

The default MPD configuration can be found at “/etc/mpd.conf.” You can create per-user configurations, but assuming this is a computer you alone own, you don’t really need to. Just edit the file in your favorite editor withsudo.

sudo vim /etc/mpd.conf

When you first open the configuration file, you might be intimidated. There are a ton of options there and even more documentation. That’s actually a good thing. You can use the file itself as a reference. Even still, there actually aren’t that many lines that you need to edit to get the MPD service working the way you need it to.

Directories

First, look for the music_directory option. That’s the directory where MPD will look for your music library. Set the option equal to the directory where your music is.

music_directory "/home/user/Music"

Next, do the same thing with the playlist_directory. This is where MPD (and NCMPCPP) will store and look for playlists.

playlist_directory "/home/user/Music/playlists"

Options

There are a few options to change in the next section. First, find the user and group options. The defaults here are actually good. If you want to change them, you can.

user    "mpd"
#group    "nogroup"

The next bit deals with how you’re going to connect to MPD. You want to uncomment the bind_to_address line that’s set to a Unix socket and the port line. It should look like the example below.

bind_to_address    "/run/mpd/socket"
port    "6600"

Depending on the client, MPD might just start playing the second your computer starts up. If you don’t want that, uncomment and set this line:

restore_paused "yes"

You’re also probably going to want MPD to automatically detect and add new music that’s added to the music directory. To do so, uncomment the line here:

auto_update    "yes"

Output

MPD Configuration

This next part handles where MPD will output your music. Usually, you’re going to want to use either ALSA or PulseAudio to play everything. If you want to take advantage of a visualizer, you’ll need to set up a FIFO interface too.

ALSA

If you’re using ALSA, you don’t need to do anything. It’s configured by default. You can change any of the #optional values if you have something specific in mind.

audio_output {
    type        "alsa"
    name        "My ALSA Device"
#   device      "hw:0,0"    # optional
#   mixer_type      "hardware"      # optional
#   mixer_device    "default"   # optional
#   mixer_control   "PCM"       # optional
#   mixer_index "0"     # optional
}

PulseAudio

Pulse is actually configured; it’s just commented out. Head down to the Pulse section and comment out the lines you need. There are a lot of additional options for Pulse, but they aren’t strictly necessary just to play music.

audio_output {
    type        "pulse"
    name        "My Pulse Output"
#   server      "remote_server"     # optional
#   sink        "remote_server_sink"    # optional
}

FIFO

If you’re interested in using a visualizer (NCMPCPP has one built in.), you need to set up FIFO. There isn’t any pre-configured FIFO output in the default configuration. You can make your own, though. Base it off of this:

audio_output {
    type                    "fifo"
    name                    "fifo_mpd"
    path                    "/tmp/mpd.fifo"
    format                  "44100:16:2"
}

That’s it. You can look around some more, but now you have everything that you need.

MPD needs to be part of the audio user group. Add it.

sudo gpasswd -a mpd audio

With MPD out of the way, you can turn your attention to NCMPCPP. Don’t worry, there isn’t nearly as much to do.

Begin by copying the default NCMPCPP configuration.

mkdir ~/.ncmpcpp
sudo gunzip /usr/share/doc/ncmpcpp/config.gz
sudo cp /usr/share/doc/ncmpcpp/config /home/user/.ncmpcpp/config
sudo chown -R user:user /home/user/.ncmpcpp/config

Connect To MPD

NCMPCPP Configuration

You need to connect NCMPCPP to MPD. The lines concerning that connection are commented out. Uncomment them.

mpd_host = localhost
mpd_port = 6600
mpd_connection_timeout = 5

Point NCMPCPP at your music directory. too.

mpd_music_dir = /home/user/Music
mpd_crossfade_time = 2

Visualizer

First, set the pat to the “.fifo” file that you configured for MPD.

visualizer_fifo_path = /tmp/mpd.fifo

Also, set the name of the visualizer to match what you set for FIFO in MPD

visualizer_output_name = fifo_mpd

Next, take care of some options to make sure that your visualizer stays in sync and looks good.

visualizer_in_stereo = yes
# You might want to play with increasing this value
visualizer_sample_multiplier = 1
visualizer_sync_interval = 30

This last set of options is purely stylistic. They determine how your visualizer will look. Uncomment the lines and take a look at the available options. Pick what you like best.

## Available values: spectrum, wave, wave_filled, ellipse.
##
visualizer_type = wave
#
visualizer_look = ●▮
#
#visualizer_color = blue, cyan, green, yellow, magenta, red
#
## Alternative subset of 256 colors for terminals that support it.
##
visualizer_color = 41, 83, 119, 155, 185, 215, 209, 203, 197, 161

NCMPCPP Playlist

Open a terminal and type in ncmpcpp. The player will open to a blank playlist. NCMPCPP has different panes. Each pane displays something different. They’re all mapped to the number keys, so the playlist you see is on pane 1 and is mapped to the 1 key.

The rest of the panes are as follows:

  • File Browser
  • Search
  • Music Library
  • Playlist Editor/Management
  • Tag Editor (be careful)
  • Outputs
  • Visualizer

Navigate around with the arrow keys. The Library pane lets you move deeper from “Artist” to “Album” and into individual songs with the left and right arrows.

Common Hotkeys

There are way too many hotkeys and controls to cover here. If you want to read them all, this is an excellent resource.

Here are some of the most basic ones, though.

  • Space – Add to playlist
  • Enter – Play
  • > – Next Song
  • < – Previous Song
  • s – Stop
  • p – Pause
  • o – Move to current song
  • z – Toggle Random (shuffle)
  • x – Toggle Crossfade
  • l – Display lyrics
  • S – Save laylist

Take a look around and experiment. You can’t master either of these programs in a day. As with the command line itself, the more experienced and comfortable you get, the more you’ll get out of them.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.