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 cuda_8.0.44_linux.run
$ sudo ./cuda_8.0.44_linux.run


  • 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]


  • CAUTION !


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.  


[global]
device=gpu
floatX=float32
root=/usr/local/cuda-8.0
[nvcc]
fastmath = True
[blas]
ldflags = -lopenblas


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


$ sudo gedit testing_theano.py


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


$ python testing_theano.py


  • 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
 1.62323296]
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.

16 comments:

  1. I am trying to use this guide, but I am stuck at 4. I hope you can help!

    when I kill x server, I cannot see anything. Instead of text mode, I just have a blank screen. What shall I do?!

    ReplyDelete
    Replies
    1. You should “ctrl + alt + F1” again in order to login again.

      Delete
  2. This allowed by to use tty as listed above:

    http://askubuntu.com/questions/487868/no-tty-after-installing-nvidia-driver

    ReplyDelete
    Replies
    1. Sorry, I checked this comments too late. Glad you figured out.

      Delete
  3. This was very helpful, thank you! I would just mention one thing, and that is in step 10 "nvcc -v" causes an error. I believe that the option should be uppercase, i.e. "nvcc -V". Otherwise, great work! You saved me a great deal of time and frustration.

    ReplyDelete
    Replies
    1. That's my huge pleasure. Glad I helped someone on Earth. I fixed "nvcc -V" thing right away. Really appreciate your correction and sorry for causing you a problem with typo. :)

      Delete
  4. I findally did it! Thank you very much!

    ReplyDelete
  5. When i try to install cuda it gives me a an error message about "kernel" when installing nvidia card drivers ?

    ReplyDelete
    Replies
    1. Could you please copy and paste the error message you've got ? That will be more easy to figure out your problem.

      Delete
  6. 와 신기하다 구글링하다가 여기까지 흘러들어왔네 ㅋㅋㅋㅋ 오늘 리눅스 또 죽어서 다시 깔아야 댐.... 벌써 몇번째인지 모르겠음..

    14. DO NOT update any system updates until you are very assure you can re-set up every above steps when those little freaks down your system entirely

    이거 추가해야함... ㅠㅠ

    ReplyDelete
  7. great article, thank you so much.

    After spending 2 weeks crushing my system,
    I found tour article and
    manage to install cuda & Theano.

    Great job !!!

    ReplyDelete
  8. I want to install Anaconda and ipython.
    can i do that after all the above process,
    or do I need to uninstall python or something ?

    ReplyDelete
    Replies
    1. Once I used Anaconda package. I'm not hundred percent sure but you might be able to install Anaconda package and run theano library with Anaconda. However, Anaconda doesn't support virtualenv. That's why I quit using Anaconda. Anaconda is great, but sometimes I don't want to stick with the default setting from the conda package. Having said that, Anaconda is still a good choice. This is just my own preference.

      Delete
  9. Thanks a lot! Managed to install it after failing so many times using other instructions.
    Ubuntu 14.04 LTS
    GTX 750 Ti
    Cuda-8.0

    ReplyDelete
  10. Nice post man keep it up Driver toolkit is 100% working software.

    ReplyDelete

When terminal is not starting in Ubuntu

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