How to Configure System-wide LD_LIBRARY_PATH for /usr/local/lib in CentOS 6


2 views

When installing custom libraries in /usr/local/lib on CentOS 6, you'll quickly encounter the classic "library not found" issue when trying to run applications that depend on them. The system's dynamic linker needs explicit instructions about where to look for these libraries.

# PATH - For finding executables
# LD_LIBRARY_PATH - For finding shared libraries at runtime
# These need different configuration approaches

For CentOS 6, the most standard method involves modifying these files:

# For shell environment (affects interactive sessions)
/etc/profile

# For non-login shells
/etc/bashrc

# For systemd services (though less relevant for CentOS 6)
/etc/environment

Add these lines to /etc/profile:

# System-wide library path configuration
if [ -z "$LD_LIBRARY_PATH" ]; then
    LD_LIBRARY_PATH="/usr/local/lib"
else
    LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH

# Optional: Add /usr/local/bin to PATH if needed
PATH="/usr/local/bin:$PATH"
export PATH

For more granular control, consider these approaches:

1. Creating a custom config file

# Create /etc/profile.d/local_libs.sh
echo 'export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"' > /etc/profile.d/local_libs.sh
chmod +x /etc/profile.d/local_libs.sh

2. Using ldconfig

# Add /usr/local/lib to the ld.so.conf
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig

After making changes, test with:

# Check LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH

# Verify library paths
ldconfig -p | grep local

# Test loading a library
LD_DEBUG=libs ldd /path/to/your/binary
  • Order matters in PATH/LD_LIBRARY_PATH - earlier entries take precedence
  • Security implications of system-wide PATH modifications
  • Alternative approaches like rpath for application-specific library paths
  • Differences between CentOS 6 and newer versions

For a Python C extension installed in /usr/local/lib:

# Without proper LD_LIBRARY_PATH:
import myextension  # Fails with ImportError

# After configuration:
import myextension  # Works!

When installing custom-built libraries in /usr/local/lib on CentOS 6, the system doesn't automatically include this directory in the library search path. Unlike /usr/lib which is hardcoded into ld.so, we need to explicitly configure /usr/local/lib for system-wide access.

There are several approaches, each with different implications:

# Method 1: Using /etc/ld.so.conf.d/ (Recommended)
echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/local_lib.conf
sudo ldconfig

# Method 2: Global profile modification (Affects all users)
sudo sh -c 'echo "export LD_LIBRARY_PATH=/usr/local/lib:\$LD_LIBRARY_PATH" >> /etc/profile'

For CentOS 6, the cleanest approach is creating a custom config file in /etc/ld.so.conf.d/:

# Create a new configuration file
sudo vi /etc/ld.so.conf.d/local.conf

# Add this single line:
/usr/local/lib

# Then update the library cache:
sudo ldconfig -v | grep /usr/local/lib  # Verify it's included

If you prefer using environment variables, modify /etc/environment:

LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Or for user-specific settings, add to ~/.bashrc:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

After configuration, verify with:

ldconfig -p | grep local
# Or test with a sample program:
LD_DEBUG=libs ldd /path/to/your/program | grep local

Common issues include:

  • Forgetting to run ldconfig after changes
  • Conflicts with SELinux policies (check /var/log/audit/audit.log)
  • Permission issues on the library files
  • Always use absolute paths in configuration
  • Document library path changes in system documentation
  • Consider using RPATH for application-specific library locations
  • Test changes in a staging environment first

Remember that while LD_LIBRARY_PATH works, the ld.so.conf method is generally more maintainable and less prone to environment-related issues.