GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ubuntu18.04LTSにPython3.7をインストールします

Pythonは、最も人気がありオンデマンドの汎用プログラミング言語の1つです。これは、手続き型、オブジェクト指向、および関数型プログラミングを含む複数のプログラミングパラダイムをサポートする、解釈された高水準プログラミング言語です。包括的な標準ライブラリのため、「バッテリーを含む」言語として説明されることがよくあります。

初心者から科学者までさまざまな種類の活動に使用されるため、広く使用されており、非常に用途の広いプログラミング言語です。システムプログラミングとスクリプティング、反復タスクの自動化、WebサイトとWebアプリケーションの構築、機械学習アルゴリズム、データ分析などに使用されます。

このチュートリアルでは、Ubuntu 18.04 LTSBionicBeaverにPython3.7をインストールする方法を学習します。 Ubuntu 18.04マシンにインストールする2つの主要な方法を含めます。1つはAPTパッケージマネージャーを使用したデッドスネークPPAを使用する方法で、もう1つはソースを介してコンパイルする方法です。

APT(PPAを使用)を使用してubuntuにPython3.7をインストールします

デフォルトでは、Ubuntu18.04にはPython3.6.9が付属しています。次のコマンドを実行すると、Pythonバージョンを取得できます。

python3 --version
Python 3.6.9

まず、Ubuntu18.04マシンのUbuntuのパッケージリポジトリインデックスを必ず更新する必要があります。これは、次のコマンドを実行することで実行できます:

$ sudo apt update

更新が完了したら、PPAをマシンに追加できるように、「software-properties-common」パッケージがマシンにインストールされていることを確認する必要があります。

$ sudo apt install software-properties-common

これで、次のコマンドを使用して、デッドスネークPPAを簡単に追加できます。

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

最後に、必要なPPAがUbuntu 18.04マシンに追加されると、aptコマンドを使用してPython3.7をインストールできるようになります。

-y aptのオプションは、プロンプトに自動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

ソースからubuntuにPython3.7をインストールする

この方法では、ソースコードからコンパイルして、Ubuntu18.04マシンにPython3.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コマンドを使用して公式のPythonWebサイトからPython3.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]

このチュートリアルを書いている間に、Python3.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

configureスクリプトが正常に実行されたら、altinstall引数を指定してmakeコマンドを実行することにより、Ubuntu18.04LTSマシンにpython3.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

これで、次のようにバージョンを確認することで、Python3.7が正常にインストールされたかどうかを確認できます。

$ python3.7 -V
Python 3.7.7

結論

最後に、Ubuntu18.04LTSマシンにPython3.7を正常にインストールしました。このチュートリアルを書いている時点では、Python 3.7の最新バージョンは3.7.7であったため、バージョン3.7.7を最新バージョンのリリースに置き換えることをお勧めします。これで、3.7用に作成されたPythonスクリプトとコードを簡単に実行できます。ご質問、ご提案、フィードバックがございましたら、下のコメントボックスにご記入ください。


Ubuntu
  1. Ubuntu18.04または20.04にPython3をインストールする方法

  2. Ubuntu18.04にPython3.9をインストールする方法

  3. Ubuntu20.04LTSにDokuwikiをインストールします

  1. Ubuntu18.04にPython3.7をインストールする方法

  2. Ubuntu18.04にPython3.8をインストールする方法

  3. Ubuntu20.04にPython3.9をインストールする方法

  1. Ubuntu16.04LTSにDjangoをインストールする方法

  2. Ubuntu18.04LTSにDjangoをインストールする方法

  3. Ubuntu20.04LTSにDjangoをインストールする方法