May 16, 2005

CLI Magic: ldconfig and friends

Author: Joe Barr

If you're like most Linux desktop users, there will come a day when you will want or need a program that isn't included or supported by your distribution of choice. If you build the needed app from source code, you might still not be able to run it because of missing dependencies. But nothing is more frustrating than fighting your way through that unsupported landscape, including finding and building the missing libraries, only to still get an error message like this one: "error while loading shared libraries: cannot open shared object file: No such file or directory."
That's where ldconfig comes in, and why it pays to know a little about what it does and how to use it.Dynamically linked libraries are located and loaded at run time, either by or, depending on how it was compiled: in a.out or ELF format. Each of these programs looks through a list of places to find libraries when they are needed. One place they both look in is a file named /etc/, which contains an ordered list of libraries to be searched for the needed module.

To get an idea how this works, let's use ldd to print the dependencies for a well-known program, like the GIMP. On my system, that command looks like this:

ldd /opt/gnome/bin/gimp

ldd then politely shows each dependency, including a pointer to the actual library location where it was found, and an offset
and results in this: =>  (0xffffe000) => /opt/gnome/lib/ (0x4002d000) => /opt/gnome/lib/ (0x4010c000) => /opt/gnome/lib/ (0x40111000) => /opt/gnome/lib/ (0x4011a000) => /opt/gnome/lib/ (0x40122000) => /opt/gnome/lib/ (0x40127000) => /opt/gnome/lib/ (0x40135000) => /opt/gnome/lib/ (0x403fd000) => /opt/gnome/lib/ (0x40478000) => /opt/gnome/lib/ (0x40491000) => /lib/tls/ (0x404a5000) => /opt/gnome/lib/ (0x404c8000) => /opt/gnome/lib/ (0x404d0000) => /usr/lib/ (0x404db000) => /opt/gnome/lib/ (0x404f1000) => /opt/gnome/lib/ (0x40517000) => /opt/gnome/lib/ (0x4054f000) => /opt/gnome/lib/ (0x40583000) => /lib/ (0x40588000) => /opt/gnome/lib/ (0x4058c000) => /usr/lib/ (0x4060c000) => /usr/lib/ (0x4063a000) => /lib/tls/ (0x406a9000) => /usr/X11R6/lib/ (0x407c2000) => /usr/X11R6/lib/ (0x408bf000) => /usr/X11R6/lib/ (0x408c3000) => /usr/X11R6/lib/ (0x408cb000) => /usr/X11R6/lib/ (0x408ce000) => /usr/X11R6/lib/ (0x408e1000) => /usr/X11R6/lib/ (0x408e6000) => /usr/X11R6/lib/ (0x408f0000) => /usr/X11R6/lib/ (0x408f8000)
        /lib/ (0x40000000) => /usr/lib/ (0x40906000)

Now let's see what happens if we rename one of those dependencies, so it can't be found at run time. We'll rename to /usr/X11R6/lib/ to /usr/X11R6/lib/ with this mv command:

mv /usr/X11R6/lib/ /usr/X11R6/lib/

Now, when we try to run the GIMP from the command line, we get:

warthawg@linux:~> gimp
gimp: error while loading shared libraries: cannot open shared object file: No such file or directory

Important Note: Do this right now, before you forget. Use the mv command to restore the library back to its original name, like this:

mv /usr/X11R6/lib/ /usr/X11R6/lib/

Failure to do so will cause your X session to crash.

Where is there?

Remember that file mentioned right up at the top, /etc/ It contains a complete list of all the shared object libraries that or will search in machine readable format. It is created by ldconfig, based on the contents of a human readable text file named /etc/ Here's a dump of my /etc/ file:

include /etc/*.conf

Now let's say that I've added a new application to my system. Following the project documentation, I've also added, and when I did the library ended up in the /usr/local/XYZ/lib directory. Since my /etc/ file doesn't contain a reference to that directory, the library will never get into the cache used by and at run time, and I won't be able to run the new application.

Fixing that problem is exactly as easy as it sounds. Simply add the directory to your /etc/ file, and then -- as root -- run the ldconfig program so it rebuilds /etc/

Note that even if that directory had been in your /etc/ file previously, the cache would not contain an entry for the new library. You would still need to run the ldconfig program to update the cache.

There you have it. A little deeper than we usually go in CLI Magic, but very much worth the effort when you just gotta have that app.

Click Here!