Youtube-dl: Python not found (18.04)

I have done a clean install of 18.04 LTS. I then installed youtube-dl using

sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

When I try to use youtube-dl, I get the following error message:

rudolffischer@HP8770w:~$ youtube-dl -U
/usr/bin/env: ‘python’: No such file or directory

Python 3 seems to be installed

rudolffischer@HP8770w:~$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Why?


On Ubuntu 18.04.2 LTS with youtube-dl version 2019.06.08, after creating symbolic link with following command:

$ sudo ln -s /usr/bin/python3 /usr/local/bin/python

youtube-dl worked as usual, the error "/usr/bin/env: ‘python’: No such file or directory" vanished.


You should be able to run youtube-dl with your version of python by doing:

/your/path/python3 /usr/local/bin/youtube-dl

Find out your python3 path by doing which python3


youtube-dl can be installed from the default repositories in all currently supported versions of Ubuntu with this command:

sudo apt install youtube-dl  

youtube-dl's self-update mechanism is disabled on Debian-based operating systems except for the youtube-dl snap package which is automatically updated. You can also update youtube-dl to the latest version by installing youtube-dl with pip.

sudo snap install youtube-dl # launch it with snap run youtube-dl

or

sudo apt remove youtube-dl
sudo apt install python3-pip  
python3 -m pip install youtube-dl

Another possible solution has been suggested in a comment by Kulfy, it deserves more visibility.

Starting from Ubuntu 20.04 (where the default version of python is python3) you can install the package python-is-python3:

sudo apt install python-is-python3

What the package does is to interpret automatically the shebang #!/usr/bin/env python as a python3 shebang. In this way you can call youtube-dl without prepending any command and/or without creating a specific alias in ~/.bashrc file.

There is also an equivalent package for deprecated python2, called python-is-python2.


The head of the youtube-dl script has #!/usr/bin/env python, meaning that it uses the "python" command.

Per https://www.python.org/dev/peps/pep-0394/ Distributions can choose include the python command linked to python2 or python3, not include the command at all, or allow the user/admin to configure it.

In debian-based installs, there are 3 main python packages:

  • python (sudo apt install python)
  • python2 (sudo apt install python2)
  • python3 (sudo apt install python3)

The "python" package installs python version 2 and includes the "python" command (symlink /usr/bin/python -> /usr/bin/python2).

The "python2" and "python3" packages do not provide the "python" command. This means that calling "python" from the CLI or a script will result in a "command not found" error.

If you're using these, you have to either:

  • call the script using whichever version you like (python2 /usr/local/bin/youtube-dl or python3 /usr/local/bin/youtube-dl) [Personally, I have alias youtube-dl='python3 /usr/local/bin/youtube-dl' in my .bash_aliases]
  • edit the youtube-dl script to use python2 or python3 (sudo sed -i '1s/python/python2/' /usr/local/bin/youtube-dl) or (sudo sed -i '1s/python/python3/' /usr/local/bin/youtube-dl)

In the above, I prefer using the aliasing option since you leave the file alone and don't have to edit it every time the file gets updated

It's also possible to fix it by creating a symlink for /usr/bin/python, but that is not adviseable.