# Install Python on Linux ## 対象 基本的にLinuxをインストールした際,Pythonもインストールされる
しかし,別途インストールが必要になることがある - 最新もしくは古いバージョンを使いたい - Dockerでコンテナを立ち上げた場合はPyhonが入っていない これらのためにPythonをインストールする方法を書く ## インストール方法の種類 古いバージョンをインストールしたいなど,要求によって方法が変わる |方法|特徴| |--------------------------------|------------------------------------------------------------| |aptでインストール|簡単!!だが,最新ではない.
OSのリリース時期によってバージョンは異なる| |リポジトリを追加してapt|リポジトリを追加することで(最新 or 古い)バージョンを利用することが可能| |公式配布HPとmake|公式から好きなバージョンを選べるが,コンパイルが必要,アンインストールが面倒?| ## aptでインストール ```bash $ sudo apt update $ sudo apt install -y python3 python3-pip ``` ## リポジトリを追加してapt リポジトリのPPA description : [https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa)
Supported Ubuntu and Python Versions - Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.11 - Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.11 - Ubuntu 22.04 (jammy) Python3.7 - Python3.12 - latest infomation: [https://github.com/deadsnakes/](https://github.com/deadsnakes/) #### 3.7 ~ ```bash # 3.10 $ sudo apt update $ sudo apt install software-properties-common $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt update $ sudo apt install -y python3.10 $ curl https://bootstrap.pypa.io/pip/get-pip.py -o get-pip.py $ python3.10 get-pip.py $ python3.10 -m pip install -r requirements.txt ``` #### 例外:2.6 ~ 3.6 古いバージョンはpipのインストールが特殊 URLでバージョン指定が必要 `https://bootstrap.pypa.io/pip/{version}/get-pip.py -o get-pip.py` ```bash # 3.6 $ sudo apt update $ sudo apt install software-properties-common $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt update $ sudo apt install -y python3.6 python3.6-distutils $ curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py $ python3.6 get-pip.py $ python3.6 -m pip install -r requirements.txt ``` ## 公式配布HPとmake [公式配布HP](https://www.python.org/ftp/python/)でマイナーバージョンまで細かく選べる インストールするバージョンをクリックし,`tar.xz`のURLをコピー `wget URL`とする ```bash # 3.10 url:https://www.python.org/ftp/python/3.10.8/ $ sudo apt update $ sudo apt install -y wget build-essential checkinstall python3-dev $ sudo apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev $ wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tar.xz $ tar xJf Python-3.10.8.tar.xz $ cd Python-3.10.8 $ ./configure --enable-optimizations --enable-shared $ make altinstall $ update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10.8 0 ```