Ubuntu の apt を意図的におかしくしてみる

Ubuntu の apt を意図的に動かせないようにしてみる

環境

  • WSL 上の Ubuntu 20.04.3 LTS
cat /etc/issue

確認していく

  1. 現状の python3 のバージョン
    python3
    1. 結果
      Python 3.8.10 (default, Jun 2 2021, 10:49:15)
      [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information.
      >>>
  2. python3.9 のインストール
    sudo apt install python3.9
  3. python3,python3.8,python3.9 の場所確認
    sudo find / -name python3*
    1. 結果
      /usr/bin/python3
      /usr/bin/python3.8
      /usr/bin/python3.9
  4. python3 で実行される python を python3.9 にする
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
    1. 結果
      update-alternatives: renaming python3 link from /usr/bin/python to /usr/bin/python3
  5. 再度 python3 のバージョン確認
    python3
    1. 結果
      Python 3.9.5 (default, Nov 23 2021, 15:27:38)
      [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
      >>>
    2. しっかり python3.9 になっているのを確認
  6. apt update が使えなくなる
    sudo apt update
    • 結果
      Get:1 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]
      Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [20.9 kB]
      Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
      Get:4 https://aquasecurity.github.io/trivy-repo/deb focal InRelease [3053 B]
      Get:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
      Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
      Get:7 https://aquasecurity.github.io/trivy-repo/deb focal/main amd64 Packages [376 B]
      Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
      Get:9 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1885 kB] 
      Get:10 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2268 kB] 
      Get:11 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [394 kB] 
      Get:12 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [309 kB] 
      Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.1 kB] 
      Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1457 kB] 
      Get:15 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [11.4 kB] 
      Get:16 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1360 kB] 
      Get:17 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [206 kB] 
      Get:34 http://archive.ubuntu.com/ubuntu focal-backports/main Translation-en [16.3 kB] 
      Get:35 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 c-n-f Metadata [1420 B] 
      Get:36 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [25.0 kB] 
      Get:37 http://archive.ubuntu.com/ubuntu focal-backports/universe Translation-en [16.3 kB] 
      Get:38 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B] Fetched 10.9 MB in 4s (3020 kB/s) Traceback (most recent call last): File "/usr/lib/cnf-update-db", line 8, in <module> from CommandNotFound.db.creator import DbCreator File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg' Reading package lists... Done 
      E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
      E: Sub-process returned an error code
    • はい
  7. python のバージョンを戻す方法
    1. python3.8 を登録
      sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
    2. ここで python3.8 を選ぶ
      sudo update-alternatives --config python3

まとめ

  • python3 のライブラリを別の言語から呼び出すときとかにデフォルトの python3 を参照するとかしてるかつ python3 のバージョンはある程度以上じゃないといけないとかあるとこういうことが起こるかも
  • 強引な手段として python3.8 で呼んでいるバイナリをコピーして python3.9 が呼んでいる名前にするとかいう手段もある(この世の終わり)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA