youtube-dl – How to install the latest version
When your distro’s version is out of date

Table of Contents

Dear user: This article has moved to a new location. Please update your bookmarks.

1. Problem space

You need to download a video from YouTube for archival purposes – e.g. you are giving a presentation in a location without Internet access. After some research, you find a nice command line tool called youtube-dl. (Good choice!)

However, your distribution packages an old version of youtube-dl, and the old version errors out with a bunch of parser errors.

The problem is that youtube-dl and the YouTube website are in a constant cat versus mouse battle. YouTube occasionally updates their website to foil tools like youtube-dl; a few days later youtube-dl releases an update to work with YouTube again.

So, you need to upgrade youtube-dl.

2. Kyle’s DIY way: pip and venv

If your distribution doesn’t update youtube-dl, then go further upstream directly to the source: the youtube-dl project publishes official releases on PyPI, the official Python Package Index.

First, make sure that the Python venv module is installed. This module creates a sandbox environment (i.e. “virtual environment”) for Python. For example: on Ubuntu and Linux Mint, the package name is python3-venv.

$ sudo apt install python3-venv

Then, fetch and install the latest youtube-dl inside a virtual environment.

$ cd .local/
$ python3 -m venv youtube-dl
$ . youtube-dl/bin/activate
(youtube-dl) $ pip install youtube-dl
(youtube-dl) $ which youtube-dl
/home/kyle/.local/youtube-dl/bin/youtube-dl
(youtube-dl) $ youtube-dl --version
2021.02.10

The first command selects a directory that applies to further commands. The second command creates the virtual environment inside a directory named youtube-dl. The third command sets environment variables to activate the virtual environment youtube-dl. The fourth command fetches and installs the latest version of youtube-dl from PyPI into the virtual environment.

The beauty of this method is that updating youtube-dl is clean and easy. Simply remove the youtube-dl directory and repeat the above steps to create the virtual environment again. No system packages and no system libraries (under /usr) are touched.

The downside of this method is that the virtual environment must be active in order to use youtube-dl. (To deactivate the virtual environment, simply run deactivate.) If the virtual environment is not active, then the older youtube-dl from /usr will run.

(youtube-dl) $ deactivate
$ which youtube-dl
/usr/bin/youtube-dl

So, let’s create a wrapper script. Insert a shell script called youtube-dl anywhere into your $PATH. ~/bin is usually a good place. If you want a systemwide installation, then insert the wrapper script into /usr/local/bin.

#!/bin/bash
. /home/kyle/.local/youtube-dl/bin/activate
exec youtube-dl "$@"

Make it executable with chmod +x, and try it out.

$ chmod +x ~/bin/youtube-dl
$ which youtube-dl
/home/kyle/bin/youtube-dl
$ youtube-dl --version
2021.02.10

Have fun!

3. Legal disclaimer

YouTube does not approve of tools like youtube-dl because they can be used to copy copyrighted material. Use wisely, and when in doubt make sure you have the author’s permission before copying or distributing a video. I am not a lawyer.

4. A positive note about Funtoo

Users of Funtoo Linux may notice that the net-misc/youtube-dl package is updated within a day of upstream youtube-dl releases. This is because the build recipes are automatically generated from PyPI indexes using a tool called autogen. autogen automates a lot of routine day-to-day work for the Funtoo Linux project.