Can I set LD_LIBRARY_PATH for an individual application? I am looking into system call failure, so is there any way I can set set the correct path using the LD_LIBRARY_PATH setting?
3 Answers
Simplest way would be to create a shell script.
Have the shell script export your new LD_LIBRARY_PATH variable then launch your application
e.g. (where foo is your app)
#!/bin/sh
LD_LIBRARY_PATH=some_path:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
foo

- 21,816
- 3
- 61
- 76
As simple as:
LD_LIBRARY_PATH=new_path:$LD_LIBRARY_PATH foo
which works in bash. I think it works in all bourne shell derivatives, but I can't guarantee it.
Of course, with this approach, you have to type the path every time. To do it repeatedly, prefer Glen's approach.

- 1
- 1

- 98,632
- 24
- 142
- 234
-
The technique works in Bourne shell, Korn shell, and POSIX shells. – Jonathan Leffler Feb 05 '10 at 17:15
One item to be aware of: you cannot set LD_LIBRARY_PATH
within a program and make it have any effect on the current program. This is because the dynamic loader (ld.so.1
or some similar name) is already loaded and has read and processed the environment variable before any of your code is run. You can set it in the current process's environment, and that value will then affect any child processes, and you could use one of the exec()
family of functions to run a program with the environment set. In an extreme case, you could re-execute the current program - but that is extreme!

- 730,956
- 141
- 904
- 1,278
-
I wonder if `dlopen` would be effective after LD_LIBRARY_PATH is set within the program – JamesWebbTelescopeAlien Jun 29 '17 at 01:46
-
You can find the source and look to see whether `dlopen()` reads the environment variable each time it is called. My impression is that it does not, but it is a long time since I experimented with and things may have changed in the last decade. – Jonathan Leffler Jun 29 '17 at 02:07