1

Example1:

When I install a package using a package manager such as the JavaScript package manager npm, the package immediately becomes available to run from any directory on any terminal:

$ npm install -g redis-cli

I don't even have to restart my laptop, this command is now available to me:

$ rdcli

Example 2:

As for another example, let's install a Python module using the Python package manager pip:

$ pip install rq

Now this command is available anywhere without restarting the OS:

$ rq

In contrast:

In contrast, when I add a folder to my Environment Variables' path, I have to restart the system:

C:\Program Files\smartmontools\bin

Now after restarting or doing this trick, this command is available to me:

$ smartctl.exe

Can anyone explain what trick package managers do in the background?


Extra:

One thing I noticed is that when you install a package with a package manger and you haven't restarted the system yet, it's only available to terminals, like CMD, Powershell or MinTTY, other apps don't see it yet, for example if you run this command in a terminal:

winpty rdcli

You will get this error:

winpty: error: cannot start 'rdcli': Not found in PATH

But after you restart the system, the command will work.

1 Answer 1

1

There are no miracles - the PATH variable (or its equivalents) is used in all operating systems.

If a package is instantly available after installation, it's because it installed itself into a folder that is already in the PATH. Windows example: C:\WINDOWS\system32, Linux : /usr/bin.

If the package is not immediately available, then it installed itself into its own folder and added it to the PATH.

There is no need to reboot - any new program will have the current PATH when started. If you are in the terminal, just close it and open a new terminal window. At the most, you may logout and login again, but reboot is too much.

2
  • Interesting, thank you. About the "Extra" part in my question, I would guess that maybe at the start of the OS, the files on the PATH folders are may be cached? And when a new file gets added to those folders, maybe some programs like winpty use the cache and the terminals just lookup all the files in the PATH folders? Would that explain it?
    – Shayan
    Commented Jun 11, 2022 at 12:15
  • 1
    It's even simpler than that - every program when started gets all the environment variables at that moment, and they never change. So if PATH is changed, an existing terminal will still have the old one in its local copy of PATH. Calling it "cache" is too clever for such a simple mechanism as that of every program having its own copy.
    – harrymc
    Commented Jun 11, 2022 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .