nikkie-ftnextの日記

イベントレポートや読書メモを発信

UbuntuイメージにPythonとuWSGIをインストールする(pythonX.X-dev、gcc、wheelが必要です)

はじめに

トマトの日!のお弁当〜♪ nikkieです。

Today(※最近) I learnedです。
UbuntuイメージにuWSGIは難しかった...(記事を書いて整理されましたが、かなりハマってました)

目次

uWSGIとは

uWSGI application server container

PythonにはWSGI1がありますが、uWSGIはそのサーバ実装の1つという理解です(他にgunicornなどがある)

記事を書いていて気づきましたが、「maintenance mode」に入ってました。
これまでありがとう、uWSGI...

そもそも:pythonイメージなら簡単に入ります

動作環境はmacOSです。

% docker --version
Docker version 27.2.1-rd, build cc0ee3e
% docker run --rm -it --platform linux/x86_64 python:3.10 bash
# python -V
Python 3.10.15
# python -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.1
wheel      0.44.0
# python -m pip install uwsgi
Successfully installed uwsgi-2.0.27
# python -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.1
uWSGI      2.0.27
wheel      0.44.0

1️⃣ Python 3.10のインストール

こちらの記事を元に、python3.10コマンドやpython3.10 -m pipでpipが使えるUbuntuイメージを用意しています。

% docker build --build-arg python_version=3.10 -t ubuntu-python:3.10 .

% docker run --rm -it --platform linux/x86_64 ubuntu-python:3.10 bash
# python3.10 -V
Python 3.10.15
# python3.10 -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.0

2️⃣ gccのインストール

意気揚々とuWSGIをインストールしようとすると...

# python3.10 -m pip install uwsgi
× Running setup.py install for uwsgi did not run successfully.
│ exit code: 1
╰─> [36 lines of output]

Exception: you need a C compiler to build uWSGI
[end of output]

ドキュメントより
https://uwsgi-docs.readthedocs.io/en/latest/Install.html#installing-from-source

To build uWSGI you need Python and a C compiler (gcc and clang are supported).

gccを入れます。

# apt update && apt install --no-install-recommends -y gcc

3️⃣🅰️ python3.x-devがインストールされていればうまくいく

# python3.10 -m pip install uwsgi
Successfully installed uwsgi-2.0.27
# python3.10 -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.0
uWSGI      2.0.27

Python 3.10のインストール」では、apt-get install python3.10-full python3.10-devしています。
このdevの方がC拡張のビルドに必要2、かつ、uWSGIはC拡張を含むという理解です。
https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa

python#.#-dev: includes development headers for building C extensions

3️⃣🅱️ wheelをインストール

別のアプローチに分岐もできます3
pip install uwsgi中に表示されているメッセージに注目

DEPRECATION: uwsgi is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed.

案内に沿ってwheelを入れます。

再度コンテナ起動!

% docker run --rm -it --platform linux/x86_64 ubuntu-python:3.10 bash
# apt update && apt install --no-install-recommends -y gcc
# python3.10 -m pip install wheel
# python3.10 -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.0
wheel      0.44.0

# python3.10 -m pip install uwsgi
Successfully installed uwsgi-2.0.27
# python3.10 -m pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.0
uWSGI      2.0.27
wheel      0.44.0

体感なのですが、wheelを入れた方がuwsgiのインストールが早い気がします(「legacy 'setup.py install' method」をしていないからかと思っていますが、そもそも計測していないので錯覚説も濃厚です)

終わりに

uWSGIはpythonイメージとubuntuイメージとで、同じようにサクッとインストールできなかったために学びがありました。

  • ubuntuイメージには、pythonX.X-devとgccが必要
  • wheelを入れてからpip install uwsgiをオススメします