How to Download YouTube Videos in Linux Using YT-DLP

YouTube is an online video-sharing platform that contains a vast repository of videos covering a wide range of topics such as music videos, educational content, vlogs, tutorials, movie trailers, gaming videos, and live streams.

While the platform offers the convenience of streaming, there are certain scenarios where you might want to download these videos for offline viewing.

In the Linux command-line environment, yt-dlp is a powerful and versatile command-line program that allows users to download 1080p or 4K YouTube videos from YouTube.com and other video sites.

In this article, we will guide you through the process of installing and using yt-dlp to download YouTube videos on a Linux command line.

What is yt-dlp?

yt-dlp is a Python-based command line and fork of the well-known youtube-dl program that is created for downloading videos from popular video streaming platforms such as YouTube, Facebook, Twitter, Reddit, Imgur, Dailymotion, Hotstar, and various other video sites.

It is developed in Python and requires only Python 3.8+ and it is platform-specific, and designed to function seamlessly on Unix, Windows, or macOS environments.

It also supports many options, allowing users to customize their downloads based on preferences. One notable feature is downloading videos in different resolutions, including high-quality formats like 1080p or 4K.

Additionally, youtube-dl supports downloading entire playlists, extracting only the audio from videos, bypassing YouTube geo-restrictions, pausing and resuming video downloads, and splitting videos into multiple files.

How to Install yt-dlp on Linux

yt-dlp is the most widely used command-line tool that is easily available to install from the default repositories in most Linux distributions, if not all.

To install yt-dlp on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install yt-dlp         [On Debian, Ubuntu and Mint]
sudo yum install yt-dlp         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/yt-dlp  [On Gentoo Linux]
sudo apk add yt-dlp             [On Alpine Linux]
sudo pacman -S yt-dlp           [On Arch Linux]
sudo zypper install yt-dlp      [On OpenSUSE]    

To install the most recent version of yt-dlp, you can use the following curl command.

sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp

If you do not have curl installed, you can alternatively use the following wget command.

sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp

Once installed, you can use the following command to update it to the latest version.

yt-dlp -U

Downloading a YouTube Video on Linux

To download a YouTube video, use the following command, where <video_url> is the URL of the YouTube video that you want to download.

yt-dlp <video_url>

For example, to download a YouTube video replace <video_url> with the actual URL of the YouTube video you intend to download.

yt-dlp https://www.youtube.com/watch?v=60ItHLz5WEA
Download YouTube Video in Linux
Download YouTube Video on Linux

You can also download multiple videos at once by specifying a list of URLs separated by spaces.

yt-dlp  <video_url_1> <video_url_2> ...

Downloading a YouTube Playlist on Linux

To download a YouTube playlist, simply run the following command to download the entire playlist. Make sure to replace the <playlist_url> of the YouTube playlist URL that you want to download.

yt-dlp -i --yes-playlist 

Downloading YouTube Videos in Different Formats

yt-dlp provides several options for downloading YouTube videos in various formats, allowing you to tailor the process to your specific needs.

To download a specific format, you first need to list the available format codes for a video by running.

yt-dlp -F https://www.youtube.com/watch?v=60ItHLz5WEA
List YouTube Video Formats
List YouTube Video Formats

To download high-quality YouTube video and audio, use the following command.

yt-dlp -f best https://www.youtube.com/watch?v=60ItHLz5WEA

To download YouTube videos in 1080 resolution, use the following command.

yt-dlp -f 'bestvideo[height=1080]+bestaudio/best[height=1080]' <video_url>

To download a YouTube video in 4K resolution, use the following command.

yt-dlp -f 'bestvideo[height=2160]+bestaudio/best[height=2160]' <video_url>

To download audio only (mp3 format) from a Youtube video, use the following command.

yt-dlp -x --audio-format mp3 <video_url>

To download a YouTube video in a specific format, use the following command.

yt-dlp -f mp4 <video_url>

Download Subtitles of YouTube Videos

yt-dlp provides the easiest way to download subtitles of videos directly alongside the video itself.

To list all available subtitles for a specific YouTube video, use the following command, which will show a list of available subtitles along with their language codes.

yt-dlp --list-subs  https://www.youtube.com/watch?v=60ItHLz5WEA 
List YouTube Video Subtitles
List YouTube Video Subtitles

To download subtitles along with a video, use the -S option.

yt-dlp -S <video_url>

To download only specific subtitles, you can specify the language code after the -S option.

yt-dlp -S en,es <video_url>

If you only want to download subtitles without the video, you can use the –write-sub option with the -S option.

yt-dlp -S --write-sub <video_url>
Conclusion

yt-dlp is a versatile and efficient command-line tool for downloading YouTube videos in the Linux environment. It comes with a broad range of customization options and supports various video and audio formats.

By following the steps outlined in this article, you can harness the power of yt-dlp to easily download and enjoy your favorite YouTube content offline on your Linux system.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 Comments

Leave a Reply
  1. I downloaded and tried yt-dlp and encountered a problem. I can’t find the file, where does it save the Youtube file and how do I choose where to save file?

    Reply
  2. yt-dlp used to work for me, but for the last several months, if I try to download audio according to this command:

    yt-dlp -x --audio-format mp3 https://www.youtube.com/watch?v=_onagRhKN5E
    

    It fails with this output:

    [youtube] Extracting URL: https://www.youtube.com/watch?v=_onagRhKN5E
    [youtube] _onagRhKN5E: Downloading webpage
    [youtube] _onagRhKN5E: Downloading android player API JSON
    [info] _onagRhKN5E: Downloading 1 format(s): 251
    [dashsegments] Total fragments: 1
    [download] Destination: The Amboy Dukes - Journey to the Center of the Mind (Official Audio) [_onagRhKN5E].webm
    [download] Got error: HTTP Error 403: Forbidden. Retrying fragment 1 (1/10)...
    [download] Got error: HTTP Error 403: Forbidden. Retrying fragment 1 (2/10)...
                                      
    [download] Got error: HTTP Error 403: Forbidden. Retrying fragment 1 (10/10)...
    [download] Got error: HTTP Error 403: Forbidden. Giving up after 10 retries
    ERROR: fragment 1 not found, unable to continue
    
    [1]+  Done                    echo https://www.youtube.com/watch?v=_onagRhKN5E
    

    This is with Debian 12. I’m almost certain it happened the same way with Debian 11.

    Reply
  3. I am always amazed at learning new things as far as Linux and Linux ecosystems are concerned. On this, I had yet to learn about its existence.

    Sometimes I tried to create my own which is also Python-based based utilizes y2mate’s REST api to download YouTube videos.

    Check it out here https://github.com/Simatwa/y2mate-api

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.