Ubuntu 20.04でPython環境（と機械学習環境）を構築する

# 初めに
冗長になってきたが，慣れるまで繰り返す．慣れても時間が経つと忘れるので，その対策でもある．

参考: 

* [WindowsでPython環境を構築する](https://qiita.com/ShotaDeguchi/items/d2e08c135f2eebaa624b)
* [Ubuntu 16.04でPython環境を構築する](https://qiita.com/ShotaDeguchi/items/ce2b0c819a67fd718fda)

# 状況確認
```terminal
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal

$ python -V

Command 'python' not found, did you mean:

  command 'python3' from deb python3
  command 'python' from deb python-is-python3

$ python3 -V
Python 3.8.10
```

# pyenvの導入
以前と同じく，git経由でインストールする．
```terminal
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src
```
環境設定を行う．
```terminal
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
```
続いて，
```terminal
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
$ echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
$ echo 'eval "$(pyenv init - bash)"' >> ~/.profile
```
今回は問題無く進んだ（[以前](https://qiita.com/ShotaDeguchi/items/ce2b0c819a67fd718fda)，トラブルに見舞われたのが何故か分からない）．

最後にシェルを再起動して，pyenvを呼び出すことができることを確認した．
```terminal
$ exec $SHELL

$ pyenv
pyenv 2.5.5
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   --version   Display the version of pyenv
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   ...

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
```

# pyenvへpythonをインストール
dependenciesを用意しておく．
```terminal
$ sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
```
`3.8.10`をインストールする．
```terminal
$ pyenv install 3.8.10
Downloading Python-3.8.10.tar.xz...
-> https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tar.xz
Installing Python-3.8.10...
patching file Misc/NEWS.d/next/Build/2021-10-11-16-27-38.bpo-45405.iSfdW5.rst
patching file configure
patching file configure.ac
Installed Python-3.8.10 to /home/user_name/.pyenv/versions/3.8.10
```
確認する．
```terminal
$ pyenv versions
* system (set by /home/user_name/.pyenv/version)
  3.8.10
```
続いて，`3.10.10`, `3.12.10`もインストールし，
```terminal
$ pyenv install 3.10.10
Downloading Python-3.10.10.tar.xz...
-> https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tar.xz
Installing Python-3.10.10...
Installed Python-3.10.10 to /home/user_name/.pyenv/versions/3.10.10

$ pyenv install 3.12.10
Downloading Python-3.12.10.tar.xz...
-> https://www.python.org/ftp/python/3.12.10/Python-3.12.10.tar.xz
Installing Python-3.12.10...
Installed Python-3.12.10 to /home/user_name/.pyenv/versions/3.12.10
```
グローバルを`3.12.10`に固定する．
```terminal
$ pyenv versions
* system (set by /home/user_name/.pyenv/version)
  3.8.10
  3.10.10
  3.12.10

$ python -V
pyenv: python: command not found

The `python' command exists in these Python versions:
  3.8.10
  3.10.10
  3.12.10

Note: See 'pyenv help global' for tips on allowing both
      python2 and python3 to be found.

$ python3 -V
Python 3.8.10

$ pyenv global 3.12.10

$ pyenv versions
  system
  3.8.10
  3.10.10
* 3.12.10 (set by /home/user_name/.pyenv/version)

$ python -V
Python 3.12.10
```

# venvによる仮想環境構築
3.3以降のpythonには，venvが付いている．
```terminal
/00_test$ python -m venv .venv_test
/00_test$ source .venv_test/bin/activate
(.venv_test)/00_test$ pip install numpy scipy pandas matplotlib
```

# 機械学習環境の構築
相変わらず，気を遣う．

## 情報の取得
**NVIDIA GPU driver**
```terminal
$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module  535.183.01  Sun May 12 19:39:15 UTC 2024
GCC version:  gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) 
```
より，`NVIDIA GPU driver=535.183.01`（`cat /sys/module/nvidia/version`も同様）．

**CUDA toolkit**
```terminal
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jun_13_19:16:58_PDT_2023
Cuda compilation tools, release 12.2, V12.2.91
Build cuda_12.2.r12.2/compiler.32965470_0
```
より，`CUDA toolkit=12.2`（今回は，`cat /usr/local/cuda/version.json`もあり，同様）．

**cuDNN**
インストールされていなかったので，[ここ](https://developer.nvidia.com/cudnn)からインストールした．
```terminal
$ cat /usr/include/x86_64-linux-gnu/cudnn_v*.h | grep CUDNN_MAJOR -A 2
#define CUDNN_MAJOR 9
#define CUDNN_MINOR 8
#define CUDNN_PATCHLEVEL 0
--
#define CUDNN_VERSION (CUDNN_MAJOR * 10000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

/* cannot use constexpr here since this is a C-only file */
```
[以前](https://qiita.com/ShotaDeguchi/items/ce2b0c819a67fd718fda)とは違う場所であったが，`9.8.0`．

## tf
以上を整え，
```terminal
/01_tf$ python -m venv .venv_tf
/01_tf$ source .venv_tf/bin/activate
(.venv_tf)/01_tf$ pip install --upgrade pip
(.venv_tf)/01_tf$ pip install tensorflow[and-cuda]
```
を行うと，
```terminal
(.venv_tf)/01_tf$ python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
```
が`tf.Tensor`を，
```terminal
(.venv_tf)/01_tf$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
```
がlist of GPUsを返すので，良いだろう．

## torch
あと少しである．
```terminal
/02_torch$ python -m venv .venv_torch
/02_torch$ source  .venv_torch/bin/activate
(.venv_torch)/02_torch$ pip install --upgrade pip
(.venv_torch)/02_torch$ pip install torch torchvision torchaudio
```
を終え，
```terminal
(.venv_torch)/02_torch$ python
Python 3.12.10 (main, Apr 13 2025, 14:54:15) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'2.6.0+cu124'

>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.9414, 0.8425, 0.3497],
        [0.7794, 0.9285, 0.6651],
        [0.1550, 0.2662, 0.8811],
        [0.0167, 0.9312, 0.3614],
        [0.5002, 0.3822, 0.8820]])

>>> torch.cuda.is_available()
True
```
より，成功

# 終わりに
無事，終わった．