Ubuntu20.04 从源代码编译安装 python3.10

Ubuntu 22.04 Release Date
Ubuntu 22.04 Jammy Jellyfish is scheduled for release on April 21, 2022
【Ubuntu20.04 从源代码编译安装 python3.10】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-x16-- botbot7 Dec2021Python-3.10.1 6439619 .rw-rw-r--125M48952 botbot2 Jan 13:58Python-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-x2-- botbot2 Jan 14:25bin 7873931 drwxr-xr-x3-- root root2 Jan 14:25include 6301328 drwxr-xr-x4-- botbot2 Jan 14:25lib 7998483 drwxr-xr-x3-- root root2 Jan 14:25share ╭─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 lrwxrwxrwx190 root root2 Jan 14:252to3 -> 2to3-3.10 6305551 .rwxr-xr-x11188 root root2 Jan 14:252to3-3.10 6305554 lrwxrwxrwx180 root root2 Jan 14:25idle3 -> idle3.10 6305549 .rwxr-xr-x11168 root root2 Jan 14:25idle3.10 6305561 .rwxr-xr-x12468 root root2 Jan 14:25pip3 6305562 .rwxr-xr-x12468 root root2 Jan 14:25pip3.10 6305555 lrwxrwxrwx190 root root2 Jan 14:25pydoc3 -> pydoc3.10 6305550 .rwxr-xr-x11018 root root2 Jan 14:25pydoc3.10 6305552 lrwxrwxrwx1100 root root2 Jan 14:25python3 -> python3.10 6305553 lrwxrwxrwx1170 root root2 Jan 14:25python3-config -> python3.10-config 6301329 .rwxr-xr-x124M46296 root root2 Jan 14:25python3.10 6305548 .rwxr-xr-x1 3.1k8 root root2 Jan 14:25python3.10-config ╭─bot@amd-5700G ~/opt/python3.10.1/bin ╰─?./python3.10 Python 3.10.1 (main, Jan2 2022, 14:23:57) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>

参考文章:
构建Python
【树莓派】给ubuntu18安装python3.7

    推荐阅读