Install FastAPI, Pydantic on termux (설치)

ILOV-IT·2023년 10월 14일
0

Termux에 FastAPI를 설치하는 사람들은 별로 없나보다. 설치에 관한 글을 찾아보기 어렵다. Termux에 FastAPI를 설치해보면 여러 에러와 마주한다. 초기 Windows Arm에 파이썬을 설치하면서도 비슷한 경험을 했는데 비슷했다.

FastAPI는 pydantic과 rust 컴파일러에 의존한다. 즉, 이 둘을 설치하면 문제 없이 설치될 것이다. 다음은 설치하면서 겪은 상황과 해결방법을 연대기순으로 기재한 것이다.

1. pip install FastAPI하면, pydantic-core를 설치하면서 문제가 발생하고, pip install --upgrade pip 하라고 안내한다.

Welcome to Termux!

~ $
~ $
~ $ python -m pip install fastapi
Collecting fastapi
Collecting pydantic-core==2.10.1 (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi)
  Downloading pydantic_core-2.10.1.tar.gz (347 kB)
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━    
  Installing build dependencies ... error
  error: subprocess-exited-with-error
  
  To update pip, run:
  pip install --upgrade pip

2. 하지만 pip install --upgrade pip는 termux에서 에러를 발생시킨다.

~ $
~ $
~ $ pip install --upgrade pip
ERROR: Installing pip is forbidden, this will break the python-pip package (termux).

~ $ python -m pip install python-pip를 한 후 다시 해도 같은 에러 메시지 나옴

3. 대안으로 pydantic을 직접 설치하면 오류 메시지 중간 즈음에, Rust 컴파일러를 찾을 수 없다는 에러가 나온다.

~ $
~ $
~ $ python -m pip install pydantic
Collecting pydantic
Collecting pydantic-core==2.10.1 (from pydantic)
  Using cached pydantic_core-2.10.1.tar.gz (347 kB)
  Installing build dependencies ... error
  error: subprocess-exited-with-error
  .
  .
  .
  writing manifest file 'maturin.egg-info/SOURCES.txt'
  running build_ext
  running build_rust
  error: can't find Rust compiler

4. Rust 컴파일러를 설치한다.

~ $
~ $
~ $ pkg install build-essential (이건 필수인지 불명확)
~ $
~ $
~ $ pkg install rust
The following NEW packages will be installed: rust
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.

5. Rust 설치 후 다시 pydantic을 설치하면 또 오류가 발생한다. 종전과는 다른데 장황한 메시지 속을 유심히 보면 새로운 오류다.

~ $
~ $
~ $ python -m pip install pydantic
Collecting pydantic
.
.
running: ZERO_AR_DATE="1" "aarch64-linux-android-ar" "cq" "/data/data/com.termux/files/usr/tmp/pip-install-ga4k_f2y/maturin_4c7e7394b9b94a15b4efb00902807651/target/release/build/psm-618e8718022be54d/out/libpsm_s.a" "/data/data/com.termux/files/usr/tmp/pip-install-ga4k_f2y/maturin_4c7e7394b9b94a15b4efb00902807651/target/release/build/psm-618e8718022be54d/out/src/arch/aarch_aapcs64.o"

	--- stderr
    error occurred: Failed to find tool. Is `aarch64-linux-android-ar` installed?

6. 'aarch64-linux-android-ar' 오류는 binutils를 설치하면 된다. (참고)

~ $
~ $
~ $ pkg install binutils
The following additional packages will be installed:
  binutils-bin binutils-libs
The following NEW packages will be installed:
  binutils binutils-bin binutils-libs
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 2765 kB of archives.
After this operation, 19.3 MB of additional disk space will be used.
Do you want to continue?

7. pydantic 설치 및 FastAPI 설치 성공

~ $
~ $
~ $ python -m pip install pydantic
Successfully installed

~ $
~ $
~ $ python -m pip install fastapi
Requirement already satisfied: annotated-types>=0.4.0 in /data/data/com.termux/files/usr/lib/python3.11/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (0.6.0)
Requirement already satisfied: pydantic-core==2.10.1 in /data/data/com.termux/files/usr/lib/python3.11/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (2.10.1)
Downloading fastapi-0.103.2-py3-none-any.whl (66 kB)
.
.
Successfully installed anyio-3.7.1 fastapi-0.103.2 idna-3.4 sniffio-1.3.0 starlette-0.27.0

반전은 FastAPI 설치가 완료되었다고 끝난게 아니었다. FastAPI 서버를 실행하면 휴대폰 내에서 localhost로 접속이 되지만, 동일 라우터 내 PC에서 안드로이드 아이피로 접속이 안 된다. 안드로이드 방화벽이 거의 모든 포트를 막고 있어 발생하는 현상으로 확인된다. 미봉책으로는 nmap을 통해 열린 포트를 찾아보는 것인데, 샤오미폰 기준으로 열린 포트는 없었다.

정리하자면, Termux에서 FastAPI 설치는

1) pkg install binutils

2) pkg install rust

3) pkg install build-essential

4) python -m pip install pydantic

5) python -m pip install fastapi

profile
because we know you'll love it

0개의 댓글