Installing FANN with Python bindings on Ubuntu


The Fast Artificial Neural Network Library (FANN) is a neural network library, which implements multilayer artificial neural networks in C with support for both fully connected and sparsely connected networks. It has a Python binding that allows you to use its functionality from within Python, but with the bits that need speed implemented in C.

We are going to install the FANN library on Ubuntu and install the Python binding. Get and unzip the library:

sudo apt-get install unzip

Configure, make and install the library:

cd fann-2.1.0/
sudo apt-get install gcc make
sudo make install

Install the Python bindings:

cd python/
sudo apt-get install g++ python-dev swig
sudo python install

The Python files are now located in a build directory. Copy them to a place where you can use them, e.g. your home directory:

cd build/lib.linux-i686-2.6/pyfann/
cp ~
cp ~

And finally test that Python can now work with the library, start up Python and type:

import libfann
print dir(libfann)

This should print out all the functions of the library.

7 thoughts on “Installing FANN with Python bindings on Ubuntu

  1. Permalink  ⋅ Reply


    June 14, 2011 at 7:31pm

    Really useful and straightforward. Thanks for sharing!

  2. Permalink  ⋅ Reply


    June 16, 2011 at 12:48pm

    Just two notes, in case it can be useful to whoever reads this “howto”.

    In case you get troubles installing this binding on a x86-64 system, you can get help in this link:

    And one question: wouldn’t be a better choice something like /usr/lib/ in order to place the libraries in order to be accessible from whatever dir we’re working on?

  3. Permalink  ⋅ Reply


    June 4, 2012 at 1:55am

    the best choice would be, instead of copying file, to just write in your project:
    from pyfann import libfann

    and it works! 🙂

  4. Permalink  ⋅ Reply


    March 1, 2013 at 11:01pm

    Thanks a lot for the tutorial, worked perfectly for me 🙂
    It seems that there is a new version of FANN out there, namely 2.2.0, can I try to install it in the same manner? And if yes, do I have to uninstall FANN 2.1.0beta first, and how?

  5. Permalink  ⋅ Reply


    September 21, 2015 at 4:10am

    birdmw@birdmw-thinkpad:~/Desktop/final_project/fann-2.1.0/python$ sudo python install
    Running SWIG before: swig -c++ -python pyfann/pyfann.i
    running install
    running build
    running build_py
    copying pyfann/ -> build/lib.linux-x86_64-2.7/pyfann
    running build_ext
    building ‘pyfann._libfann’ extension
    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DSWIG_COMPILE -I../src/include -I/usr/include/python2.7 -c pyfann/pyfann_wrap.cxx -o build/temp.linux-x86_64-2.7/pyfann/pyfann_wrap.o
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
    c++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector –param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/pyfann/pyfann_wrap.o ../src/doublefann.o -o build/lib.linux-x86_64-2.7/pyfann/
    /usr/bin/ld: ../src/doublefann.o: relocation R_X86_64_32S against `.rodata’ can not be used when making a shared object; recompile with -fPIC
    ../src/doublefann.o: error adding symbols: Bad value
    collect2: error: ld returned 1 exit status
    error: command ‘c++’ failed with exit status 1

    • Permalink  ⋅ Reply

      chiranjib patra

      June 9, 2018 at 11:24am

      you have to install these packages to work properly

      sudo apt-get install libfann2 libfann-dev

  6. Permalink  ⋅ Reply


    October 30, 2015 at 12:28pm

    Matthew, I found exactly the same problems as yours. What I did, I add “-fPIC” to CPPFLAGS and LDFLAGS at the Makefile in the fann root directory and also the Makefile in the src directory. Then everything was fine.

Leave a Reply

Your email will not be published. Name and Email fields are required.