0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[備忘録]レンタルサーバーにPython3.12をビルドする

Last updated at Posted at 2025-04-01

備忘録。

前提

  • レンタルサーバーを使用している(sudoを使えない)
  • 新たにPython3.12.9をビルドしたい
  • OpenSSLを手動でインストールする
  • _ctypesも手動でインストールする

最近のPythonはOpenSSL1.1.1以上を使う必要があるが、レンタルサーバーに入っているOpenSSLは1.1.1未満だったため、エラーが発生したっぽい。
レンタルサーバーに_ctypeが入ってないかった。

解決方法

1.自身のホームディレクトリにOpenSSLをインストールする

wget https://www.openssl.org/source/openssl-3.4.0.tar.gz
tar -xzf openssl-3.4.0.tar.gz
cd openssl-3.4.0
./config --prefix=$HOME/local/openssl --openssldir=$HOME/local/openssl
make
make install

2.libssl.so.3がないらしい

~/local/openssl/bin/openssl version

を実行してみたところ、

error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory

と出た
libssl.so.3 がどこにあるかを確認

find ~/local/openssl -name "libssl.so.3"

~/local/openssl/lib64/ にあったので、
確認したライブラリのパスを LD_LIBRARY_PATH に追加

export LD_LIBRARY_PATH="$HOME/local/openssl/lib64:$LD_LIBRARY_PATH"

永続化したかったので、.bashrcに書いた

echo 'export LD_LIBRARY_PATH="$HOME/local/openssl/lib64:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc

再度

~/local/openssl/bin/openssl version

を実行したら、OpenSSL 3.4.0 と表示された。

3. _ctypeをインストール

libffiというものを入れれば良いらしい。

wget https://github.com/libffi/libffi/releases/download/v3.4.7/libffi-3.4.7.tar.gz
tar -xvzf libffi-3.4.7.tar.gz
cd libffi-3.4.7
./configure --prefix=$HOME/local/libffi
make
make install

ライブラリのパスを LD_LIBRARY_PATHに指定する。

export LD_LIBRARY_PATH=$HOME/local/libffi/lib64:$LD_LIBRARY_PATH

これも永続化したかったので、.bashrcに追加した。

echo 'export LD_LIBRARY_PATH="$HOME/local/libffi/lib64:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc

3.インストールしたSSL, libffiを指定し、Pythonを再ビルドする

wget https://www.python.org/ftp/python/3.12.9/Python-3.12.9.tgz
tar xvf Python-3.12.9.tgz
cd ~/Python-3.12.9
./configure --prefix=$HOME/local \
  --with-openssl=$HOME/local/openssl \
  --with-system-ffi \
  CPPFLAGS="-I$HOME/local/libffi/include -I$HOME/local/openssl/include" \
  LDFLAGS="-L$HOME/local/libffi/lib64 -L$HOME/local/openssl/lib64"
make
make install

4.仮想環境を作る

cd
./local/bin/python3.12 -m venv venv
source venv/bin/activate

5.ちゃんと入ってるかの確認

python
import ssl
import _ctypes

何も出なければOK!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?