How to download and convert Youtube videos to mp3, in Ubuntu Linux
Updated: 21 April 2026, Package names and formatting have been updated for modern Ubuntu. Original publication date: 25 March 2017.
Why This Guide?
This post walks through downloading and converting YouTube videos to MP3 on Ubuntu Linux. A quick disclaimer: this is not an endorsement of copyright infringement. I use these tools to download videos for offline listening during my long London Underground commute, where there's no GSM or Wi-Fi signal.
Requirements
You'll need three packages installed:
youtube-dl, "A small command-line program to download videos from YouTube.com and a few more sites."ffmpeg, "A complete, cross-platform solution to record, convert and stream audio and video." Required byyoutube-dlfor audio extraction.libavcodec-extra, The Libav codec library, required byyoutube-dlfor converting files to MP3.
Installation
Run the following commands:
sudo apt-get install youtube-dl
sudo apt-get install ffmpeg
sudo apt-get install libavcodec-extra
The Script
Save the following script as fetch.sh. It reads URLs from a text file called tracks.lst and downloads each one:
#!/bin/bash
tracks=()
while read line
do
echo -e "Downloading '$line'..."
youtube-dl -q --console-title --extract-audio --audio-format=mp3 -o "%(title)s.%(ext)s" $line
done < tracks.lst
Usage
1. Create a file called tracks.lst and add one YouTube URL per line.
2. Make the script executable: chmod +x fetch.sh
3. Run it: ./fetch.sh
The resulting [Youtube Title].mp3 files will be saved in the same folder as the script.
Tips
If you'd rather keep the original video format instead of converting to MP3, simply remove the --extract-audio --audio-format=mp3 flags from the youtube-dl command in the script.