≡ Menu

Linux: Set OR Change The Library Path

I‘ve compile and installed a library at /usr/local/lib/libapp2.so -> libapp2.so.1.4.3. How do I set the Library path under Linux operating systems?

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, 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:


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:

        libGeoIP.so.1 -> libGeoIP.so.1.4.6
        libGeoIPUpdate.so.0 -> libGeoIPUpdate.so.0.0.0
        libmysqlclient_r.so.15 -> libmysqlclient_r.so.15.0.0
        libmysqlclient.so.15 -> libmysqlclient.so.15.0.0
        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
See gcc man page for further details.

Further readings:

man ldconfig
man ld
man gcc

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 4 comments… add one }
  • Michael April 11, 2010, 6:45 pm

    Great article.
    Unfortunately in Fedora 12 the (local) manual page of ldconfig is missing :-(

  • Vijay Kanta August 11, 2013, 11:56 am

    You don’t cease to amaze me Vivek. Great article to keep my Linux journey going.

  • Anil September 17, 2013, 11:41 pm

    Great information…
    Thanks a lot.

  • peltouze January 7, 2016, 2:00 pm

    Old thread but still, I’ll try.
    Can’t we just add any path to LD_LIBRARY_PATH environment variable ?
    (I’m working on a system on which I don’t have root privileges…)

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , , , , , , , , , ,