備忘録

python

Ubuntu18.04セットアップ、nvidiaドライバーからdockerまで

GPUを使った機械学習用マシンをセットアップします。

まずはUbuntuインストールからnvidiaのドライバーインストールまで

 

ubuntu 18.04 GUIで最小インストール

 

# sshサーバーインストール、設定
$ sudo apt install ssh

sshセットアップ後はリモートのsshクライアントから操作します。

 

ヘッドレス運用なのでCUI起動に設定
~$ sudo systemctl set-default multi-user.target
[sudo] xxx のパスワード:
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.

~$ sudo reboot

 

参考

CUI起動設定(GUI->CUI
$ sudo systemctl set-default multi-user.target

GUI起動設定(CUI->GUI
$ sudo systemctl set-default graphical.target

 

Ubuntuを最新状態に更新する。

~$ sudo apt update
~$ sudo spt upgrade
~$ sudo reboot

 

NVIDIAドライバーインストール

 

はじめにインストール時に設定されているnouveauドライバーを無効化する。

nvidiaドライバーインストール時に無効化するか聞いてくるので必要ないかもだが念のため。

nouveauの状態確認

~$ lsmod | grep nouveau

nouveau 1851392 0
mxm_wmi 16384 1 nouveau
ttm 110592 1 nouveau
drm_kms_helper 172032 1 nouveau
drm 458752 3 drm_kms_helper,ttm,nouveau
i2c_algo_bit 16384 1 nouveau
wmi 24576 4 intel_wmi_thunderbolt,wmi_bmof,mxm_wmi,nouveau
video 45056 1 nouveau

nouveauを無効化
~$ sudo vi /etc/modprobe.d/blacklist-nouveau.conf

最終行に追記 (ファイルがない場合は新規作成)
blacklist nouveau
options nouveau modeset=0

設定の有効化
~$ sudo update-initramfs -u
update-initramfs: Generating /boot/initrd.img-4.18.0-18-generic

~$ sudo reboot

 

nouveau無効化を確認
:~$ lsmod | grep nouveau
何も返ってこないので無効化されている。

搭載カードの確認(RTX2080ti)
~$ lspci | grep VGA

01:00.0 VGA compatible controller: NVIDIA Corporation GV102 (rev a1)

 

ドライバーインストールに必要なパッケージをインストール。

これをインストールしておかないと後々怒られる。 

~$ sudo apt-get install linux-headers-generic linux-source build-essential

 

NVIDIAのサイトでドライバーダウンロードURLを確認
https://www.nvidia.co.jp/Download/Find.aspx?lang=jp&QNF=1

確認したURLからドライバーをダウンロード

~$ wget http://jp.download.nvidia.com/XFree86/Linux-x86_64/418.56/NVIDIA-Linux-x86_64-418.56.run

インストールファイルに実行権限を付与し実行
~$ sudo chmod +x NVIDIA-Linux-x86_64-418.56.run
~$ sudo ./NVIDIA-Linux-x86_64-418.56.run

 

インストール時のエラーメッセージ

The distribution-provided pre-install script failed! Are you sure you want to continue?
Abort installation

ERROR: Installation has failed.
Please see the file '/var/log/nvidia-installer.log' for details.
You may find suggestions on fixing installation problems in the README
available on the Linux driver download page at www.nvidia.com.

 

 

エラーログを確認
~$ vi /var/log/nvidia-installer.log

nvidia-installer log file '/var/log/nvidia-installer.log'
creation time: Tue Apr 30 13:53:15 2019
installer version: 418.56

PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

nvidia-installer command line:
./nvidia-installer

Unable to load: nvidia-installer ncurses v6 user interface

Using: nvidia-installer ncurses user interface
-> Detected 16 CPUs online; setting concurrency level to 16.
-> Installing NVIDIA driver version 418.56.
-> Running distribution scripts
executing: '/usr/lib/nvidia/pre-install'...
-> done.
-> The distribution-provided pre-install script failed! Are you sure you want to continue? (Answer: Abort installation)
ERROR: Installation has failed. Please see the file '/var/log/nvidia-installer.log' for details. You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com.

 

スクリプトを確認
~$ vi /usr/lib/nvidia/pre-install
------------------
#!/bin/sh

# Trigger an error exit status to prevent the installer from overwriting
# Ubuntu's nvidia packages.

exit 1
-------------------
"exit 1":エラー終了になっている
インストール時は"exit 0"に変更しておく

 

インストラーを再実行

~$ sudo ./NVIDIA-Linux-x86_64-418.56.run

The distribution-provided pre-install script completed successfully. If this is the first time you have run the installer, this script may have helped
disable Nouveau, but a reboot may be required first. Would you like to continue, or would you prefer to abort installation to reboot the system?

"Continue installation"

nouveauはすでに無効化しているので問題なし。インストールを継続する。

またエラー発生

ERROR:
Unable to find the development tool `cc` in your path;
please make sure that you have the package 'gcc' installed.
If gcc is installed on your system, then please check that `cc` is in your PATH.

ERROR:
Installation has failed.
Please see the file '/var/log/nvidia-installer.log' for details.
You may find suggestions on fixing installation problems in the README
available on the Linux driver download page at www.nvidia.com.

 エラーログを確認

~$ vi /var/log/nvidia-installer.log

nvidia-installer log file '/var/log/nvidia-installer.log'
creation time: Tue Apr 30 14:02:35 2019
installer version: 418.56

PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

nvidia-installer command line:
./nvidia-installer

Unable to load: nvidia-installer ncurses v6 user interface

Using: nvidia-installer ncurses user interface
-> Detected 16 CPUs online; setting concurrency level to 16.
-> Installing NVIDIA driver version 418.56.
-> Running distribution scripts
executing: '/usr/lib/nvidia/pre-install'...
-> done.
-> The distribution-provided pre-install script completed successfully. If this is the first time you have run the installer, this script may have helped disable Nouveau, but a reboot may be required first. Would you like to continue, or would you prefer to abort installation to reboot the system? (Answer: Continue installation)
ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the package 'gcc' installed. If gcc is installed on your system, then please check that `cc` is in your PATH.
ERROR: Installation has failed. Please see the file '/var/log/nvidia-installer.log' for details. You may find suggestions on fixing installation problems in the README available on the Linux driver download page at www.nvidia.com. 

ビルド関連のパッケージが不足しているのインストール

すでに上で実行している場合は必要なし。

~$ sudo apt-get install linux-headers-generic linux-source build-essential

 

インストーラー再実行

また警告

The CC version check failed:
The kernel was built with gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3), but the current compiler version is cc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0.
This may lead to subtle problems; if you are not certain whether the mismatched compiler will be compatible with your kernel,
you may wish to abort installation, set the CC environment variable to the name of the compiler used to compile your kernel,
and restart installation.


WARNING:
Unable to find a suitable destination to install 32-bit compatibility libraries.
Your system may not be set up for 32-bit compatibility.
32-bit compatibility files will not be installed;
if you wish to install them, re-run the installation and set a valid directory with the --compat32-libdir option.

32bit互換ライブラリは必要なので無視する。インストールを継続

またワーニング

An incomplete installation of libglvnd was found. All of the essential libglvnd libraries are present,
but one or more optional components are missing.
Do you want to install a full copy of libglvnd? This will overwrite any existing libglvnd libraries.

Install and overwrite existing files 

上書きして問題なし。

次は選択

Would you like to run the nvidia-xconfig utility to automatically update your X configuration file
so that the NVIDIA X driver will be used when you restart X?
Any pre-existing X configuration file will be backed up.

No 

「Xの設定を更新するか?」聞いてくるがNoを選択。
nvidia-xconfigコマンドを使えばいつでも変更できる。

インストール完了メッセージ

Installation of the NVIDIA Accelerated Graphics Driver for Linux-x86_64 (version: 418.56) is now complete.
Please update your xorg.conf file as appropriate; see the file /usr/share/doc/NVIDIA_GLX-1.0/README.txt for details.

これでインストール完了

 

dockerのインストール

入ってないと思うが旧バージョンdockerの削除。
$ sudo apt-get purge docker lxc-docker docker-engine docker.io

必要なパッケージのインストール
~$ sudo apt install curl apt-transport-https ca-certificates software-properties-common

~$ dpkg -l | grep docker
~$ dpkg -l | grep runc
~$ dpkg -l | grep containerd

~$ sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

 

~$ sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ 不明 ] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]

 

