The question is basically the title. That is, how does a package manager say apt in Linux know or decide where to install files?
The question arises because you can for example do a build from a Makefile and then just run the executable from the directory where the build was performed -- that is there is no rule for doing an install. Or there could be an install rule, but how is it determined where files should be installed? Similarly, apt does a package install, but what are the rules for where files are installed?
you can for example do a build from a Makefile and then just run the executable from the directory where the build was performed
This is true for some programs, typically those that do not have any dependencies. If a program has other files that it needs, it needs to know where to find them. Such paths are "baked into" the executable in the compilation, and cannot be changed without recompiling the program.
Typically, the location of those files will be under some prefix (e.g. $PREFIX/bin
, $PREFIX/share/...
, $PREFIX/lib/...
), and the program will be configured to use a prefix by the configure
script, which creates the Makefile
. The default prefix is usually the root directory or /usr
, which is why most executables are in /bin
or /usr/bin
. You can set a different prefix by using ./configure --prefix=$NEW_PREFIX
.
If I am not root, I install most of my programs in $HOME/.local
, so I get my executables in /home/amadan/.local/bin/...
. The subsequent make
compiles the program in the current directory, and then make install
puts everything into the configured locations.
For example, ffmpeg
has a bunch of dependencies; for example, one of them is lib/libavcodec.a
. If I compile it as a user (using ./configure --prefix="$HOME/.local"
), I end up with /home/amadan/.local/lib/libavcodec.a
after make install
has done its thing (and also, obviously, /home/amadan/.local/bin/ffmpeg
). If I try to run ffmpeg
in its build location, without make install
, it will not work, as it will look in wrong places for its supporting files.
That is, how does a package manager say apt in Linux know or decide where to install files?
The package manager itself does not. The locations to put the files are defined inside the package. The exact mechanism depends on the package manager. For example, apt
mainly uses .deb
files, which contain (among other files) a data archive. The data archive (in TAR format, possibly compressed) will contain files with full path, so that if you extract the archive in the root directory, the files will be placed where they belong.
For example, wget_1.21-1+deb11u1_arm64.deb
contains data.tar.xz
, which contains:
./
./etc/
./etc/wgetrc
./usr/
./usr/bin/
./usr/bin/wget
...etc
apt
will extract this in the root folder, and you end up with /usr/bin/wget
.