Raspberry Pi に Python3.6をinstallしてみた


はじめに

現在(2019/06) http://ftp.jaist.ac.jp/raspbian/http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ などにあるミラーサーバーには最新のPythonのパッケージは3.5までで3.6以降は各自でダウンロードしてインストールする必要がある。ここではRaspberry Pi環境内でPython3.6をinstall方法についてまとめてみる。

方法

⑴ package をdownload & install

Raspberry Pi に Python 3.7.0 をインストールする
のサイトが参考になります。

## download
$ wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

## unzip
$ tar -xf Python-3.6.8.tar.xz
$ cd Python-3.6.8

## install
$ ./configure
$ sudo make
$ sudo make altinstall

## symbolic
$ ln -s /usr/local/bin/python3.6 /usr/local/bin/python3

これでpythonは起動できます。
ですが、numpyかbeautufulsoupなどをpipでパッケージをinstallをしようとするとerrorになります。

## launch
$ python3
Python 3.6.8 (default, Jun 17 2019, 16:55:54) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

### pip -> error
$ sudo pip3 install numpy
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

実は、pythonでmakeしているときに下記のerrorが出ていました。

building '_ssl' extension
*** WARNING: renaming "_ssl" since importing it failed: build/lib.linux-armv7l-3.6/_ssl.cpython-36m-arm-linux-gnueabihf.so: undefined symbol: SSL_get0_alpn_selected

sslのモジュールが必要みたいです。

⑵ openSSL install

apt-get (or yum) install openssl-devel などでpackageをinstallして解決すれば問題ないのですが、 ラズパイの場合はそうではなかったので下記の手順を踏む必要があります。

1. openssl を download & install
2. python3.6パッケージ内にある Modules/Setup.dist を開いて、openssl関連のファイルパスを記述する
3. 再度 python3.6 を make installする

⑵.1 openssl を download & install

Version 1.1.1 will be supported until 2023-09-11 (LTS).
Version 1.1.0 will be supported until 2019-09-11.
Version 1.0.2 will be supported until 2019-12-31 (LTS).
Version 1.0.1 is no longer supported.
Version 1.0.0 is no longer supported.
Version 0.9.8 is no longer supported.

opensslの公式サイトを見るとopenssl1.0.2以上でないとサポートされておらず、もしpython3.7をinstallする場合はopenssl1.0.2以上でないと対応していないとのことです。なので、openssl1.1.1をinstallすることにします。

$ wget https://www.openssl.org/source/openssl-1.1.1.tar.gz
$ tar -xf openssl-1.1.1.tar.gz
$ cd openssl-1.1.1
$ sudo ./config --prefix=/usr/src/openssl-1.1.1 shared zlib
$ sudo make depend
$ sudo make
$ sudo make test
$ sudo make install

⑵.2 Modules/Setup.dist を編集

209-212行目のコメントアウトを外してSSLのパスを指定します。defaultではSSL=/usr/local/sslとなっていますが、先ほどdownloadしたopensslのフォルダ先を指定します。

Modules/Setup.dist
SSL=/usr/src/openssl-1.1.1
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

⑵.3 再度 python3.6 を make installする

./configure && make && sudo make install をすると完了です。

おわりに

ちなみに、python3.7.3をraspberry pi内にインストールを試みましたが、python本体は起動できるもののpip installで undefined symbol: SSL_CTX_get0_param とerrorが発生します。そのため、しばらくはpython3.6でラズパイの開発進めようかなと思っています。 3.5 -> 3.6 にしたメリットとしては async非同期機能があるのが大きいでしょうか。

あと余談として pythonインストール時に ./configure --enable-optimizations とするとパフォーマンスが約10%上がるみたいです。

Reference