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.

Installing FANN with Python bindings on Ubuntu
Tagged on:             

7 thoughts on “Installing FANN with Python bindings on Ubuntu

  • 2011-06-14 at 19:31

    Really useful and straightforward. Thanks for sharing!

  • 2011-06-16 at 12:48

    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?

  • 2012-06-04 at 01:55

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

    and it works! 🙂

  • 2013-03-01 at 23:01

    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?

  • 2015-09-21 at 04:10

    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

    • 2018-06-09 at 11:24

      you have to install these packages to work properly

      sudo apt-get install libfann2 libfann-dev

  • 2015-10-30 at 12:28

    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 to john Cancel reply

Your email address will not be published. Required fields are marked *