The LD_LIBRARY_PATH
environment variable tells the dynamic linker where to look for shared libraries (.so files) when executing programs. This is particularly useful when:
- Testing locally compiled libraries
- Using third-party SDKs with custom library paths
- Running applications with non-standard library dependencies
For a temporary solution (valid only for current shell session):
export LD_LIBRARY_PATH=/path/to/sdk/lib:$LD_LIBRARY_PATH
For a permanent solution (applies to all future sessions), choose one of these methods:
Edit your user's bash configuration file:
nano ~/.bashrc
Add this line at the end:
export LD_LIBRARY_PATH=/path/to/sdk/lib:$LD_LIBRARY_PATH
Then apply changes:
source ~/.bashrc
For system-wide changes (requires sudo privileges):
sudo nano /etc/environment
Add your path like this (multiple paths separated by colons):
LD_LIBRARY_PATH="/path/to/sdk/lib:/another/path"
Check if your path is correctly set:
echo $LD_LIBRARY_PATH
Test library loading with:
ldd /path/to/your/executable | grep 'not found'
- Library Precedence: Libraries in LD_LIBRARY_PATH take precedence over system libraries - use with caution
- Security: Avoid using this in production environments where possible
- 64-bit vs 32-bit: On multiarch systems, you may need to set both
LD_LIBRARY_PATH
andLIBRARY_PATH
Instead of modifying LD_LIBRARY_PATH, consider:
- Adding library path to
/etc/ld.so.conf
: - Using rpath during compilation:
sudo sh -c 'echo "/path/to/sdk/lib" >> /etc/ld.so.conf'
sudo ldconfig
gcc -Wl,-rpath=/path/to/sdk/lib -o myprogram myprogram.c
The LD_LIBRARY_PATH
environment variable tells the dynamic linker where to look for shared libraries when executing programs. This is particularly useful when:
- Working with custom-built libraries
- Using third-party SDKs with non-standard library locations
- Testing different library versions
Temporary solution (for current session):
export LD_LIBRARY_PATH=/path/to/sdk/lib:$LD_LIBRARY_PATH
Permanent solution (for all sessions):
Add this line to your ~/.bashrc
or ~/.profile
:
export LD_LIBRARY_PATH="/path/to/sdk/lib:$LD_LIBRARY_PATH"
Then run:
source ~/.bashrc
When dealing with multiple library locations:
export LD_LIBRARY_PATH="/path/to/lib1:/path/to/lib2:$LD_LIBRARY_PATH"
Check if the path is set correctly:
echo $LD_LIBRARY_PATH
Test library loading:
ldd /path/to/your/executable | grep 'not found'
For system-wide configuration (requires root privileges):
sudo sh -c 'echo "/path/to/sdk/lib" > /etc/ld.so.conf.d/custom_libs.conf'
sudo ldconfig
- Avoid adding . (current directory) to LD_LIBRARY_PATH due to security risks
- Remember that changes require terminal restart or sourcing the config file
- Prefer absolute paths over relative paths
For compiled binaries, you can hardcode library paths:
gcc -Wl,-rpath,/path/to/sdk/lib -o program program.c