Ubuntu20.04 從原始碼編譯安裝 python3.10

ponponon發表於2022-01-08

Ubuntu 22.04 Release Date

Ubuntu 22.04 Jammy Jellyfish is scheduled for release on April 21, 2022

If you’re ready to use Ubuntu 22.04 Jammy Jellyfish, you can either upgrade your current Ubuntu system or download Ubuntu 22.04 and install it from ISO.

Ubuntu22 還有幾個月就要來到了,自帶的 Python 版本將是 3.10 ,但是我想在 Ubuntu20.04 上也想用呢!

Ubuntu20.04 自帶的 Python 版本是 3.8 ,如果想安裝 python3.9 可以用該命令:sudo apt install python3.9

Ubuntu20.04 的倉庫沒有收錄 python3.10 ,那我們就自己從原始碼編譯安裝吧!

準備工作,先安裝依賴項

sudo apt update
sudo apt upgrade
sudo apt install gcc
sudo apt install g++
sudo apt install libffi-dev
sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
我把 python3.10 原始碼壓縮包下載到 ~/Downloads 目錄下
我把 python3.10 安裝到 ~/opt 目錄下

下載 Python 原始碼

~/opt 目錄下執行下面的命令下載原始碼的壓縮包

sudo curl -O https://www.python.org/ftp/python/3.10.1/Python-3.10.1.tgz

解壓縮

tar zxvf ./Python-3.10.1.tgz

檢視

╭─bot@amd-5700G ~/Downloads
╰─➤  ll | grep Python                                                     
 6439688 drwxr-xr-x     16    -        - bot  bot    7 Dec  2021  Python-3.10.1
 6439619 .rw-rw-r--      1  25M    48952 bot  bot    2 Jan 13:58  Python-3.10.1.tgz

準備好安裝路徑

cd ~/opt
mkdir python3.10.1

安裝

cd ~/Downloads/Python-3.10.1
sudo ./configure --enable-optimizations --prefix=/home/bot/opt/python3.10.1
sudo make -j8 
sudo make install
預設只用一個處理器編譯,這太慢了,我們多程式並行處理:sudo make -j8 && make install,-j8 表示用 8 個處理器,如果你的處理器只有4個就改成4

安裝好了就是下面的樣子

╭─bot@amd-5700G ~/opt/python3.10.1
╰─➤  ll
  inode Permissions Links Size Blocks User Group Date Modified Name
6301327 drwxr-xr-x      2    -      - bot  bot    2 Jan 14:25  bin
7873931 drwxr-xr-x      3    -      - root root   2 Jan 14:25  include
6301328 drwxr-xr-x      4    -      - bot  bot    2 Jan 14:25  lib
7998483 drwxr-xr-x      3    -      - root root   2 Jan 14:25  share
╭─bot@amd-5700G ~/opt/python3.10.1
╰─➤  cd bin
╭─bot@amd-5700G ~/opt/python3.10.1/bin
╰─➤  ll
  inode Permissions Links Size Blocks User Group Date Modified Name
6305556 lrwxrwxrwx      1    9      0 root root   2 Jan 14:25  2to3 -> 2to3-3.10
6305551 .rwxr-xr-x      1  118      8 root root   2 Jan 14:25  2to3-3.10
6305554 lrwxrwxrwx      1    8      0 root root   2 Jan 14:25  idle3 -> idle3.10
6305549 .rwxr-xr-x      1  116      8 root root   2 Jan 14:25  idle3.10
6305561 .rwxr-xr-x      1  246      8 root root   2 Jan 14:25  pip3
6305562 .rwxr-xr-x      1  246      8 root root   2 Jan 14:25  pip3.10
6305555 lrwxrwxrwx      1    9      0 root root   2 Jan 14:25  pydoc3 -> pydoc3.10
6305550 .rwxr-xr-x      1  101      8 root root   2 Jan 14:25  pydoc3.10
6305552 lrwxrwxrwx      1   10      0 root root   2 Jan 14:25  python3 -> python3.10
6305553 lrwxrwxrwx      1   17      0 root root   2 Jan 14:25  python3-config -> python3.10-config
6301329 .rwxr-xr-x      1  24M  46296 root root   2 Jan 14:25  python3.10
6305548 .rwxr-xr-x      1 3.1k      8 root root   2 Jan 14:25  python3.10-config
╭─bot@amd-5700G ~/opt/python3.10.1/bin
╰─➤  ./python3.10
Python 3.10.1 (main, Jan  2 2022, 14:23:57) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

建立環境變數

如下面的程式碼新增環境變數

export PATH=$PATH:/home/bot/opt/python3.10.1/bin

如果是 bash,就新增到 ~/.bashrc
如果是 zsh,就新增到 ~/.zshenv

不要用了重新 source 一下
比如 source ~/.bashrc

參考:
【樹莓派】給ubuntu18安裝python3.7
macOS 中 zsh 配置檔案及其載入順序
Files
切換使用者後,/etc/profile的配置不起效

更換軟體源

替換為清華源

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

建立軟連線

不贅述
參考:
【樹莓派】給ubuntu18安裝python3.7

使用 3.10

我一般都用 pipenv 來管理包和虛擬環境

pipenv install --python=python3.10

參考文章:
構建Python
【樹莓派】給ubuntu18安裝python3.7

相關文章