I want to remove some of the paths the linker uses to find .so libraries for testing purposes.

I have found a way to add library paths:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path/to/library"

Are there a variable similar to LD_LIBRARY_PATH that I can use to remove library paths such as /usr/local/lib or /usr/lib that are not in LD_LIBRARY_PATH but picked up by the linker? I.e. how can I ignore paths that are given in /etc/ld.so.conf.d/ ?

The reason for this is that I am busy creating a program that, for a given executable, it recursively finds library dependencies. I want to see if I can make a program more portable by finding all its dependencies, copying those dependencies into a local directory, and make a local-run bash script to setup LD_LIBRARY_PATH and then run the executable. I want to test if this local-run-executable works after previously important library search paths are removed.


You would be interested in removing library paths if a given shared library has embedded paths via the rpath feature. Those are added at the time the library is created by the linker.

You can remove (or alter) those paths using chrpath, e.g.,

chrpath -d mylibraryfile.so

Removing pathnames from the LD_LIBRARY_PATH variable also is a possible area of interest; you can do that by string substitution and re-exporting the variable. However, the question does not seem to be concerned with that. There is no variable which acts to cancel out LD_LIBRARY_PATH.

For seeing library dependencies, the mention of /etc/ld.so.conf.d/ makes it sound as if the platform is only Linux. You can use ldd to list dependencies. Aside from OSX, all of the BSDs also support ldd. Here is one of the scripts which I use for this purpose:

# $Id: ldd-path,v 1.1 2007/07/09 19:30:28 tom Exp $
# Edit the output of ldd for the given parameters, yielding only the
# absolute pathnames.
ldd $* | sed \
        -e 's/([^)]*)//g' \
        -e 's/^.*=>//' \
        -e 's/[         ][      ]*//g' \
        -e '/^$/d'

But (addressing a comment), there is no portable mechanism for telling the loader to ignore an existing path. The GNU ld documentation gives a summary of what is sought, and the order in the description of the -rpath option. These items conclude the list:

  • The default directories, normally /lib and /usr/lib.
  • For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file.

Further reading

  • If I am correctly reading this answer, then you have given me a tool to make my portable-isation robust against embedded paths in the library (thanks, willl have a look!). Or am I missing something? Will follow your links. – Simon Streicher Mar 26 '16 at 11:17
  • 1
    yes - I've used this tool to remove paths (incorrectly) added by developers which interfere with building packages. – Thomas Dickey Mar 26 '16 at 11:19
  • I have edited my question to be more specific (how can I ignore paths that are given in /etc/ld.so.conf.d/). Although very useful (thanks), this does not quite answers my question. – Simon Streicher Mar 26 '16 at 11:48
  • 2
    Short: you can't do that portably. You can only add paths to LD_LIBRARY_PATH, which are checked before the linker's defaults. – Thomas Dickey Mar 26 '16 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.