The LD_LIBRARY_PATH environment variable controls where Linux searches for shared libraries during program execution. Set it incorrectly and applications fail. Configure it properly and programs run smoothly.
What Does LD_LIBRARY_PATH Do?
LD_LIBRARY_PATH tells the dynamic linker where to find shared libraries at runtime. When you execute a program, the dynamic linker (ld.so) searches these directories before checking standard locations like /lib and /usr/lib.
Multiple directories separate with colons. The system searches them in order from left to right.
Common use cases include:
- Testing new library versions without system-wide installation
- Running applications with custom library locations
- Creating portable software environments
- Preserving old library versions for legacy applications
LD_LIBRARY_PATH affects runtime behavior. The similar variable LIBRARY_PATH affects compilation and linking.
How LD_LIBRARY_PATH Causes Problems
Misuse of LD_LIBRARY_PATH creates serious issues. Understanding these problems prevents future debugging nightmares.
Security Risks
The dynamic linker searches LD_LIBRARY_PATH directories before standard locations. Malicious users can exploit this by placing harmful libraries in these directories. Setuid and setgid executables ignore this variable for this reason.
Performance Impact
The linker must search every directory in LD_LIBRARY_PATH for each shared library. Failed open() system calls multiply with each directory added. Applications on network filesystems experience significant slowdowns.
Library Version Conflicts
Wrong library versions load when paths override system libraries. Applications crash or produce incorrect results. Debugging these issues wastes hours.
LD_LIBRARY_PATH to system-wide profile files. This exposes all applications to potential conflicts.
Setting LD_LIBRARY_PATH in Linux
Open your shell configuration file:
$ nano ~/.bashrc
Add the export statement at the end:
export LD_LIBRARY_PATH=/path/to/your/libraries
Save the file and reload the configuration:
$ source ~/.bashrc
Verify the change:
$ echo $LD_LIBRARY_PATH
Adding Multiple Directories
Append new paths instead of replacing existing ones:
export LD_LIBRARY_PATH=/new/path:${LD_LIBRARY_PATH}
This preserves existing library paths that other applications need.
| Method | Command | Result |
|---|---|---|
| Replace | export LD_LIBRARY_PATH=/new/path |
Only new path remains |
| Append | export LD_LIBRARY_PATH=/new/path:${LD_LIBRARY_PATH} |
Both paths exist |
Checking Which Libraries Load
Use ldd to display library dependencies:
$ ldd /usr/bin/program
linux-vdso.so.1 => (0x00007fff9646c000)
libexample.so.1 => /usr/lib64/libexample.so.1 (0x00000030f9a00000)
libc.so.6 => /lib64/libc.so.6 (0x00000030f8200000)
This shows where the system finds each library. Missing libraries display “not found” messages.
ldd after changing LD_LIBRARY_PATH to verify correct library paths.
Better Alternatives to LD_LIBRARY_PATH
Avoid LD_LIBRARY_PATH when possible. These methods provide better solutions.
Use RPATH During Compilation
Embed library paths directly into executables:
$ cc -o program obj1.o obj2.o -Wl,-rpath=/path/to/lib -L/path/to/lib -lmylib
Set LD_RUN_PATH for multiple directories:
$ export LD_RUN_PATH=/path/to/lib1:/path/to/lib2
$ cc -o program obj1.o obj2.o -L/path/to/lib1 -lmylib1
Create Wrapper Scripts
Set LD_LIBRARY_PATH only for specific applications:
#!/bin/sh
LD_LIBRARY_PATH=/path/to/lib1:/path/to/lib2
export LD_LIBRARY_PATH
exec /path/to/bin/program "$@"
This limits the variable’s scope to one application.
Test From Command Line
Set the variable for a single command:
$ env LD_LIBRARY_PATH=/path/to/libs ./program
The variable does not persist after the command finishes.
LD_LIBRARY_PATH in your shell affects all subsequent commands until you close the terminal.
System-Wide Configuration
Modify system library paths through /etc/ld.so.conf.d/ instead of LD_LIBRARY_PATH.
Create a new configuration file:
# echo "/path/to/custom/libs" > /etc/ld.so.conf.d/custom.conf
Rebuild the library cache:
# ldconfig
This method affects all users but maintains proper system organization.
FAQs
LD_LIBRARY_PATH tells the dynamic linker where to search for shared libraries at runtime. It specifies directories that the system checks before standard locations like /lib.
Add export LD_LIBRARY_PATH=/your/path:${LD_LIBRARY_PATH} to ~/.bashrc then run source ~/.bashrc. Changes apply to new terminal sessions automatically.
The library directory is not in LD_LIBRARY_PATH or standard locations. Add the directory to LD_LIBRARY_PATH or use ldd to check which libraries are missing.
No. Adding it system-wide creates security risks and conflicts. Use wrapper scripts for specific applications or embed paths with RPATH during compilation instead.
LD_LIBRARY_PATH affects runtime library loading. LIBRARY_PATH helps the compiler find libraries during linking. They serve different stages of program execution.