Any error "unable to execute x: no such file or directory" means that you or program which you executed made an attempt to execute x (gcc in our case) but binary with this filename was not found in $PATH .

$PATH is a system environment variable which holds a list of directories where OS searches for a binary file when user wants to execute it. To check $PATH value on Linux, Mac, Windows WSL 2, use:

echo $PATH

This will show something like:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

As you see the list of directories is split by colons :. Folders which are closer to beginning have a precedence. This means if binary found in /usr/local/sbin then search will stop and even if binary with same filename exists in a other dir, always only first will be used.

So how to fix "No such file or directory" on Ubuntu

For most cases you need to install some package which holds this binary. If you are on Ubuntu or on Windows WSL 2, just run:

sudo apt install build-essential

The package build-essential is used to build programs from source code. It holds a lot of binaries dedicated for your own architecture (x86, AMD64 or ARM), and one of them during installation will be placed to /usr/bin/gcc.

By the way, to check where in the PATH some binary placed use which:

which gcc

Fix No such file or directory on Windows Cygwin

Use apt-cyg to install gcc:

apt-cyg install gcc-core