It took me several days and trying different methods including a docker image (which didn't work) to figure out how can I solve it:
install the latest compatible driver from ubuntu->software & update->Additional Drivers-> Using NVIDIA binary driver (for my case the latest compatible was 340.107). Make sure nvidia-smi works successfully.
install gcc-4.7 and g++-4.7 by adding the following lines to /etc/apt/sources.list and then running sudo apt update && sudo apt install g++-4.7 gcc-4.7
deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe
the above lines coud be removed from /etc/apt/sources.list after that
if you have previously installed a newer version of gcc and g++, remove gcc and g++ which are symbolic links from your $PATH and create new sybolic links to newly installed gcc-4.7 and g++-4.7 (e.g sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc)
install required libraries by: apt-get install libglu1-mesa libxi-dev libxmu-dev libglu1-mesa-dev
Download cuda_6.5.14_linux_64.run from NVIDIA website and extract it by: ./cuda_6.5.14_linux_64.run --tar mxvf
copy InstallUtils.pm file from the extracted package and copy it to your perl-base location (for mine it was /usr/lib/x86_64-linux-gnu/perl-base)
set your perl library path (e.g export PERL5LIB=/usr/lib/x86_64-linux-gnu/perl-base)
then install the cude toolkit from the extracted package (sudo sh cuda-linux64-rel-6.5.14-18749181.run)
after installation and setting the PATH and LD_LIBRARY_PATH you sould be able to compile and run cuda samples.
a simple example you can find in cuda-samples/0_Simple/simplePrintf and compile it by running make command. Following was the output that I got:
GPU Device 0: "Quadro FX 5800" with compute capability 1.3
Device 0: "Quadro FX 5800" with Compute 1.3 capability
cuPrintf() is called. Output:
[1, 0]: Value is:10
[1, 1]: Value is:10
[1, 2]: Value is:10
[1, 3]: Value is:10
[1, 4]: Value is:10
[1, 5]: Value is:10
[1, 6]: Value is:10
[1, 7]: Value is:10
[3, 0]: Value is:10
[3, 1]: Value is:10
[3, 2]: Value is:10
[3, 3]: Value is:10
[3, 4]: Value is:10
[3, 5]: Value is:10
[3, 6]: Value is:10
[3, 7]: Value is:10
[2, 0]: Value is:10
[2, 1]: Value is:10
[2, 2]: Value is:10
[2, 3]: Value is:10
[2, 4]: Value is:10
[2, 5]: Value is:10
[2, 6]: Value is:10
[2, 7]: Value is:10
[0, 0]: Value is:10
[0, 1]: Value is:10
[0, 2]: Value is:10
[0, 3]: Value is:10
[0, 4]: Value is:10
[0, 5]: Value is:10
[0, 6]: Value is:10
[0, 7]: Value is:10