Setting Library path in Linux


To totally unlock this section you need to Log-in


Login

You need to use ldconfig config file and ldconfig command which 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 (for Debian distributions), and in the trusted directories such as /lib64 or /usr/lib64 (/lib or /usr/lib on 32 bit systems). The /etc/ld.so.conf contains lib settings which can be used to add or delete paths.

However, you need to simply drop your config file in /etc/ld.so.conf.d/ directory and it will be used by /sbin/ldconfig to configure dynamic linker run time bindings.

Add Your Path

Create a file called /etc/ld.so.conf.d/myapp.conf:

# vi /etc/ld.so.conf.d/myapp.conf

Add the following path:

/usr/local/lib

Save and close the file.

Activate Your Library Path

You must run the following command to activate path:

# ldconfig

Verify Your New Library Path

# ldconfig -v | less

# ldconfig -v | grep /usr/local/lib

Sample outputs:

/usr/local/lib:
        libGeoIP.so.1 -> libGeoIP.so.1.4.6
        libGeoIPUpdate.so.0 -> libGeoIPUpdate.so.0.0.0
/usr/lib64/mysql:
        libmysqlclient_r.so.15 -> libmysqlclient_r.so.15.0.0
        libmysqlclient.so.15 -> libmysqlclient.so.15.0.0
/lib:
        libutil.so.1 -> libutil-2.5.so

How Do I Delete The Library Path?

Simply, delete the file:

# rm /etc/ld.so.conf.d/myapp.conf
# ldconfig

How Do I Edit The Library Path?

Simply edit the file and reload the changes:

# vi /etc/ld.so.conf.d/myapp.conf
# ldconfig

How Do I Compile Program With Shared Libs And GNU GCC?

You can use the following gcc:

$ gcc -Wl,-R/path/to/lib -I/path/to/include -L/path/to/lib -o myAppName mycode.c -llibapp2

Add to LD_LIBRARY_PATH

Add the library path you need and set this at shell for temporary use or add to the shell initialization file for permanent effect:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/foobar/lib

Check shared libraries used by program

You can see the list of the shared libraries used by a program using ldd. So, for example, you can see the shared libraries used by ls by typing:

ldd /bin/ls

Generally you'll see a list of the sonames being depended on, along with the directory that those names resolve to. In practically all cases you'll have at least two dependencies:

  • /lib/ld-linux.so.N (where N is 1 or more, usually at least 2). This is the library that loads all other libraries.
  • libc.so.N (where N is 6 or more). This is the C library. Even other languages tend to use the C library (at least to implement their own libraries), so most programs at least include this one.

Beware: do not run ldd on a program you don't trust. As is clearly stated in the ldd(1) manual, ldd works by (in certain cases) by setting a special environment variable (for ELF objects, LD_TRACE_LOADED_OBJECTS) and then executing the program. It may be possible for an untrusted program to force the ldd user to run arbitrary code (instead of simply showing the ldd information). So, for safety's sake, don't use ldd on programs you don't trust to execute.