~$ sudo add-apt-repository \
> "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
> $(lsb_release -cs) stable"

 

~$ sudo apt update
~$ sudo apt install docker-ce docker-ce-cli containerd.io

 

nvidia-docker2インストール

公式サイトのまんま実行

https://github.com/NVIDIA/nvidia-docker

パッケージリポジトリの追加
~$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
~$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
~$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
~$ sudo apt-get update

nvidia-docker2をインストール。Docker daemonの設定再読込
~$ sudo apt-get install -y nvidia-docker2
~$ sudo pkill -SIGHUP dockerd

最新の公式CUDAイメージを使ってnvidia-smiを実行
~$ docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi

Unable to find image 'nvidia/cuda:10.0-base' locally
10.0-base: Pulling from nvidia/cuda
898c46f3b1a1: Pull complete
63366dfa0a50: Pull complete
041d4cd74a92: Pull complete
6e1bee0f8701: Pull complete
112097260ef3: Pull complete
30a67c795176: Pull complete
0d286a7b6e12: Pull complete
Digest: sha256:faac85a7d28e086173915df6456784778c4dacb429ff067def0c4a12671240e8
Status: Downloaded newer image for nvidia/cuda:10.0-base
Tue Apr 30 15:05:34 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.56 Driver Version: 418.56 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| 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 RTX 208... Off | 00000000:01:00.0 On | N/A |
| 35% 33C P8 18W / 260W | 732MiB / 10981MiB | 0% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+

