【メモ】研究室のサーバーで複数バージョンのPython3の環境整備


研究室のサーバー管理者となって実質丸々1台を管理することになったので、そのときのことのメモ。色々な都合があり、一般ユーザーで言う所のローカルインストールで行ったので、読み替えれば一般ユーザーにも応用可能。

環境・条件

$ cat /etc/issue
Ubuntu 18.04.5 LTS \n \l

手順

依存ソースのダウンロード・解凍

以下、ローカルtcl, tkは結果的に使用せず。(Pythonが特にパッケージ管理システムで正常に入っていないマシンにpythonをローカルインストールするなら必要だと思う。)

/usr/local/src
# wget https://www.openssl.org/source/openssl-1.1.1h.tar.gz
# wget --no-check-certificate https://prdownloads.sourceforge.net/tcl/tcl8.6.10-src.tar.gz
# wget --no-check-certificate https://prdownloads.sourceforge.net/tcl/tk8.6.10-src.tar.gz
# wget --no-check-certificate https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tar.xz
# wget --no-check-certificate https://www.python.org/ftp/python/3.6.12/Python-3.6.12.tar.xz
# wget --no-check-certificate https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tar.xz
# wget --no-check-certificate https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tar.xz
# wget --no-check-certificate https://www.python.org/ftp/python/3.9.0/Python-3.9.0tar.xz
# tar xzf openssl-1.1.1h.tar.gz
# tar xzf tcl8.6.10-src.tar.gz
# tar xzf tk8.6.10-src.tar.gz
# tar xf Python-3.5.10.tar.xz
# tar xf Python-3.6.12.tar.xz
# tar xf Python-3.7.9.tar.xz
# tar xf Python-3.8.6.tar.xz
# tar xf Python-3.9.0.tar.xz
# rm *.tar.xz *.tar.gz

各パッケージのインストール

/usr/local/src
# cd openssl-1.1.1h
# ./config --prefix=/usr/local/ --openssldir=/usr/local/ssl/ shared
# make
# make test
# make install

OpenSSLをリンカーの検索対象に追加
# echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib" >> ~/.bashrc
# openssl version
OpenSSL 1.1.1h  22 Sep 2020

# cd ../tcl8.6.10/
# mkdir build && cd build
# ../unix/configure --prefix=/usr/local/
# make
# make test
# make install

# cd ../../tk8.6.10/
# mkdir build && cd build
# ../unix/configure --prefix=/usr/local/
# make
# make test --> 何かエラーが出た。けどinstallは通った。
# make install

Pythonのインストール・リンク作成

~/.bashrc
function pyconf (){
    ./configure --prefix=/usr/local/$1 --with-ensurepip --with-openssl=/usr/local/
}

ソースからビルドしたtcltkを使うならこっち。
function pyconf (){
    ./configure --prefix=/usr/local/$1 --with-ensurepip --with-openssl=/usr/local/ --with-tcltk-includes="/usr/local/include" --with-tcltk-libs="/usr/local/lib -ltc8.6 -ltk.8.6"
}
# mkdir python390
# cd src/Python-3.9.0
# pyconf python390
# make
# make install
# ln -nLs /usr/local/python390/bin/python3.9 python390
# ln -nLs /usr/local/python390/bin/pip3 pip390
同様の手順をを各バージョンについて行う。

もしmakeの時点で、「"-fPIC"をつけてコンパイルしてね。」的なメッセージでエラーになっても、make cleanしてもう一度やれば成功する(おそらくメモリをきれいにしたりメモリの状態を変えたりすると上手くいく場合がある。)可能性があるので試すべし。

参考・関連ページ