Thursday, July 16, 2015

CUDA 8.0 and Theano Setup in Ubuntu linux for Deep learning

< CUDA & Theano installation >
TIPS that I’ve realized so far !

4/MAR/2017 Update: updated CUDA 7.5 to CUDA 8.0 and added trouble shooting section.

Before I forget the knowledge and tips from this massive hassle, I think I should write down something right now.

     0. The BEST way to install CUDA&Theano in a nutshell.

  1. Install UBUNTU 14.04. (UBUNTU 16.04)
  2. Download RUN version of CUDA 8.0 toolkit.
  3. Disable Nouveau driver.
  4. Stop X server.
  5. Reboot.
  6. Install full version of CUDA 8.0 toolkit. (Driver, Toolkit, Samples)
  7. Edit .bashrc file to include PATH and LD_LIBRARY_PATH.
  8. Reboot.
  9. Install and update G++/GCC compiler.
  10. Compile CUDA sample and test deviceQuery.  
  11. install Numpy,Scipy,Pip, and Theano
  12. Install OpenBlas.
  13. Make .theanorc file.
  14. Test Theano library.

  1. Install UBUNTU 14.04.

  • Ubuntu is highly recommended for beginners since it has tons of troubleshooting articles on the web. You would find information or guides about Ubuntu easier than any other type of Linux systems.

  • I’m not gonna cover the Installation process of Ubuntu 14.04 here. Instead, you will be able to easily find it by googling it and tons of blog postings are covering Ubuntu installation issues.

  • I also recommend you to install “Anaconda” library. This is one of the easiest way to get all the dependencies that you need to run theano library.

  1. Download RUN version of CUDA 8.0 toolkit.

  • DO NOT download NVIDIA graphics driver from NVIDIA graphics driver download webpage. This will cause a login error. I believe this page (NVIDIA graphics driver download) is solely purposed to support graphic oriented tasks. Forget this page. Don't even click it.

  • Instead of that, refer to this page (CUDA toolkit download). There are two options. You can either select DEB installation or RUN file installation. I recommend RUN file installation because you can choose to opt out of installing the bundle driver and sample files.

  • According to various sources, some people suffer from NVIDIA graphics driver issue so that they should install specific NVIDIA driver which is not included in CUDA toolkit package. However, in my case, that was not the case. Both of my desktop and laptop system didn’t work with seperate driver. Having said that, I metion this issue because some of readers might suffer from this issue. If your system is not working with driver included in CUDA toolkit, refer to this page.

  1. Disable Nouveau driver.

  • I’ve struggled with this issue for ages. The thing is, most of the QNA pages on the web omit this process and I could not find out why does my linux system always show black screen after I reboot the system.

  • This was because of the “Nouveau” driver that crashes with NVIDIA driver. To nip this in the bud before it ruins your mental health, I suggest you a fancy solution with this issue.

  • We have to block Nouveau driver before it crashes with NVIDIA graphics driver. Type the following command.
$ sudo gedit /etc/modprobe.d/blacklist.conf

  • Then, add the following commands to block Nouveau driver.

blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

# for other linux distributions
blacklist vga16fb
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

  1. Stop X server.

  • You should kill X server (Ubuntu GUI system) to install graphics related drivers. “ctrl + alt + F1” will lead you to text mode. Type the following command.

$ sudo service lightdm stop

  1. Install full version of CUDA 8.0 toolkit. (Driver, Toolkit, Samples)

  • Go to the folder that you downloaded *.run file. Type the following command.

$ chmod a+x
$ sudo ./

  • After browsing a long instruction article, you will encounter a few questions as followings. Please carefully read the questions and type your answers as the following.

Do you accept the previously read EULA? (accept/decline/quit): accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 346.46? ((y)es/(n)o/(q)uit): y
Do you want to install the OpenGL libraries? ((y)es/(n)o/(q)uit) [ default is yes ]: n

Do you want to run nvidia-xconfig? This will update the system X configuration file so that the NVIDIA X driver is used. The pre-existing X configuration file will be backed up. This option should not be used on systems that require a custom X configuration, such as systems with multiple GPU vendors. (y)es/(n)o/(q)uit [ default is no ]: n

Install the CUDA 8.0 Toolkit? ((y)es/(n)o/(q)uit): y

Enter Toolkit Location [ default is /usr/local/cuda-8.0 ]: [Enter]

Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): y

Install the CUDA 8.0 Samples? ((y)es/(n)o/(q)uit): y

