Ubuntu 22.04 LTSに最新バージョンのPythonをインストールする方法

環境
Ubuntuのバージョンの確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"
# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"

Pythonのインストール手順
1.デフォルトでは以下のPythonがインストールされています。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python3 -V
Python 3.10.4
# which python3
/usr/bin/python3
# python3 -V Python 3.10.4 # which python3 /usr/bin/python3
# python3 -V
Python 3.10.4
# which python3
/usr/bin/python3

2.パッケージを更新します
# sudo apt update

依存関係パッケージをインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sudo apt install build-essential libbz2-dev libdb-dev \
libreadline-dev libffi-dev libgdbm-dev liblzma-dev \
libncursesw5-dev libsqlite3-dev libssl-dev \
zlib1g-dev uuid-dev tk-dev
# sudo apt install build-essential libbz2-dev libdb-dev \ libreadline-dev libffi-dev libgdbm-dev liblzma-dev \ libncursesw5-dev libsqlite3-dev libssl-dev \ zlib1g-dev uuid-dev tk-dev
# sudo apt install build-essential libbz2-dev libdb-dev \
  libreadline-dev libffi-dev libgdbm-dev liblzma-dev \
  libncursesw5-dev libsqlite3-dev libssl-dev \
  zlib1g-dev uuid-dev tk-dev

3.Pythonの最新版をダウンロードします(2022年7月15日時点)

https://pythonlinks.python.jp/ja/index.html

「Python-3.10.5.tar.xz」をダウンロードします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tar.xz
# wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tar.xz
# wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tar.xz

4.ダウンロードしたファイルは、次のコマンドで解凍します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# tar xJf Python-3.10.5.tar.xz
# cd Python-3.10.5
# ./configure
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
# tar xJf Python-3.10.5.tar.xz # cd Python-3.10.5 # ./configure 略 If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations
# tar xJf Python-3.10.5.tar.xz
# cd Python-3.10.5
# ./configure  
略
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations

5.pythonのコンパイルを行います

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# make
# make
# make

インストールを行います

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sudo make install
Successfully installed pip-22.0.4 setuptools-58.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
# sudo make install 略 Successfully installed pip-22.0.4 setuptools-58.1.0 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
# sudo make install
略
Successfully installed pip-22.0.4 setuptools-58.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

6.バージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python3.10 -V
Python 3.10.5
# python3.10 -V Python 3.10.5
# python3.10 -V
Python 3.10.5

方法2

# python3 -V
Python 3.10.5

Ubuntu 22.04

Posted by arkgame