python3 -m venv ~/py_envs
source ~/py_envs/bin/activate
python3 -m pip install yt-dlp
python3 -m pip install --upgrade yt-dlp
Before executing the 2nd command above, you can try executing first the 3rd command to see if the prompt returns a newer version suggestion than python3.13-venv
yt-dlp is a better option with more functionalities. It's a fork from the youtube-dl
Note: After you leave the virtual environment, if you need to use yt-dlp again, you have to first execute the 3rd and 4th commands above to set the virtual environment.
Since this is installed in a virtual environment, you won't get notices of new versions available, so you have to run the 6th command above to check if there's an update ready to be installed.
Sometimes the yt-dlp command may return an error when trying to download a video, in most cases that's fixed installing the new version.
Thre's a lot of information and help in the manual:
man yt-dlp
If you want to check first all the available formats of a YouTube video you need the URL of it along with the flag "-F" after the yt-dlp command:
yt-dlp -F https://www.youtube.com/watch?v=wh-07BzfgYY
And you will have an output like the following:
There will be a list of all the formats with IDs, formats, resolutions, video and audio codecs used, file sizes, extensions, FPS, if video or audio only, etc.
The command to download the format of your preference, by example if ID 18 do the following now using the lower case flag "-f" :
yt-dlp -f 18 https://www.youtube.com/watch?v=wh-07BzfgYY
That format with ID 18 includes video and audio. But you can choose to merge video only plus audio only formats, in this case you need to have installed FFMPEG for the merge, if not installed yet proceed with:
sudo apt install ffmpeg
Then you can download and merge, by example ID 396 (video only) plus ID 139 (audio only) with the following command:
yt-dlp -f 396+139 https://www.youtube.com/watch?v=wh-07BzfgYY
Note: It's possible to download videos not only from YouTube.
-----------------------
The following produce an error and now it has been deprecated. It's better to use a virtual environment as shown here above:
sudo apt install python3-pip
The following method of installation is even older and deprecated, it's here just as a reference: