How do I permanently set the ld_library_path environment variable in Linux?

I’m trying to configure the ld_library_path in Linux so it persists across terminal sessions.

I initially ran:

export LD_LIBRARY_PATH=/usr/local/lib

Then I edited ~/.bash_profile and added:

LD_LIBRARY_PATH=/usr/local/lib  
export LD_LIBRARY_PATH

But after restarting the terminal, running echo $LD_LIBRARY_PATH gives an empty result.

What’s the correct and permanent way to set ld_library_path so it loads every time I start a new shell?

Should I be editing .bashrc, .profile, or something else?

Would appreciate clarification!

In most interactive shell sessions, .bashrc is actually the file that gets sourced, not .bash_profile.

So if you’re setting LD_LIBRARY_PATH inside .bash_profile, but starting a terminal that sources .bashrc, it won’t carry over.

Try adding the export directly to ~/.bashrc like this:

bash
Copy
Edit
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Then either restart your shell or run source ~/.bashrc to reload.

This makes sure your change kicks in every time you launch a terminal window, especially if you’re using something like GNOME Terminal or xterm.

If your Linux distro uses ~/.profile as the default shell config (common with Debian/Ubuntu variants and graphical sessions), that might be a better place for persistent environment variables like LD_LIBRARY_PATH.

You can safely append the following:

bash
Copy
Edit
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Then log out and log back in, not just restart the terminal.

This ensures it gets picked up during full login shells or desktop environment launches, which often don’t read .bashrc or .bash_profile by default.

For system-wide persistence (especially useful if multiple users need it), placing the variable in a custom script under /etc/profile.d/ works well.

You can create a new file like:

bash
Copy
Edit
sudo nano /etc/profile.d/custom_ldpath.sh

And inside:

bash
Copy
Edit
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Then save, exit, and reboot.

This ensures the variable is available to all login shells system-wide. Just keep in mind: if you override LD_LIBRARY_PATH, always append :$LD_LIBRARY_PATH so you don’t clobber existing paths accidentally.