How to Set the Environmental Variable Ld_Library_Path in Linux

How to modify a LD_LIBRARY_PATH environment variable?

You can add it to your ~/.bashrc:

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/" >> ~/.bashrc

simple quest about LD_LIBRARY_PATH and new entries

Read about how ${LD_LIBRARY_PATH} is used like this:

$ man

You already know that ${PATH} is a list of directories that are polled when just an application name is given:

$ foo

and ${LD_LIBRARY_PATH} works exactly the same for the dynamic library linker.

The tricky bit is that uses a cache of the shared libraries found the last time the ${LD_LIBRARY_PATH} was used. This speeds up the program starting, just as any cache is supposed to do. Lots of examples show just mentioning a single directory to pick up an application-specific library:

$ export LD_LIBRARY_PATH=/my/wonderful/library
$ foo

but that is depending that the existing cache already any other needed library because the will know to search only that "/my/wonderful/library/" location.

A better solution is to add the new directory like this:

$ export LD_LIBRARY_PATH=/my/wonderful/library:${LD_LIBRARY_PATH}
$ foo

which will work even if the cache gets deleted out from under you and ldconfig(1) has to rebuild that cache.

This idiom that exports the ${LD_LIBRARY_PATH} permanently is a bad, bad idea:

$ export LD_LIBRARY_PATH=/my/wonderful/library:${LD_LIBRARY_PATH}
$ foo

because the custom ${LD_LIBRARY_PATH} will be used for the "foo" application plus any child program in this process tree. Instead, always limit the setting to process tree here instead of anything run from this shell ever again. Keep control of where the path is set like this:

$ LD_LIBRARY_PATH=/my/wonderful/library:${LD_LIBRARY_PATH} foo

limits visibility of the ${LD_LIBRARY_PATH} to only "foo" and any process it spawns.

What is LD_LIBRARY_PATH and how to use it?

Typically you must set java.library.path on the JVM's command line:

java -Djava.library.path=/path/to/my/dll -cp /my/classpath/goes/here MainClass

how to set LD_LIBRARY_PATH in ubuntu 11.04

The simplest way to do it is on the same command line of your program:

LD_LIBRARY_PATH="/my/special/path:$LD_LIBRARY_PATH" myprogram

You can also export that variable to make it persist through commands in the current terminal:

export LD_LIBRARY_PATH="/my/special/path:$LD_LIBRARY_PATH"


In this case, both programs will see the new library path.

Change current process environment's LD_LIBRARY_PATH

The reason

os.environ["LD_LIBRARY_PATH"] = ...

doesn't work is simple: this environment variable controls behavior of the dynamic loader ( on Linux, on Solaris), but the loader only looks at LD_LIBRARY_PATH once at process startup. Changing the value of LD_LIBRARY_PATH in the current process after that point has no effect (just as the answer to this question says).

You do have some options:

A. If you know that you are going to need from /some/path, and control the execution of python script from the start, then simply set LD_LIBRARY_PATH to your liking (after checking that it is not already so set), and re-execute yourself. This is what Java does.

B. You can import /some/path/ via its absolute path before importing When you then import, the loader will discover that it has already loaded, and will use the already loaded module instead of searching for it again.

C. If you build yourself, you can add -Wl,-rpath=/some/path to its link line, and then importing will cause the loader to look for dependent modules in /some/path.

Setting LD_LIBRARY_PATH environment variable for loading a shared library at runtime (g++)

To answer the second question first:

source executes the script inside the current shell, ./ opens and executes it in a different shell.

Now for your first question:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH ./test sets the LD_LIBRARY_PATH variable before just one command (the ./test command). For the same reason above, I believe this isn't getting transferred to whatever shell ./test creates. To make it persist, you may need to put the export LD_LIBRARY_PATH=... in your ~/.bashrc

Related Topics

Leave a reply