在Ubuntu 22.04系统上安装Python 3.11的两种方法

本文介绍在Ubuntu 22.04 Linux系统上安装Python 3.11版本的两种方法,一种是从deadsnakes PPA安装,第二种是从源代码手动构建Python 3.11。建议在安装之前,执行sudo apt update && sudo apt upgrade -y更新一下Ubuntu 22.04系统。

方法1:在Ubuntu 22.04上从deadseeves PPA安装Python 3.11

deadsnakes PPA提供了在Ubuntu 22.04上安装Python 3.11的最简单方法。它还允许用户接收持续更新、错误修复和安全更新。

首先,安装所需的依赖程序包:

sudo apt install software-properties-common -y

将deadseeves PPA添加到APT包管理器源列表中:

sudo add-apt-repository ppa:deadsnakes/ppa

按Enter键继续。添加PPA后,可以使用以下命令在Ubuntu 22.04上安装Python 3.11:

sudo apt install python3.11

验证安装:

$ python3.11 --version

Python 3.11.1

方法2:从源代码在Ubuntu 22.04上安装Python 3.11

这是在Ubuntu Linux系统上安装Python 3.11的另一种方法。使用此方法,可以保证获得最新的Python版本。此方法的唯一问题是,您将无法通过APT包管理器接收持续更新、错误修复和安全更新。

首先,安装从源代码构建Python 3.11所需的软件包:

sudo apt update && sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev pkg-config -y

安装好软件包后,从Python官方发布页面下载最新的Python 3.11 gzip tarball,地址在https://www.python.org/downloads/source/。

您仍然可以使用wget下载tarball:

wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgz

提取下载的存档文件:

tar -xf Python-3.11.*.tgz

现在导航到目录:

cd Python-3.11.*/

我们将检查是否满足所需的依赖关系,并使用以下命令优化二进制文件:

./configure --enable-optimizations

样本输出如下:

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking for Python interpreter freezing... ./_bootstrap_python

checking for python3.11... no

checking for python3.10... no

checking for python3.9... no

checking for python3.8... python3.8

checking Python for regen version... Python 3.8.10

checking for pkg-config... /usr/bin/pkg-config

checking pkg-config is at least version 0.9.0... yes

checking for --enable-universalsdk... no

checking for --with-universal-archs... no

checking MACHDEP... "linux"

checking for gcc... gcc

configure: creating ./config.status

config.status: creating Makefile.pre

config.status: creating Misc/python.pc

config.status: creating Misc/python-embed.pc

config.status: creating Misc/python-config.sh

config.status: creating Modules/Setup.bootstrap

config.status: creating Modules/Setup.stdlib

config.status: creating Modules/ld_so_aix

config.status: creating pyconfig.h

configure: creating Modules/Setup.local

configure: creating Makefile

选中后,使用以下命令启动构建过程。我们使用-j来提供系统上可用的内核数量,这使得构建过程更快:

make -j $(nproc)

样本输出:

copying and adjusting /home/ubuntu/Python-3.11.1/Tools/scripts/idle3 -> build/scripts-3.11

copying and adjusting /home/ubuntu/Python-3.11.1/Tools/scripts/2to3 -> build/scripts-3.11

changing mode of build/scripts-3.11/pydoc3 from 664 to 775

changing mode of build/scripts-3.11/idle3 from 664 to 775

changing mode of build/scripts-3.11/2to3 from 664 to 775

renaming build/scripts-3.11/pydoc3 to build/scripts-3.11/pydoc3.11

renaming build/scripts-3.11/idle3 to build/scripts-3.11/idle3.11

renaming build/scripts-3.11/2to3 to build/scripts-3.11/2to3-3.11

gcc -pthread -c -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -fno-semantic-interposition -std=c11 -Objects/moduleobject.o Objects/namespaceobject.o Objects/object.o Objects/obmalloc.o Objects/picklebufobject.o Objects/rangeobject.o Objects/setobject.o Objects/sliceobject.o Objects/structseq.o Objects/tupleobject.o Objects/typeobject.o Objects/unicodeobject.o Objects/unicodectype.o Objects/unionobject.o Objects/weakrefobject.o Python/_warnings.o Python/Python-ast.o Python/Python-tokenize.o Python/asdl.o Python/ast.o Python/ast_opt.o Python/ast_unparse.o Python/bltinmodule.o Python/ceval.o Python/codecs.o Python/compile.o Python/context.o Python/dynamic_annotations.o Python/errors.o Python/frame.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getplatform.o  Modules/timemodule.o  Modules/_weakref.o  Modules/_abc.o  Modules/_functoolsmodule.o  Modules/_localemodule.o  Modules/_operator.o  Modules/_stat.o  Modules/symtablemodule.o  Modules/pwdmodule.o  Modules/xxsubtype.o Python/deepfreeze/deepfreeze.o Modules/getpath.o Python/frozen.o -lpthread -ldl  -lutil                        -lm

make[1]: Leaving directory '/home/ubuntu/Python-3.11.1'

注意:根据您的系统,从源代码构建Python可能需要很长时间。您可以调整传递给make命令中-j选项的数字,以指定要运行的并行构建作业的数量,这可以加快构建过程。

构建过程完成后,使用以下命令安装Python 3.11:

sudo make altinstall

我们使用altinstall而不是install来保持/usr/bin/Python中的默认Python二进制路径。

执行输出:

Looking in links: /tmp/tmprw_g09fu

Processing /tmp/tmprw_g09fu/setuptools-65.5.0-py3-none-any.whl

Processing /tmp/tmprw_g09fu/pip-22.3.1-py3-none-any.whl

Installing collected packages: setuptools, pip

Successfully installed pip-22.3.1 setuptools-65.5.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

完成后,检查版本:

$ python3.11 --version

Python 3.11.1

附:在Ubuntu 22.04上安装Python扩展

Python模块很重要,因为它们为Python添加了功能。可以使用Python包管理器(PIP)安装这些模块。

在Ubuntu 22.04上安装PIP:

sudo apt install python3-pip

现在使用PIP安装任何模块。以下是具体的使用语法:

sudo pip install module-name

例如:

sudo pip install mpesa

样本输出:

Collecting mpesa

  Downloading mpesa-0.0.1.tar.gz (2.4 kB)

  Preparing metadata (setup.py) ... done

Requirement already satisfied: pip in /usr/lib/python3/dist-packages (from mpesa) (22.0.2)

Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from mpesa) (2.25.1)

Building wheels for collected packages: mpesa

  Building wheel for mpesa (setup.py) ... done

  Created wheel for mpesa: filename=mpesa-0.0.1-py3-none-any.whl size=2626 sha256=9031754a0bdbde5600705450a140ae82845dabb09a9776f44dbcd5b910c1dcd1

  Stored in directory: /root/.cache/pip/wheels/bc/4b/55/b4a27060527309dd78bcb6969e653731bb89e00db89f82cfef

Successfully built mpesa

Installing collected packages: mpesa

Successfully installed mpesa-0.0.1

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

您可以使用命令列出所有已安装的模块:

$ pip list

在Ubuntu 22.04系统上安装Python 3.11的两种方法

至此,我们已经成功地完成了如何在Ubuntu 22.04上安装Python 3.11及安装Python扩展。现在,您可以使用已安装的Python 3.11来构建web应用程序、创建工作流、软件开发等。

栏目相关文章