Linux Commands

How to Export the Ld_Library_Path in Linux

In Linux, having a short glimpse of environment variables helps the users to understand more about the system’s work. These variables let you configure and optimize the system accordingly. The “ld_library_path” is one of those variables which holds an information about where the shared libraries are stored in your system.

It means that “ld_library_path” contains a list of paths where these libraries are stored so that the executable files can access them whenever required.

You can also add new paths to the “ld_library_path” to maintain a well-organized system file structure. This quick guide explains a simple method to export the “ld_library_path” in Linux so that you can add new paths to it.

How to Export the Ld_Library_Path in Linux

Shared libraries are essential elements of Linux applications that consist of codes or scripts that multiple other programs can reuse. This process enhances the overall efficiency of Linux systems. When you execute a program, the system dynamically links it with the required libraries which ensures that it runs without error.

Exporting involves setting the “ld_library_path” to specific values that indicate the paths that are mentioned earlier. For this, use the “export” command in your shell’s configuration file. Go through the following sections for a step-by-step explanation:

1. Check the Current Directories in Ld_Library_Path

It is a good practice to check the current value of “ld_library_path” to check the current directories.

echo $ld_library_path

This command shows nothing in our system because we have yet to configure it. In general, it displays either the added path of libraries or nothing.

2. Configure the Shell
Bash is the default shell in most of the Linux distros. However, you can always change the shells according to your needs. To configure a shell, you must open your shell’s configuration file using one of the following commands that correspond to your shell:

For Bash: nano ~/.bashrc

For Zsh: nano ~/.zshrc

For Fish: nano ~/.config/fish/config.fish

Always remember that the tilde (~) sign indicates the home directory in Linux. A text file would open upon entering the command like the following:

Now, to export the LD_LIBRARY_PATH, go to the last line and add the following command:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path

A colon (:) separates all the paths in an environment variable. Therefore, we use a colon to add a new path.

Replace “/path” here with the path that you want to export. For your understanding, let’s take the “Documents” directory as an example:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/Documents

After typing this command, save and exit the configuration file. In Bash, you can do it in three steps: press CTRL + X, Y, and Enter. Similarly, exit your respective files.

Moreover, this saves the file but it does not update in the current terminal session. Hence, enter the following command to implement the changes:

source ~/.bashrc

Replace “~/.bashrc” with the file paths that you used in the first step. It does not display anything upon execution.

Lastly, you must verify that the path is successfully added using the following command:

echo $LD_LIBRARY_PATH

Note: If it does not show anything, you must have made a mistake in the previous steps. It might be a spelling issue or you missed a colon, other signs, etc. Therefore, follow all the steps correctly.

Conclusion

The LD_LIBRARY_PATH is a crucial environment variable in which you can set the paths to the shared libraries so that the programs can access them whenever needed. Since most users are deprived of its method, this guide focuses on explaining how to export the LD_LIBRARY_PATH in Linux using simple steps. This process often comes in handy when maintaining an organized file directory structure.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.