プログラムを中心とした個人的なメモ用のブログです。 タイトルは迷走中。
内容の保証はできませんのであしからずご了承ください。

2022/03/30

Ubuntu 20.04 に古い NVIDIA Driver と CUDA をインストール

update2022/03/31 event_note2022/03/30 8:22

古いグラフィックボードが手に入ったので、それに対応した NVIDIA Driver および CUDA をインストールしたときのメモです。

環境

  • Ubuntu 20.04
  • GPU:NVIDIA Quadro 2000

NVIDIA Driver のインストール

以下のコマンドでインストールするドライバーのバージョンがわかります。

$ ubuntu-drivers devices

または、以下のページで必要なドライバーのバージョンを調べることもできます。

以上より必要なドライバーは 390 だとわかりました。
ドライバーは上記のページからダウンロードできますが、Ubuntu の場合は apt でインストールしたほうが楽です。

$ sudo apt -y install nvidia-driver-390

一度再起動し、nvidia-smi でドライバーが正しくインストールされたか確認します。

$ nvidia-smi
Tue Mar 15 13:51:47 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.144                Driver Version: 390.144                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Quadro 2000         Off  | 00000000:01:00.0  On |                  N/A |
| 30%   48C   P12    N/A /  N/A |    577MiB /   964MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1193      G   /usr/lib/xorg/Xorg                           141MiB |
|    0      2102      G   /usr/lib/xorg/Xorg                           220MiB |
|    0      2276      G   /usr/bin/gnome-shell                         206MiB |
+-----------------------------------------------------------------------------+

CUDA のインストール

NVIDIA Driver と CUDA のバージョンの関係は以下で確認できます。

今回インストールした NVIDIA Driver は 390.144 なので、CUDA は 9.1 インストールします。

CUDA のインストール方法ですが、以下でプラットフォームに応じた方法を調べることができます。

Ubuntu 20.04 では以下が表示されました。

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
$ sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
$ sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
$ sudo apt-get update
$ sudo apt-get -y install cuda

しかし、このコマンドでは CUDA のバージョンが指定されていないため、実行してしまうと最新の CUDA がインストールされてしまいます。
なので、次に示すように、バージョン 9.1 を指定してインストールしてやります。

CUDA 9.1 のインストール

CUDA 9.1 をインストールしたいところですが、apt でインストールできる CUDA の一覧を見てみても 9.1 はありません。
なので、以下を参考に Ubuntu 16.04 用のパッケージをインストールしました。

$ curl -sL http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub | sudo apt-key add

$ wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
$ sudo apt update

$ sudo apt --fix-broken install
$ sudo apt install cuda-toolkit-9-1
$ sudo reboot
$ rm cuda-repo-ubuntu1604_9.1.85-1_amd64.deb

注意点として、apt で cuda-9-1 をインストールすると NVIDIA Driver も最新に更新されてしまうので、cuda-toolkit-9-1 をインストールする必要があります。

インストールが完了したら、~/.bashrc の末尾に以下を追記します。

export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

ここで再起動します。

CUDA バージョンの確認

nvidia-smi で確認できるはずなのですが、私の環境では何も表示されました。
ただ、nvcc では確認できたので、インストールはされているんじゃないかと。

$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85

また、以下でも確認できます。

$ cat /usr/local/cuda/version.txt
CUDA Version 9.1.85

CUDA のインストール後に NVIDIA Driver がおかしくなってしまった場合

前述したように、NVIDIA Driver と CUDA のバージョンが合っていなければ動作がおかしくなります。

以下を参考に NVIDIA Driver と CUDA を削除してからインストールをやり直す必要があります。

$ sudo apt-get --purge remove nvidia-*
$ sudo apt-get --purge remove cuda-*

docker コンテナ内で GPU が使えるか確認しようとして諦めた

せっかく CUDA がインストールできたので、機械学習で GPU が使えそうか確認します。
まず pytorch を使い、ホスト上で以下のコードで確認してみました。

import torch
print(torch.cuda.is_available())

すると、以下のようにエラーが表示されました。

UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 9010). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver. (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:115.)
  return torch._C._cuda_getDeviceCount() > 0

PyTorch のバージョンも CUDA に合わせる必要があるようです。
以下のページを参考に pytorch のバージョンを指定してインストールしてみます。

すると以下のエラーが表示されました。

$ pip3 install torch==1.1.0 torchvision==0.3.0 -f https://download.pytorch.org/whl/cu90/torch_stable.html
Looking in links: https://download.pytorch.org/whl/cu90/torch_stable.html
/usr/share/python-wheels/urllib3-1.25.8-py2.py3-none-any.whl/urllib3/connectionpool.py:999: InsecureRequestWarning: Unverified HTTPS request is being made to host '172.20.16.253'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ERROR: Could not find a version that satisfies the requirement torch==1.1.0 (from versions: 1.4.0, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.8.1, 1.9.0, 1.9.1, 1.10.0, 1.10.1, 1.10.2, 1.11.0)
ERROR: No matching distribution found for torch==1.1.0

ここらへんから面倒になり、先に docker コンテナを作ることにしました。
まずは NVIDIA Container Toolkit をインストールしようと以下のページを見ると、

動作要件が以下のようになっていました。

  1. GNU/Linux x86_64 with kernel version > 3.10
  2. Docker >= 19.03 (recommended, but some distributions may include older versions of Docker. The minimum supported version is 1.12)
  3. NVIDIA GPU with Architecture >= Kepler (or compute capability 3.0)
  4. NVIDIA Linux drivers >= 418.81.07 (Note that older driver releases or branches are unsupported.)

というわけでここで諦めました。