P在Ubuntu 18.04 LTS上安裝Python 3.7

Python是最流行和按需的通用​​編程語言之一。它是一種高級的解釋型編程語言,支持多種編程範例,包括過程,面向對象和函數式編程。由於具有全面的標準庫,因此通常將其稱為“含電池”語言。

用途廣泛的一種非常通用的編程語言,因為它由初學者為科學家用於各種類型的活動。用於編程和腳本系統,自動執行重複性任務,構建網站和Web應用程序,機器學習算法,數據分析等。

在本教程中,您將學習如何在Ubuntu 18.04 LTS Bionic Beaver上安裝Python 3.7。包括在Ubuntu 18.04機器上安裝的兩種主要方法。一種來自使用APT程序包管理器的Deadsnakes PPA,另一種來自源代碼編譯。

使用APT(帶有PPA)在Ubuntu 3上安裝Python 3.7

Ubuntu 18.04默認帶有Python 3.6.9。您可以通過運行以下命令來獲取Python版本:

python3 --version
Python 3.6.9

首先,您需要在Ubuntu 18.04計算機上更新Ubuntu軟件包存儲庫索引。這可以通過運行以下命令來完成:

$ sudo apt update

更新完成後,您需要確保在計算機上安裝了“ software-properties-common”軟件包,以便可以將PPA添加到計算機上。

$ sudo apt install software-properties-common

現在,您可以使用以下命令輕鬆添加Deadsnakes PPA:

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

最後,將必需的PPA添加到Ubuntu 18.04計算機後,您現在可以使用apt命令安裝Python 3.7。

-y apt選項告訴命令為提示設置auto yes。

$ 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是否已成功安裝:

$ 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
--2020-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

2020-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插入目錄並運行configure腳本以評估是否已滿足編譯所需的依賴關係。 –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_PARAM_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。參數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

結論

最後,您已經在Ubuntu 18.04 LTS計算機上成功安裝了python 3.7。在撰寫本文時,Python 3.7的最新版本是3.7.7。因此,您可以將3.7.7版替換為最新版本。現在,您可以輕鬆運行為3.7創建的Python腳本和代碼。如果您有任何問題,建議或反饋,請在下面的評論框中發布。

Sidebar