さくらのレンタルサーバーにpython3をインストール

移行元で使っていたpython3の資産を、さくらのレンタルサーバーで使用したい。
2025年3月現在、さくらのレンタルサーバーでは標準でpython2しか使えないので、独自にpython3をインストールする。

前提

  • さくらのレンタルサーバーのサポート情報(FAQ)を見ると、標準のpythonの使用を推奨していますが、独自にpython 3.12.9をインストールします。
  • レンタルサーバーのスタンダードプランで提供されるFreeBSD 13.0-RELEASE(amd64)を使用します。
  • python3のインストールにはopenssl 1.1.1以降が必要です。
    標準で1.1.1(OpenSSL 1.1.1k-freebsd)がインストールされていますが、ライブラリがインストールされていないので、ビルドでエラーになります。


    To find the necessary bits, look in configure.ac and config.log.

    Could not build the ssl module!
    Python requires a OpenSSL 1.1.1 or newer

  • この問題を解決するために、先にOpenSSLをインストールします。また、pythonビルド時のエラーを回避するために LD_LIBRARY_PATH を指定します。
  • root権限はないので、pythonやOpenSSLのインストール先は、ホームディレクトリ直下($HOME/local)にしています。

インストール手順

OpenSSL インストール手順

  1. OpenSSLのビルド
    opensslからソースコードをダウンロードして、ビルド・インストールします。ここでopenssl 3.4を使用していますが、LTS版の3.0や3.5がお薦めです。

    mkdir ~/work
    cd ~/work
    
    wget --no-check-certificate https://www.openssl.org/source/openssl-3.4.0.tar.gz
    tar xvfz openssl-3.4.0.tar.gz
    cd openssl-3.4.0
    
    ./Configure --prefix=$HOME/local
    make
    make install
  2. 環境変数LD_LIBRARY_PATH の追加
    .bash_profile(または.bashrc)を開いて、環境変数PATH、LD_LIBRARY_PATH を編集し、読み直します。

    vi ~/.bash_profile
        ...
        PATH=$HOME/local/bin:$PATH
        ...
        export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH
        ...
    source ~/.bash_profile

python3 インストール手順

  1. python3のビルド
    python3のソースコードをダウンロードし、ビルド・インストールします。
    ここでは、python 3.12.9を使用しています。

    cd ~/work
    
    wget https://www.python.org/ftp/python/3.12.9/Python-3.12.9.tgz
    tar xvfz Python-3.12.9.tgz
    cd Python-3.12.9
    
    ./configure --prefix=$HOME/local --with-openssl=$HOME/local
        ...
        checking for shm_unlink... yes
        checking for include/openssl/ssl.h in /home/nextdoorwith/local... yes
        checking whether compiling and linking against OpenSSL works... yes
        checking for --with-openssl-rpath...
        checking whether OpenSSL provides required ssl module APIs... yes
        checking whether OpenSSL provides required hashlib module APIs... yes
        checking for --with-ssl-default-suites... python
        checking for --with-builtin-hashlib-hashes... md5,sha1,sha2,sha3,blake2
        checking for libb2... no
        checking for --disable-test-modules... yes
        ...
        checking for stdlib extension module _lzma... yes
        checking for stdlib extension module _ssl... yes
        checking for stdlib extension module _hashlib... yes
        checking for stdlib extension module _testcapi... yes
        ...
    make
        ...
        The necessary bits to build these optional modules were not found:
        _tkinter
        To find the necessary bits, look in configure.ac and config.log.
    
        Checked 111 modules (31 built-in, ..., 0 disabled, 1 missing, 0 failed on import)
    
    make install
        ...
        Processing /tmp/tmpb8swe3_5/pip-24.3.1-py3-none-any.whl
        Installing collected packages: pip
        Successfully installed pip-24.3.1
  2. 既定のpythonコマンドを変更します。
    which python
        /usr/local/bin/python
    
    cd ~/local/bin
    ln -s python3 python
    ln -s pip3 pip
    
    which python
        /home/myuser/local/bin/python
    which pip
        /home/myuser/local/bin/pip