在Ubuntu 18.04 LTS上安装Python 3.7

时间:2020-03-05 15:32:32  来源:igfitidea点击:

Python是最受欢迎和按需按需的通用编程语言之一。
它是一种解释的高级编程语言,支持多个编程范例,包括过程,面向对象和功能编程。
由于其综合标准库,通常被描述为"包含"语言的"电池"。

它被广泛使用和极其多功能的编程语言,因为它被使用的科学家用于不同类型的活动。
它用于系统编程和脚本,自动化重复任务,构建和Web应用程序,机器学习算法,数据分析等等。

在本教程中,我们将学习如何在Ubuntu 18.04 LTS Bionic Beaver中安装Python 3.7.
我们将包括在Ubuntu 18.04机器中安装的两种主要方法,其中一个是使用APT包管理器通过Deadsnakes PPA,而另一种是通过编译来源。

使用apt(带ppa)在Ubuntu上安装Python 3.7

默认情况下,Ubuntu 18.04附带Python 3.6.9.
通过运行以下命令,我们可以获得python版本:

python3 --version
Python 3.6.9

首先,我们需要确保更新Ubuntu 18.04计算机的Ubuntu的包存储库索引。
这可以通过执行以下命令来完成:

$sudo apt update

完成更新后,我们需要确保在我们的机器SOT中安装了"软件属性 - 常用"包,我们可以将PPA添加到我们的机器中。

$sudo apt install software-properties-common

现在,我们可以使用以下命令轻松添加DeadsNakes PPA:

$sudo add-apt-repository ppa:deadsnakes/ppa

最后,随着所需的PPA被添加到我们的Ubuntu 18.04机器中,我们现在可以只使用APT命令安装Python 3.7.

APT中的"-y""选项通知命令设置自动是以提示。

$sudo apt install python3.7 -y
Output
$sudo apt install python3.7 -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3.7-minimal libpython3.7-stdlib python3-distutils python3-lib2to3
  python3.7-minimal
Suggested packages:
  python3.7-venv python3.7-doc binfmt-support
The following NEW packages will be installed:
  libpython3.7-minimal libpython3.7-stdlib python3-distutils python3-lib2to3
  python3.7 python3.7-minimal
