One of such common errors which I often see while installing a program from its source code is error while loading shared libraries. The full error generally looks like this:
error while loading shared libraries:
cannot open shared object file: No such file or directory
For example, I was trying to use FreeRADIUS server and it showed me this error:
radiusd: error while loading shared libraries: libfreeradius-radius-2.1.10.so: cannot open shared object file: No such file or directory
The reason behind this error is that the libraries of the program have been installed in a place where the dynamic linker cannot find them.
Let me show you how you can go about fixing this issue.
Fixing ‘cannot open shared object file: No such file or directory’ error
One quick way to fix this “error while loading shared libraries” automatically is to use ldconfig.
All you need to do is to open the terminal (Ctrl+Alt+T) and type the following command:
sudo /sbin/ldconfig -v
This one-liner should solve the problem in most cases. However, if it doesn’t, I have discussed another method to handle this error. But before that, let me tell you what does the above command do.
What are shared object files? How does the above command fixes the issue?
You see, in C/C++, a .so (shared object) is a compiled library file. It is called shared object because this library file can be shared by several programs. These generated libraries are usually located in /lib or /usr/lib directories.
Now if you wonder how did this tiny command fixed this problem, you should read the man page of ldconfig which says:
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache is used by the run-time linker, ld.so or ld-linux.so. ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.
I hope this quick fix helps you in eliminating the nasty error while loading shared libraries message in Ubuntu and other Linux.
If not, you can do some investigation and try to fix the issue the way it is mentioned in the next section.
An alternate method to fix ‘cannot open shared object file’ error
The above discussed method fixes the issue if the library in question is available in your system. But that may not always be the case.
If you do not have the program installed on your system, you won’t have its library file. The ldconfig cannot do anything if there is no library file in the first place.
So, the alternate method is to install the required program and it should create the library automatically.
Let me show it to you by an example. Let’s say you see this error:
error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory
The problem is with libgobject version 2.0. The version number is important because some programs depend on a specific version of the library and if they don’t find, they complain about it.
Now, apt provides the search option that can be used for searching a package and knowing its version before installing it.
abhishek@itsfoss:~$ apt search libgobject Sorting... Done Full Text Search... Done librust-gobject-sys-dev/focal 0.9.0-2 amd64 FFI bindings to libgobject-2.0 - Rust source code
Now, this librust-gobject-sys-dev package could be what you need if you know that you were trying to run a Rust program. But what if it was a Python program you were running that complained about it?
You can widen your search by removing the lib from the package name while searching. The lib signifies library and libraries may be provided by a generic package that could be named gobject-xyz.
It would be a good idea to search for the string in the names of the package (instead of descriptions) to get more concise results.
abhishek@itsfoss:~$ apt search --names-only gobject Sorting... Done Full Text Search... Done gobject-introspection/focal-updates 1.64.1-1~ubuntu20.04.1 amd64 Generate interface introspection data for GObject libraries libavahi-gobject-dev/focal 0.7-4ubuntu7 amd64 Development headers for the Avahi GObject library libavahi-gobject0/focal 0.7-4ubuntu7 amd64 Avahi GObject library libcairo-gobject-perl/focal,now 1.005-2 amd64 [installed,automatic] integrate Cairo into the Glib type system in Perl libcairo-gobject2/focal,now 1.16.0-4ubuntu1 amd64 [installed,automatic] Cairo 2D vector graphics library (GObject library) libghc-gi-gobject-dev/focal 2.0.19-1build1 amd64 GObject bindings libghc-gi-gobject-doc/focal,focal 2.0.19-1build1 all GObject bindings; documentation
In the above truncated output, you’ll have to see if the package is related to the original program you were trying to run. You must also check the version of the library provided.
Once you have identified the correct package, install it like this:
sudo apt install package_name
Once installed, you may run the ldconfig command again to update the cache:
sudo /sbin/ldconfig -v
This method requires some effort on your end but this is how the dependencies are handled.
Nothing works; what now?
If you are unfortunate enough, the above methods might not work for you. What can you do?
First, keep in mind that the shared libraries may be used from other packages in some cases. If you were trying to run XYZ program and ABC program installs the correct version of the shared library, it may (or may not) work for you. You can give it a hit and trial.
Second, if you are trying to run a program that is too old or too new, it may require a library version that is not available for your Linux distribution.
What you may do is check if you can use some other version of the program—for example, using Eclipse version 3 instead of version 4. This may help your case.
The other way would be to check the developer's website or forums and see if you can manually install the correct version of the library from its source code. That requires a lot of effort (in 2020) but you don’t have a lot of options.
Did it work for you?
I hope I have made things a bit more clear for you. Did you manage to fix the issue of shared libraries in your system? If you have questions, or suggestions, feel free to drop a comment. Ciao :)