Salah satu package installer dari Python adalah pip atau pip3 tergantung dari varian Linux yang digunakan. Dibanyak sistem yang mengaplikasikan read-only filesystem, seperti pada Docker atau Kubernetes

Contoh error yang muncul

$ pip install --user --no-cache-dir requests
ERROR: Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/cli/base_command.py", line 180, in exc_logging_wrapper
    status = run_func(*args)
             ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/cli/req_command.py", line 245, in wrapper
    return func(self, options, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/commands/install.py", line 333, in run
    build_tracker = self.enter_context(get_build_tracker())
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/cli/command_context.py", line 27, in enter_context
    return self._main_context.enter_context(context_provider)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/contextlib.py", line 526, in enter_context
    result = _enter(cm)
             ^^^^^^^^^^
  File "/usr/local/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/operations/build/build_tracker.py", line 46, in get_build_tracker
    root = ctx.enter_context(TempDirectory(kind="build-tracker")).path
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/utils/temp_dir.py", line 137, in __init__
    path = self._create(kind)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pip/_internal/utils/temp_dir.py", line 177, in _create
    path = os.path.realpath(tempfile.mkdtemp(prefix=f"pip-{kind}-"))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/tempfile.py", line 373, in mkdtemp
    prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/tempfile.py", line 126, in _sanitize_params
    dir = gettempdir()
          ^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/tempfile.py", line 315, in gettempdir
    return _os.fsdecode(_gettempdir())
                        ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/tempfile.py", line 308, in _gettempdir
    tempdir = _get_default_tempdir()
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/tempfile.py", line 223, in _get_default_tempdir
    raise FileNotFoundError(_errno.ENOENT,
FileNotFoundError: [Errno 2] No usable temporary directory found in ['/tmp', '/var/tmp', '/usr/tmp', '/']

solusi dibawah ini hanya bisa dijalankan bila kita memiliki directory yang bisa read-write, kalo hanya terbatas di read-only, buat base image dockernya terlebih dahulu, baru terapkan policy read-only di filesystem.

pip on ready-only system

Disini kita memiliki folder /logs yang memiliki akses read-write (baca tulis), jadi kita akan memanfaatkan folder tersebut sebagai temporary folder dan tempat untuk menginstall library dari pip

1. Set TMPDIR

Ganti temporary directory ke folder yang bisa dibaca-tulis (read-write)

export TMPDIR=/logs

jalankan kembali perintah pip

$ pip install -user --no-cache-dir requests
ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/.local'

2. Set TARGET Folder

Tambahkan parameter --target

$ pip install --help
...
...
  -t, --target <dir>          Install packages into <dir>. By default this
                              multiple platforms supported by the target
                              abis supported by the target interpreter.
...
...

tetapi karena --target tidak bisa digabung dengan --user, maka kita ubah command-nya menjadi

pip install --target /logs --no-cache-dir requests

pip install berhasil

Terakhir set PYTHONPATH=/logs

export PYTHONPATH=/logs

Leave a comment

Your email address will not be published. Required fields are marked *