GTX-TITAN 搭載PCで、Ubuntu 16.04 LTS, CUDA-8.0RC インストール備忘録

# 前提の環境

- Motherboard: GA-Z170N-Gaming 5 
- CPU: Intel Core-i5 6500
- GPU: GeForce GTX TITAN

# 参考にさせていたただいた記事

> GTX-1080 など GeForce を Ubuntu 16.04 LTS で CUDA-8.0RC と共に使う
> http://qiita.com/hidenorly/items/a3eb3a0375e6703d163b 

# 手順

### 必要パッケージのインストール

```bash
$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
$ sudo apt-get install nvidia-367
$ sudo apt-get install mesa-common-dev
$ sudo apt-get install freeglut3-dev
```

### ダウンロード

https://developer.nvidia.com/cuda-toolkit からダウンロード（要ログイン）
![image](https://qiita-image-store.s3.amazonaws.com/0/111730/2f7da00d-2134-5026-a382-15dbbd015d20.png)

cuda_8.0.27_linux.run を選択（1.4GB）
![image](https://qiita-image-store.s3.amazonaws.com/0/111730/1d44b081-b09a-a4ad-97dc-d5f2e3bac334.png)

### パス追加

`~/.bashrc` に以下を追加

    export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    export CUDA_HOME=/usr/local/cuda

### gcc 4.8 系に戻す

```bash
$ cd /usr/bin/
$ sudo unlink gcc
$ sudo ln -s gcc4.8 gcc
$ sudo unlink g++
$ sudo ln -s g++-4.8 g++
```

### X無しのモードで作業

`CTRL+ALT+F1`を押して黒画面へ。

ログイン後、
`sudo init 3` を実行して、非X環境してから、先ほどダウンロードしたインストーラを実行。

```bash
$ chmod +x cuda_8.0.27_linux-run
$ sudo sh ./cuda_8.0.27_linux-run
```

EULAが長いけど、`f` 連打。
最後に

    ===========
    = Summary =
    ===========
    
    Driver:   Installed
    Toolkit:  Installed in /usr/local/cuda-8.0
    Samples:  Installed in /home/lekto, but missing recommended libraries
    
    Please make sure that
     -   PATH includes /usr/local/cuda-8.0/bin
     -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root
    
    To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin
    To uninstall the NVIDIA Driver, run nvidia-uninstall
    
    Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.
    
    Logfile is /tmp/cuda_install_21782.log

とか出ればOK。

```bash
$ sudo reboot
```

### 再起動後、Xログインは出来ない

`CTRL+ALT+F1`を押して再び黒画面へ。
ログイン。

### gcc 5 系に戻す

```bash
$ cd /usr/bin/
$ sudo unlink gcc
$ sudo ln -s gcc-5 gcc
$ sudo unlink g++
$ sudo ln -s g++-5 g++
```
### ドライバーを再インストール

```bash
$ sudo apt-get install nvidia-367 --reinstall
```

こんな表示のはず。

```
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 274 not upgraded.
Need to get 0 B/69.7 MB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 212246 files and directories currently installed.)
Preparing to unpack .../nvidia-367_367.44-0ubuntu0~gpu16.04.1_amd64.deb ...
Stopping nvidia-persistenced
nvidia-persistenced: no process found
Done.
Removing all DKMS Modules
Done.
Stopping previous nvidia-persistenced
nvidia-persistenced: no process found
Done.
Unpacking nvidia-367 (367.44-0ubuntu0~gpu16.04.1) over (367.44-0ubuntu0~gpu16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up nvidia-367 (367.44-0ubuntu0~gpu16.04.1) ...
update-initramfs: deferring update (trigger activated)
INFO:Enable nvidia-367
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/put_your_quirks_here
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/lenovo_thinkpad
DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/dell_latitude
Loading new nvidia-367-367.44 DKMS files...
Building only for 4.4.0-34-generic
Building for architecture x86_64
Building initial module for 4.4.0-34-generic
Done.

nvidia_367:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-34-generic/updates/dkms/

nvidia_367_modeset.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-34-generic/updates/dkms/

nvidia_367_drm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-34-generic/updates/dkms/

nvidia_367_uvm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-34-generic/updates/dkms/

depmod....

DKMS: install completed.
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for initramfs-tools (0.122ubuntu8) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-34-generic
W: Possible missing firmware /lib/firmware/i915/skl_guc_ver6.bin for module i915_bpo

```

リブート後はX環境でログインできるはず

```bash
$ sudo reboot
```

# 補足

16.04 のとってきたISOの問題か、デフォルトでロケールが日本語になってた。

```bash
$ localectl
   System Locale: LANG=ja_JP.UTF-8
       VC Keymap: n/a
      X11 Layout: jp
```
コンソールまで日本語だとつらい？し、ググりづらいので英語に。

```bash
$ sudo update-locale LANG=en_US.UTF-8
$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: n/a
      X11 Layout: jp
```