Why does Bash give "No such file or directory" for a program that's in my PATH?

by Borea Deitz   Last Updated July 12, 2019 14:01 PM - source

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.

Tags : linux bash path


Answers 2


The problem seems to be an existing location entry from the previous installation, which apt-get remove and apt-get purge did not remove or purge.

hash docker-compose removes the entry. More information can be found here:

https://github.com/docker/compose/issues/3371#issuecomment-381162525 https://unix.stackexchange.com/questions/82991/bash-is-not-finding-a-program-even-though-its-on-my-path

Borea Deitz
Borea Deitz
July 12, 2019 13:41 PM

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
harrymc
harrymc
July 12, 2019 14:00 PM

Related Questions



How to remove duplicate dirs from $PATH?

Updated June 22, 2018 23:01 PM


A more comfortable way to edit a long $PATH?

Updated November 16, 2015 07:00 AM