The contents of the standard system library directories (usually
/usr/lib) and any directories defined in
/etc/ld.so.conf file and/or
/etc/ld.so.conf.d/*.conf files are examined by the
ldconfig command and the results are cached in
ldconfig command is normally run automatically by the package management tools whenever you install or remove library packages.
The dynamic loader uses this cache to speed up finding the required libraries whenever a new program is loaded.
If you add or remove symlinks within system library directories manually, you will most likely have to run
ldconfig as root afterwards to make the system refresh the cache and so make your changes effective. Without running that command, the dynamic loader will have no clue that the library paths have changed, and will happily keep using the library paths from the old cache.
Although the above is essentially the basic mechanism for looking up libraries, it can be overridden by using the
LD_LIBRARY_PATH(= "check these library directories first") or
LD_PRELOAD(= "always load this library first) environment variables.
It is also possible to embed library path information into the program binary itself, using section attributes named
DT_RPATH (deprecated) or
DT_RUNPATH. These will also override the cache mechanism, but as far as I know their use is fairly unusual, precisely because it tends to lead into problems like the original question when you need a program to work in a system/environment that is no longer an exact match of the system the program was developed for.