# Ubuntu22.04重灌筆記:GPU安裝與深度學習
###### tags: `Tutorial`, `Ubuntu`, `VScode`, `python`
目錄:
+ [重灌Ubuntu](#ubuntu)
+ [CUDA安裝](#cuda)
+ [Nvidia driver](#nvidia)
+ [CUDA Toolkit](#toolkit)
+ [cudnn](#cudnn)
+ [建立虛擬環境](#virtualenv)
+ [安裝python 3.9](#py39)
+ [終端機管理工具: tmux](#tmux)
其他筆記:
+ [VScode 使用 SSH 進行遠端程式開發](https://hackmd.io/@Chieh997/VScodeSSH)
+ [深度學習python套件安裝](https://hackmd.io/@Chieh997/deeppython): Install Pytorch, Tensorflow, Pyspark
+ [R in Ubuntu22.04](https://hackmd.io/@Chieh997/ubuntur): Install R and use with Jupyter Notebook and VScode
---
<a id='ubuntu'></a>
## 重灌Ubuntu
+ [Ubuntu 是什麼?如何透過 USB 安裝 Ubuntu 作業系統?完整教學指南](https://adersaytech.com/practical-software/ubuntu-install-tutorial.html)
### 製作重灌隨身碟
1. 前往[Rufus官方網站](https://rufus.ie/zh_TW/)並下載Rufus軟體

2. 前往[Ubuntu官方網站](https://ubuntu.com/download/desktop)下載桌面版的安裝檔(ISO)
3. 使用Rufus及安裝檔建立重灌用開機隨身碟

### 進入開機選單選擇USB開機
| | PC | Laptop |
|:--------:|:---:|:------:|
| Asus | F8 | Esc |
| Acer | F12 | F12 |
| HP | F9 | F9 |
| Dell | F12 | F12 |
| Gigabyte | F12 | F12 |
---
<a id='cuda'></a>
## CUDA安裝
+ [Ubuntu 20.04中安裝nvidia-driver-460版 & CUDA-11.4.2版 & cuDNN](https://medium.com/@scofield44165/ubuntu-20-04%E4%B8%AD%E5%AE%89%E8%A3%9Dnvidia-driver-cuda-11-4-2%E7%89%88-cudnn-install-nvidia-driver-460-cuda-11-4-2-cudnn-6569ab816cc5)
+ [Ubuntu 安裝 CUDA cuDNN pytorch tensorflow mxnet](https://mikethreeacer.medium.com/ubuntu-18-04-%E5%AE%89%E8%A3%9D-cuda-cudnn-anaconda-pytorch-1f170b3326a4#1b0a)
+ [Ubuntu16.04安装NVIDIA驱动+CUDA9.0+cuDNN7.0](https://blog.csdn.net/mtllyb/article/details/79505012)
+ [Ubuntu 22.04 for Deep Learning](https://gist.github.com/amir-saniyan/b3d8e06145a8569c0d0e030af6d60bea)
### 確認GPU資訊
```bash=0
sudo lshw -numeric -C display
```
<a id='nvidia'></a>
### Install Nvidia Driver
1. [清除既有nvidia driver和ppa](https://forums.developer.nvidia.com/t/newly-installed-drivers-are-not-found-when-nvidia-smi-is-called/82686/6)
```bash=+
sudo apt-get remove --purge '^nvidia-.*'
sudo apt-get --purge remove "*cublas*" "cuda*"
sudo apt-get --purge remove "*nvidia*"
sudo add-apt-repository --remove ppa:graphics-drivers/ppa
sudo rm /etc/X11/xorg.conf
sudo apt autoremove
sudo reboot
```
2. 列出支援的GPU driver版本
```bash=+
ubuntu-drivers devices
# or
ubuntu-drivers list
```
3. 安裝nvidia-driver並重新啟動
```bash=+
sudo apt-get update
sudo apt upgrade
sudo apt install nvidia-driver-{VERSION_NUMBER}
sudo reboot
```
4. 檢查nvidia driver版本
```bash=+
nvidia-smi
```
<a id='toolkit'></a>
### Install CUDA-Toolkit
+ [NVIDIA CUDA Installation Guide for Linux](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu-installation)
1. 依序執行官方網站中的指令,local或network都可以

+ Install [Latest CUDA](https://developer.nvidia.com/cuda-downloads) - 推薦使用network下載,較容易成功。
```bash=0
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda
```
+ Install Older CUDA (e.g. [11.7](https://developer.nvidia.com/cuda-11-7-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=deb_network)) - 建議用deb(local)安裝,大部分可沿用但須在最後一行指定CUDA版本
```bash=0
sudo apt-get install cuda-{11}-{7}
```
+ 安裝出現錯誤(`E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.`)的[解法](https://blog.csdn.net/weixin_42156097/article/details/103805063)
```bash=+
sudo apt-get install aptitude
sudo aptitude install cuda
sudo apt-get install cuda
```
2. `.bashrc`設定環境並刷新terminal
```bash
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
export PATH=$PATH:/usr/local/cuda/bin
```
```bash=+
source .bashrc
```
3. 檢查CUDA版本
```bash=+
nvcc -V
```
<a id='cudnn'></a>
### Install cuDNN
#### Method 1: [Latest Version Download](https://developer.nvidia.com/cudnn-downloads)
```bash=1
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cudnn
```
#### Method 2: [cuDNN Package Manager Installation](https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#cudnn-package-manager-installation-overview)
1. Enable the repository
```bash=1
# ${OS} = debian11, ubuntu1804, ubuntu2004, or ubuntu2204
wget https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/cuda-${OS}.pin
sudo mv cuda-${OS}.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/3bf863cc.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/ /"
sudo apt-get update
```
2. Install the [cuDNN library](https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/)
```bash=+
# ${cudnn_version} = 8.{x}.0.*
# ${cuda_version} = cuda11.{y}
sudo apt-get install libcudnn8=${cudnn_version}-1+${cuda_version}
sudo apt-get install libcudnn8-dev=${cudnn_version}-1+${cuda_version}
```
+ [How to verify CuDNN installation?](https://stackoverflow.com/questions/31326015/how-to-verify-cudnn-installation)
```bash=+
whereis cudnn_version.h
CUDNN_H_PATH=$(whereis cudnn_version.h)
cat ${CUDNN_H_PATH} | grep CUDNN_MAJOR -A 2
```
The result should be look like the following, which means the version is `{X}.{Y}.0`
```C
#define CUDNN_MAJOR {X}
#define CUDNN_MINOR {Y}
#define CUDNN_PATCHLEVEL 0
```
---
<a id='virtualenv'></a>
## 建立虛擬環境
### [導出舊環境](https://stackoverflow.com/questions/14684968/how-to-export-virtualenv)
先進入舊環境,使用下列指令將需要用到的package導出至`requirements.txt`
```bash=0
pip freeze > requirements.txt
```
### 安裝Virtualenv與Virtualenvwrapper
1. 更新系統安裝包
```bash=0
sudo apt-get update
sudo apt-get upgrade
```
2. 安裝pip
```bash=+
sudo apt-get install python3-pip
```
3. 安裝Virtualenv與Virtualenvwrapper
```bash=+
sudo apt-get install virtualenv
pip install virtualenvwrapper
```
#### Virtualenv使用方法
+ 常用指令
```bash
# 建立環境
virtualenv {env_name}
# 啟用環境
source ./{env_name}/bin/activate
# 離開環境
deactivate
```
### Virtualenvwrapper使用方法
+ 在啟動檔`.bashrc`中設定路徑與要求
```bash
# {path/to/python}可以用`which python`來查詢
export WORKON_HOME={path/to/env}
export VIRTUALENVWRAPPER_PYTHON={path/to/python}
# export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
# export WORKON_HOME=$HOME/.virtualenvs
# export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
# 依據安裝位置嘗試下列其中一行指令即可
source ~/.local/bin/virtualenvwrapper.sh
# source /usr/local/bin/virtualenvwrapper.sh
```
+ 執行啟動檔
```bash=0
source .bashrc
```
+ 常用指令
```bash
# 查詢環境
lsvirtualenv
# 建立環境
mkvirtualenv -p {python3.X} {venv_name}
# 刪除環境
rmvirtualenv {venv_name}
# 使用環境
workon {venv_name}
# 離開環境
deactivate
```
### 重新安裝套件
```bash=+
pip install -r {path/to/requirements.txt}
```
#### Reference
+ [Ubuntu 環境下安裝pip & python3](http://python-learnnotebook.blogspot.com/2018/10/ubuntu-pip-python3.html)
+ [Python3 - Virtualenv & Virtualenvwrapper 拯救骯髒的套件環境](https://medium.com/@minecola/python3-%E6%8B%AF%E6%95%91%E9%AA%AF%E9%AB%92%E7%92%B0%E5%A2%83%E5%A4%A7%E4%BD%9C%E6%88%B0-with-virtualenv-4b398142463d)
+ [Using different versions of python with virtualenvwrapper - StackOverflow](https://stackoverflow.com/questions/6401951/using-different-versions-of-python-with-virtualenvwrapper)
<a id='py39'></a>
## 安裝python3.9
1. 更新系統安裝包
```bash=0
sudo apt update
sudo apt install software-properties-common
```
2. 安裝`deadsnakes` PPA
```bash=+
sudo add-apt-repository ppa:deadsnakes/ppa
```
3. 安裝Python3.9
```bash=+
sudo apt install python3.9
```
4. 如果出現`No module named 'distutils.util'`的錯誤,安裝對應版本的`distutils`(可能需要再運行`python3.9 get-pip.py`)
```bash=+
sudo apt install python3.{X}-distutils
```
#### Reference
+ [How to Install Python 3.9 on Ubuntu 22.04 using APT](https://linuxhint.com/install-python-ubuntu-22-04/)
+ [How to install pip for Python 3.9 on Ubuntu 20.04 - StackOverflow](https://stackoverflow.com/questions/65644782/how-to-install-pip-for-python-3-9-on-ubuntu-20-04)
<a id='tmux'></a>
## 終端機管理工具:tmux
使用時機:
1. 可開啟多個terminal視窗(但對於VScode使用者較無影響)。
2. 若程式執行時間過長,避免ssh斷線後中斷運行,此工具可協助於背景運行程式,不受連線情形影響。
### Install tmux
```bash=0
sudo apt install tmux
```
Pane, window, session 等的差別與使用可參考 [Linux tmux 終端機管理工具使用教學](https://blog.gtwang.org/linux/linux-tmux-terminal-multiplexer-tutorial/)
### [Cheat Sheet](https://tmuxcheatsheet.com)
+ session
`tmux new -s {my_session}`: creat new session
`tmux ls`: show all sessions
`tmux a -t {my_session}`: attach to specific session
`Ctrl`+`b` `$`: rename when attach
`Ctrl`+`b` `d`: detach when attach
`Ctrl`+`b` `x` `y`: kill when attach