Linux – Why does Bash give “No such file or directory” for a program that’s in the PATH

bashlinuxpath

I installed docker-compose on my Ubuntu 16.04 machine following the instructions at https://docs.docker.com/compose/install/ to use curl to download the binary directly to /usr/local/bin/:

$ ls -lh /usr/local/bin/docker-compose 
-rwxr-xr-x 1 root root 16M Jul 11 15:55 /usr/local/bin/docker-compose

/usr/local/bin/ is in my PATH:

$ echo $PATH
/home/me/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Note that it comes before /usr/bin, in fact.

When I try to check its version, I get what seems to be a PATH-related error:

$ docker-compose --version
bash: /usr/bin/docker-compose: No such file or directory

I get the correct behavior either by specifying the full path of the executable,

$ /usr/local/bin/docker-compose --version

docker-compose version 1.24.0, build 0aa59064

or by using sudo,

$ sudo docker-compose --version
docker-compose version 1.24.0, build 0aa59064

Why can't I run $ docker-compose --version? Why does Bash complain about /usr/bin in particular, when it ought to find the executable in /usr/local/bin?

It might be relevant that I previously installed docker-compose via apt, but I have removed and purged this package.

Best Answer

You have installed docker-compose once in one place. Then you deleted it and installed it in another location.

In that way, you have run into an optimization of bash for not having to search $PATH every time you type a command, by caching the results in memory. The path hash is a hash-table, maintained by bash, that contains the locations on disk where the shell should look for executable programs when a command is run. The hash table gets cleared on events that obviously invalidate the results (such as modifying $PATH), or by using the inbuilt hash command.

When you executed docker-compose again, bash just tried to get it from where it found it the last time, only that it wasn't there any more, so you got that error message.

To invalidate the hash for docker-compose, run the command:

 hash docker-compose
Related Question