How to Install and Use Youtube-dl on Linux

Youtube-dl is a fantastic tool for downloading videos. I use it nearly everyday to download my favourite channel videos, playlists etc from youtube.com.

Hint: If YouTube is blocked in your country or area, you can set up your own VPN server or proxy server to unblock it.

Install Youtube-dl on Linux from Python

You can install youtube-dl on Linux from python package repository. First you need to install the python package manager – pip.

Debian/Ubuntu:     sudo apt install python-pip

CentOS:            sudo yum install python-pip

Fedora:            sudo dnf install python-pip

Arch Linux:        sudo pacman install python-pip

OpenSUSE:          sudo zypper install python-pip

Then install youtube-dl with pip:

sudo pip install youtube-dl

Now the installation is completed.

Do Not Install it From Software Repository

If you are using Linux, then you can also install youtube-dl from your distribution’s repository. For example to install it on Debian/Ubuntu systems, just issue the following command:

sudo apt-get install youtube-dl

But I recommend the first method because the version from the repository is often not the latest. You might get some error when using youtube-dl if you choose to install it from repository.

Update or Remove YouTube-dl

Sometimes youtube-dl can’t download videos. For example, it can’t find any videos in a playlist, you can try upgrading to the latest stable version. Use the following command to update:

sudo pip install --upgrade youtube-dl

Use this command to remove it from your OS:

sudo pip uninstall youtube-dl

Unable to Extract Title Error

If you encounter the “unable to extract title error”, then you need to reinstall or upgrade youtube-dl.

How to Download Videos From Youtube

To download a single video, just type youtube-dl in the terminal followed by the video URL. For instance:

youtube-dl https://www.youtube.com/watch?v=l7Be_6NEPQE

To download a playlist, just append the playlist URL to youtube-dl, like this:

youtube-dl https://www.youtube.com/playlist?list=PLCGm32c1bz2qw8OrqqIgn-27-DapsiSJS

To download all videos from a user, type the following in the terminal:

youtube-dl -ciw ytuser:username

You can find the username when you go to the user’s channel page and then click the videos button.

youtube-user

Now you can see the userid in the address bar of your browser.

youtube-userid

Also, you can append the user’s YouTube URL to download all of the user’s videos.

youtube-dl -ciw https://www.youtube.com/user/username

If you aren’t a native English speaker, you might want to download and embed subtitles in the videos.

youtube-dl -ciw --write-auto-sub --embed-subs https://www.youtube.com/user/username

You need to install ffmpeg for this to work.

sudo apt install ffmpeg

Download an Entire Channel

Just append the channel’s URL.

youtube-dl -ciw https://www.youtube.com/channle/channelname

Download behind a Proxy

You can use the --proxy option to specify HTTP/HTTPS/SOCKS proxy like below.

youtube-dl --proxy socks5://127.0.0.1:1080/ https://www.youtube.com/videourl

Download Audio Books From YouTube

If you are going to download audiobooks from YouTube, but you only want the audio, not the video, so you can save disk space. Then you can use the -x option to convert video files to audio-only files.

youtube-dl -ciwx https://www.youtube.com/user/GreatestAudioBooks

You need to install ffmpeg for this to work.

sudo apt install ffmpeg

You can also specify the audio format and audio quality. By default, the video files can be converted to .opus files, which your software might not be able to play. You can tell YouTube-DL to convert video files to mp3 format and use the highest quality (0).

youtube-dl -ciwx --audio-format mp3 --audio-quality 0 https://www.youtube.com/user/GreatestAudioBooks

Playing Video/Audio Files in the iOS VLC Player

The VLC player can play all kinds of audio/video file without problems, including .opus, .mp3, .aac, .mp4, .mkv files. However, I found that if the file name includes emoji characters, then the VLC player on iOS may not be able to recognize the file.

VLC player on iOS can correctly display the embedded subtitles, just like how it’s displayed on YouTube. However, the VLC player on my Linux desktop also displays the timestamps in the subtitle, which isn’t useful at all. The MPV media player on Linux desktop can display embedded subtitles correctly.

sudo apt install mpv

Extract Audio From Video

You can extract audio from a video file using FFmpeg.

sudo apt install ffmpeg

Check what audio stream format is used.

ffprobe input-video.mkv

For example, my video file uses the opus audio stream, so I use the following command to extract a .opus audio file.

ffmpeg -i input-video.mkv -vn -acodec copy output-audio.opus

How to Play Audio Files From Command Line

You can use mplayer.

sudo apt install mplayer

Then run it like this:

mplayer -nogui -loop 1 /path/to/the/audio-file.mp3

where:

  • -nogui: disable graphical user interface.
  • -loop 1: play it only once.

Troubleshooting

Some folks might encounter the following error while trying to download YouTube videos.

urlopen error [Errno 99] Cannot assign requested address

One of the reasons for this error is that your computer has two network interfaces (wired and wireless). You need to disable one of them, so you will have only one active network interface.

Youtube-DL Fork

Youtube-DL hasn’t been updated for a long time. If you use it, your download speed is capped at 60kbps. To get better speed, use the youtube-dl fork, yt-dlp.

sudo pip3 install yt-dlp

The command-line syntax is the same as the original youtub-dl.

Wrapping Up

I hope this tutorial helped you install and use YouTube-DL to download videos from YouTube.

Recommended Reading

Rate this tutorial
[Total: 7 Average: 5]

3 Responses to “How to Install and Use Youtube-dl on Linux

  • Looks like the pip command is incorrect; should be `pip install youtube_dl` and NOT `pip install youtube-dl`.

  • sudo pip install youtube-dl not sudo pip install youtue-dl beginning

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here