乐趣区

关于python:Ubuntu2004-从源代码编译安装-python310

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

参考:
【树莓派】给 ubuntu18 装置 python3.7
macOS 中 zsh 配置文件及其加载程序
Files
切换用户后,/etc/profile 的配置不起效

更换软件源

替换为清华源

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

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

退出移动版