When you encounter an error like the following on a Linux system: “error while loading shared libraries: libexample.so.1: cannot open shared object file: No such file or directory”. it means that the application you are trying to run is unable to find a required shared library. This article will explain what this error means, why it happens, and how to resolve it.
Shared Libraries and Their Naming Convention
In Linux, shared libraries (files ending with .so) follow a specific naming convention that typically includes a version number. For example, you might see a file named:
libpthread_rt.so.1
The “.1” is part of the SONAME (shared object name) and represents the major version of the library. When a program is compiled, it records this SONAME as the dependency it expects to load at runtime. Therefore, the application specifically looks for a file named libpthread_rt.so.1 rather than just libpthread_rt.so.
This naming convention ensures that applications link against the correct version of a library, maintaining compatibility and stability across updates.
Why this Error Occurs
The error message:
error while loading shared libraries: libpthread_rt.so.1: cannot open shared object file: No such file or directory
indicates that although a library file might exist (for example, /lib/libpthread_rt.so), the application is specifically looking for /lib/libpthread_rt.so.1. The absence of a file with the exact expected name prevents the dynamic linker from loading the library, leading to the error.
How to Diagnose the Problem
1. Identify the Missing Library
- Examine the error message to see which library is reported as missing (e.g., libpthread_rt.so.1).
- Use commands like
find
orlocate
to search for the file on your system:
find / -name "libpthread_rt.so*" 2>/dev/null
Example Output:
/usr/lib/x86_64-linux-gnu/libpthread_rt.so
If the exact version (libpthread_rt.so.1) is missing, the application will fail to load it.
2. Check the Library’s SONAME
To ensure that the existing library is indeed the correct one, inspect its SONAME:
readelf -d /usr/lib/x86_64-linux-gnu/libpthread_rt.so | grep SONAME
Example Output:
Library soname: [libpthread_rt.so.1]
If the SONAME matches the missing file, a symbolic link may resolve the issue.
Solutions to Fix the Error
1. Create a Symbolic Link
If the correct library exists but with a different filename (missing the version suffix), you can create a symbolic link that matches the expected name.
Steps:
- Open a terminal and navigate to the directory where the library is located (commonly
/lib
or/usr/lib
). - Create the symbolic link:
sudo ln -s /usr/lib/x86_64-linux-gnu/libpthread_rt.so /usr/lib/x86_64-linux-gnu/libpthread_rt.so.1
- Update the dynamic linker cache:
sudo ldconfig
Why This Works:
This approach tells the system that libpthread_rt.so.1 should point to libpthread_rt.so, satisfying the dependency of your application.
2. Install the Correct Version of the Library
If creating a symbolic link isn’t ideal (perhaps due to compatibility concerns or version mismatches), the better solution is to install the correct version of the library.
Using a Package Manager:
- Debian/Ubuntu:
sudo apt-get update
sudo apt-get install <package-containing-libpthread_rt>
- Red Hat/CentOS/Fedora:
sudo yum install <package-containing-libpthread_rt>
Replace <package-containing-libpthread_rt>
with the actual package name. If you’re unsure, search for the correct package:
apt-cache search libpthread
3. Verify Library Paths
If the library exists but isn’t being found, the dynamic linker might not be searching the correct directories.
Check Environment Variable:
echo $LD_LIBRARY_PATH
Example Output:
/usr/local/lib:/usr/lib/x86_64-linux-gnu
If the correct directory isn’t included, add it manually:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
To make this change permanent, add the line to your ~/.bashrc or ~/.profile file.
Update the Dynamic Linker Cache:
After installing libraries or creating symbolic links, always run:
sudo ldconfig
This command refreshes the list of available shared libraries.
Common Errors & Troubleshooting Tips
Even after following the steps above, you might still encounter issues. Here are some common pitfalls and their solutions:
1. Permission Denied While Creating a Symbolic Link
Error:
ln: failed to create symbolic link ‘/lib/libpthread_rt.so.1’: Permission denied
Solution:
Ensure you have the necessary permissions by using sudo
:
sudo ln -s /usr/lib/x86_64-linux-gnu/libpthread_rt.so /usr/lib/x86_64-linux-gnu/libpthread_rt.so.1
2. ldconfig Command Not Found
If ldconfig
is missing, reinstall the libc-bin package:
sudo apt-get install --reinstall libc-bin
3. Library Still Not Found After Installation
If the library still isn’t found after installing it:
- Verify that it’s in a directory recognized by the linker:
ls -l /usr/lib/x86_64-linux-gnu/libpthread_rt.so.1
- If the file exists but isn’t detected, force a refresh of the linker cache:
sudo ldconfig -v
External References
Conclusion
The error “error while loading shared libraries: cannot open shared object file: No such file or directory” typically occurs because the application is unable to locate a shared library with the exact filename (including version numbers) it expects.
To resolve the issue:
- Identify the missing library by examining the error message.
- Check if the correct library exists using
find
andreadelf
. - Fix the issue by either:
- Creating a symbolic link (
ln -s
) - Installing the correct library version via package manager
- Updating library paths (
export LD_LIBRARY_PATH
)
- Creating a symbolic link (
- Run
sudo ldconfig
to update the dynamic linker cache.
Following these steps should help you resolve the error and allow your application to load the required shared libraries successfully.