Enter CUDA Samples Location [ default is /root ]: [Enter]


DO NOT install OpenGL library. This might cause an error with your graphic driver.  

  1. Reboot.

  • Reboot your system with the following command.

$ sudo reboot

  1. Edit .bashrc file to include PATH and LD_LIBRARY_PATH.

  • If you don’t enroll the directory contains CUDA toolkit, you won’t be able to compile any code based on GPU programming. Add the following commands to ~/.bashrc file.

  • CAUTION : In Ubuntu, there is no file such as ~/.bash_profile. You should edit ~/.bashrc file.

$ sudo gedit ~/.bashrc

  • Once you see the gedit screen, type the following commands, and save it.
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

  1. Reboot.

  • Reboot your system with the following command.

$ sudo reboot

  1. Install and update G++/GCC compiler.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
$ gcc -v
$ make -v

  1. Compile CUDA sample and test deviceQuery.  

  • This process will check whether  your CUDA library is correctly installed in your Ubuntu system.

~$ cd NVIDIA_CUDA-8.0_Samples
~/NVIDIA_CUDA-8.0_Samples$ make

~/NVIDIA_CUDA-8.0_Samples$ cd bin/x86_64/linux/release
~/release$ ./deviceQuery

  • Another easy way to check out whether your NVIDIA driver is in shipshape manner, you can use the following two commands.

$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2016 NVIDIA Corporation Built on Sun_Sep__4_22:14:01_CDT_2016 Cuda compilation tools, release 8.0, V8.0.44

$ nvidia-smi
Tue Jul 14 19:50:26 2015       
| NVIDIA-SMI 346.46     Driver Version: 346.46         |                       
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|   0  GeForce 840M        Off  | 0000:0A:00.0     N/A |                  N/A |
| N/A   50C    P0    N/A /  N/A |      6MiB /  2047MiB |     N/A      Default |
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|    0            C+G   Not Supported                                         |

  • DO NOT TYPE “sudo apt-get install nvidia-cuda-toolkit”. This command is outdated one and this will lead you to CUDA 5.5 version. And it won’t work either. So, please follow the instruction I’ve written above.

  • You have to be sure with the above two commands. If there is any command that doesn’t pop out the above messages, that indicates that you have a problem with your NVIDIA CUDA driver and compiler.

  1. Install Numpy,Scipy,Pip and Theano

  • Now, we have to install python libraries that is necessary for Theano library. Type the following.

$ sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ git

  • Once you install pip installer, you can easily install Theano library with the following command.

$ sudo pip install Theano

  1. Install OpenBlas.

$ sudo apt-get install libopenblas-dev

  1. Make .theanorc file.

  • “.theanorc” file contains a few configuration for theano library. You should create theanorc file by typing the following command.

$ sudo gedit ~/.theanorc

  • In the Gedit edit window, type the followings and save the file.  

fastmath = True
ldflags = -lopenblas

  1. Test Theano library.
  • Go to any directory type the following to make a python file.

$ sudo gedit

  • Copy and paste this code. Save “” file.

$ python

  • If you can get the messages similar to the following messages, Theano library is successfully installed in your computer.

  • Check out if you are getting the message “Using cpu device”. If you get “cpu” message, you should fix your configuration about CUDA and Theano library.
Using gpu device 0: GeForce 840M
[GpuElemwise{exp,no_inplace}(), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.03167891502 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
Used the gpu
CUDA test has been finished

< Trouble Shooting (Updated 4/Mar/2017) >
  • If you are getting a message that says:
"The driver installation is unable to locate the kernel source. Please  make sure that the kernel source packages are installed and set up correctly. If you know that the kernel source packages are installed
 and set up correctly, you may pass the location of the kernel source."
$ sudo apt-get install dkms fakeroot build-essential linux-headers-generic

This was the only command that has solved the problem. 

  •  IF you are getting a message like this: 
"It appears that an X server is running. Please exit X before installation. If you're sure that X is not running, but are getting this error, please delete any X lock files in /tmp."

This means you did not successfully shut down the X. Go back to the step 4 and make sure you surely shut down the X-server(GUI). If you are seeing the Ubuntu GUI, that means you are still having X-server on. 

If you have any question, please mail me to the following address.

When terminal is not starting in Ubuntu

Untitled When terminal in Ubuntu is not starting gnome-termial crashes with python 3.6 update. Go to /usr/bin/gnome-termi...