Friday, April 15, 2011

How to install libpcap on latest Ubuntu

I haven't used libpcap since I posted this blog last time (2011). But many people may not get it work following the old steps below. Therefore, I tried to install libpcap myself on the latest Ubuntu 12.10.

Here are the steps:
  1. Download libpcap-1.3.0.tar.gz from and decompress it;
  2. Download flex-2.5.37.tar.gz from and decompress it;
  3. Download bison-2.7.tar.gz from and decompress it;
  4. Download m4-1.4.16.tar.gz from and decompress it;
  5. Then, enter into directories m4-1.4.16, bison-2.7, flex-2.5.37 and libpcap-1.3.0 in order, and execute the following commands:
    1. $ sudo ./configure
    2. $ sudo make
    3. $ sudo make install
  6. ** Basically, just find the latest version from these websites to download.
Until now, installation is finished. You may test it with the following code (test.c):

    #include <stdio.h>
    #include <pcap.h>

    int main(int argc, char *argv[])
        char *dev, errbuf[PCAP_ERRBUF_SIZE];

        dev = pcap_lookupdev(errbuf);
        if (dev == NULL) {
            fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        printf("Device: %s\n", dev);

Commands to compile and execute this program:
  1. $ gcc test.c -lpcap
  2. $ sudo ./a.out
  3. ** use sudo to execute the program, because of permission required
Expected output should be like this:
                                                          Device: eth0

One problem you may come across during running the program:

"error while loading shared libraries: cannot open shared object file: No such file or directory"

This is because we have installed using .tar.gz, then library is installed in /usr/local/lib directory.


Open /etc/ as root, and add the following two lines to it:


Then execute command:

$ sudo ldconfig

Now compile and run test.c again, and you may get the result.


Old Version:

Libpcap is a portable C/C++ library for network traffic capture. Although libpcap is primarily a packet-capturing tool, it also can create and manipulate packets from saved files, which can then be used in the wide variety of tools that support the libpcap format.

In this article, I will show how to install libpcap on Ubuntu.

  1. Download libpcap-1.0.0.tar.gz (512.0KB) from, and decompress it (tar zxvf libpcap-1.0.0.tar.gz) to your self-defined directory;
  2. Download flex-2.5.35.tar.gz (1.40MB) from, and decompress it (tar zxvf flex-2.5.35.tar.gz) to your self-defined directory;
  3. Download bison-2.4.1.tar.gz (1.9MB) from, and decompress it (tar zxvf bison-2.4.1.tar.gz) to your self-defined directory;
  4. Download  m4-1.4.13.tar.gz (1.2MB) from, and decompress it (tar zxvf m4-1.4.13.tar.gz) to your self-defined directory;
  5. Then, enter into the directories m4-1.4.13, bison-2.4.1, flex-2.5.35, libpcap-1.0.0 in the order, and execute the following commands:
    1. (sudo) ./configure
    2. (sudo) make
    3. (sudo) make install
Now, everything should work well. And you can get a .c file to test your installation.

Notice: installing libpcap and running your programs should have an access to your root priority.
Good Luck! ^_^


  1. use this url.
    I can't install this

    1. Sorry, I haven't used it for a long time. But I guess it may be the problem of version.

    2. renjith..
      try these link for bison and m4..

      i hope it will solve ur problem..

  2. hello sir

    thank for this

    i need to know what should i do after download all fill zip

    put in same folder then upload because when i do ask me copy file overrite same file found so what should i do

    1. I'm afraid I cannot really understand your problem. Do you mean overwriting happened during decompression?

  3. For UbUntU m4,flex,bison can directly be installed

    apt-get install m4 flex bison

  4. Yeah thanks. Would have been nice to have some explanations on what are each of this packet for? M4 ? Flex ? Bison ?

  5. In ubuntu 12, add these to libc.conf (or any of the conf) in /etc/

  6. thanks a lot..before come to this page i wasted my 6 hrs but finally succeeded to install libpcap...

  7. i installed libpcap but now when i execute the above given program am getting the error ./a.out:error while loading shared open shared object file:No such directory or file. can any one provide a soln asap

  8. hi i can't find in mac please tell me what to do???

  9. Firstly, Thanks a lot mate. It really helped. Amazing!
    It will be nice if you can add the following along with the above process.

    Adding lines to with the following command!

    sudo sh -c "echo '/usr/local/lib' >> /etc/"


  10. wow this really saved me hours of time Thanks a lott !!!

  11. Your guidance really helped me a lot :) Keep it up!