0 upgraded, 6 newly installed, 0 to remove and 321 not upgraded.
Need to get 4,792 kB of archives.
After this operation, 26.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 libpython3.7-minimal amd64 3.7.7-1+bionic1 [596 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-lib2to3 all 3.6.9-1~18.04 [77.4 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 python3.7-minimal amd64 3.7.7-1+bionic1 [1,839 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.9-1~18.04 [144 kB]
Get:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 libpython3.7-stdlib amd64 3.7.7-1+bionic1 [1,784 kB]
Get:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 python3.7 amd64 3.7.7-1+bionic1 [351 kB]
Fetched 4,792 kB in 7s (737 kB/s)                                              
Selecting previously unselected package libpython3.7-minimal:amd64.
(Reading database ... 134446 files and directories currently installed.)
Preparing to unpack .../0-libpython3.7-minimal_3.7.7-1+bionic1_amd64.deb ...
Unpacking libpython3.7-minimal:amd64 (3.7.7-1+bionic1) ...
Selecting previously unselected package python3.7-minimal.
Preparing to unpack .../1-python3.7-minimal_3.7.7-1+bionic1_amd64.deb ...
Unpacking python3.7-minimal (3.7.7-1+bionic1) ...
Selecting previously unselected package libpython3.7-stdlib:amd64.
Preparing to unpack .../2-libpython3.7-stdlib_3.7.7-1+bionic1_amd64.deb ...
Unpacking libpython3.7-stdlib:amd64 (3.7.7-1+bionic1) ...
Selecting previously unselected package python3-lib2to3.
Preparing to unpack .../3-python3-lib2to3_3.6.9-1~18.04_all.deb ...
Unpacking python3-lib2to3 (3.6.9-1~18.04) ...
Selecting previously unselected package python3-distutils.
Preparing to unpack .../4-python3-distutils_3.6.9-1~18.04_all.deb ...
Unpacking python3-distutils (3.6.9-1~18.04) ...
Selecting previously unselected package python3.7.
Preparing to unpack .../5-python3.7_3.7.7-1+bionic1_amd64.deb ...
Unpacking python3.7 (3.7.7-1+bionic1) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
Setting up libpython3.7-minimal:amd64 (3.7.7-1+bionic1) ...
Setting up python3.7-minimal (3.7.7-1+bionic1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
Setting up python3-lib2to3 (3.6.9-1~18.04) ...
Setting up python3-distutils (3.6.9-1~18.04) ...
Setting up libpython3.7-stdlib:amd64 (3.7.7-1+bionic1) ...
Setting up python3.7 (3.7.7-1+bionic1) ...

现在,为了确保Python成功安装了Python,我们可以检查Python版本。

$python3.7 -V
Python 3.7.7

从源中安装Python 3.7在Ubuntu上

在此方法中,我们将学习如何通过从源代码中编译它来在Ubuntu 18.04计算机中安装Python 3.7.
要继续前进,我们需要确保满足某些准备工作。
首先,我们需要确保在我们的计算机中安装编译源代码所需的构建工具。

为此,我们首先需要更新APT的本地存储库索引,如下所示:

$sudo apt update

随着本地存储库索引已更新,我们现在将安装编译和安装Python3.7所需的依赖项。
为此,我们需要执行以下命令:

$sudo apt install -y build-essential wget zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

一旦依赖关系都安装并准备就绪,我们将使用wget命令从官方Python下载最新版本的Python 3.7,如下所示:

$wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
--2017-03-17 16:19:44--  https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
Resolving www.python.org (www.python.org)... 151.101.8.223, 2a04:4e42:2::223
Connecting to www.python.org (www.python.org)|151.101.8.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23161893 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.7.tgz’
Python-3.7.7.tgz    100%[===================>]  22.09M  2.50MB/s    in 9.2s
2017-03-17 16:19:54 (2.41 MB/s) - ‘Python-3.7.7.tgz’ saved [23161893/23161893]

在撰写本教程时,Python 3.7在最新版本3.7.7下发布。
因此,我们将使用tar命令下载并提取TGZ文件。

$tar -xf Python-3.7.7.tgz

从TGz文件中提取源代码后,我们将在目录中CD中CD,然后执行配置脚本以便评估是否满足了编译所需的依赖项。
-Enable-OptimIzations标志优化Python二进制文件,并将代码执行增加10-20%。

$cd Python-3.7.7
$./configure --enable-optimizations
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... yes
checking for pkg-config... no
checking for openssl/ssl.h in /usr/local/ssl... no
checking for openssl/ssl.h in /usr/lib/ssl... no
checking for openssl/ssl.h in /usr/ssl... no
checking for openssl/ssl.h in /usr/pkg... no
checking for openssl/ssl.h in /usr/local... no
checking for openssl/ssl.h in /usr... yes
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PA内存_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

一旦配置脚本成功执行,我们现在将通过使用AltinStall参数执行Make命令在Ubuntu 18.04 LTS计算机中安装Python 3.7.
Arguit AltInstall可确保它未安装为系统默认Python版本。

$sudo make altinstall
changing mode of /usr/local/bin/idle3.7 to 755
changing mode of /usr/local/bin/pyvenv-3.7 to 755
changing mode of /usr/local/bin/pydoc3.7 to 755
changing mode of /usr/local/bin/2to3-3.7 to 755
rm /usr/local/lib/python3.7/lib-dynload/_sysconfigdata_m_linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.7/lib-dynload/__pycache__
Creating directory /usr/local/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
	/usr/local/share/man/man1/python3.7.1
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--altinstall --upgrade" ;; \
		install|*) ensurepip="--altinstall" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/; \
fi
Looking in links: /tmp/tmp06ih2lq9
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.2.3 setuptools-41.2.0

现在,我们可以通过查看其版本如下检查Python 3.7是否成功安装。

$python3.7 -V
Python 3.7.7