できた。

anaconda5.3でOpenCVを使う

ネット上にはいろいろなインストール方法の情報が氾濫していますが、シンプルにcondaのみでインストールできます。

 

OpenCV3.4のための仮想環境をpython3.7ベースで作成

$ conda create --name cv-py37 python=3.7

$ conda install opencv

 

cv2をインポートできるか確認

$ python

Python 3.7.0 (default, Jun 28 2018, 07:39:16)

[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2

>>>

これでおしまい

 

 

pyenvを削除

いろいろやった結果、機械学習の勉強レベルではpyenvはない方がスムーズでした。

pyenv本体を削除(アンインストール)します。

 

まずpyenvの仮想環境を削除

$ pyenv uninstall anaconda3-5.2.0

pyenv: remove /Users/as/.pyenv/versions/anaconda3-5.2.0

 

brewでインストールしていたので

$ brew uninstall pyenv

Uninstalling /usr/local/Cellar/pyenv/1.2.7... (608 files, 2.4MB)

 

.bash_profile 内のpyenvに関連する以下の項目を削除

# for pyenv

eval "$(pyenv init -)"

# for conda

. /Users/xxxxx/.pyenv/versions/anaconda3-5.2.0/etc/profile.d/conda.sh

Anancondaの完全削除(アンインストール)方法

macOS 10.13.6 (17G65)

Anaconda3 5.2.0

 

$ conda install anaconda-clean

$ anaconda-clean --yes

$ python --version

Python 3.6.5

$rm -rf Anaconda3

 

$ open .bash_profile

# added by Anaconda3 5.2.0 installer
# export PATH="/anaconda3/bin:$PATH"

 

$ python --version

Python 2.7.10