skip to content

On a fresh VM, why does `mlflow models serve` fail on a missing pyenv or take minutes to start, and what does `--env-manager` change?

level: seniorimportance: should knowfreq 6%

answer

  1. not your shell's interpreter
  2. virtualenv unless told otherwise
  3. pyenv supplies the recorded Python
  4. a cache keyed by requirements
  5. local, uv or conda instead

basics

~20 s

mlflow models serve defaults to --env-manager virtualenv: before starting, it builds an environment for the model's recorded Python version and requirements, using pyenv for the Python. uv builds it faster, conda uses conda, and local skips building entirely.

solid answer

~40 s

With the default `--env-manager virtualenv`, MLflow downloads the model, reads its recorded Python version and requirements, and builds a virtual environment for them before launching uvicorn inside it. It checks for pyenv first and stops with "Could not find the pyenv binary" when pyenv is missing. The first start installs every dependency, which is why it takes minutes; the environment is cached under `~/.mlflow/envs` (`MLFLOW_ENV_ROOT`), keyed by the Python version and requirements, so later starts with the same requirements reuse it. `uv` builds the same kind of environment much faster, `conda` builds from the model's `conda.yaml`, and `local` runs in the current interpreter, which is fastest but correct only if that interpreter already holds the model's dependencies. `mlflow models prepare-env` builds the environment ahead of time.

go deeper

for a junior

Recall that the default environment manager is virtualenv, that it needs pyenv, and that local, uv and conda are the alternatives.

for a middle

Explain how the environment is built from the recorded Python and requirements, where it is cached, and why the first start is slow while later ones are not.

for a senior

Choose the manager per host: uv or prepare-env on a VM, local only inside an image that installed the requirements at build time, and spot binding and worker defaults that break a first deployment.

for a principal

Decide where dependency resolution happens across a fleet of model servers — at provisioning, at image build or at start — and what each choice costs in start-up time, reproducibility and supply-chain exposure.

## Why the server builds an environment A logged MLflow model records the **Python version** and **pip requirements** it was trained with. `mlflow models serve` does not trust the shell's interpreter to match them. By default it creates a separate environment for the model, installs those requirements, and starts the scoring server (uvicorn plus the FastAPI app) **inside** it. The **`--env-manager`** option decides how that environment is built. The files MLflow writes at logging time belong to model packaging; this answer is about what the option does to a serving process. ## What each environment manager does | `--env-manager` | Builds | Needs on the host | Start-up cost | |---|---|---|---| | `virtualenv` (default) | a venv for the recorded Python and requirements | **pyenv**, for the Python version | slow first start, cached afterwards | | `uv` | the same kind of environment, built with uv | the `uv` command on the PATH | much faster builds; MLflow itself recommends it | | `conda` | an environment from the model's `conda.yaml` | conda | slow; MLflow warns about Anaconda's terms of service | | `local` | nothing | the model's dependencies already installed | none, but correctness is on you | When the option is omitted, the CLI help says it defaults to `virtualenv`. Before building, the virtualenv path checks for **pyenv** and raises "Could not find the pyenv binary" with a link to pyenv's installation guide. That is the first failure on a fresh VM: nothing is wrong with the model, the host simply lacks the tool the default manager needs. ## The first start and the cache The slow first start is the dependency install: for a scikit-learn valuation model, pandas, NumPy, scikit-learn and MLflow itself are installed into the new environment before the server binds its port. MLflow names the environment by a hash of the Python version and requirements and keeps it under **`~/.mlflow/envs`**, which `MLFLOW_ENV_ROOT` moves. Consequences: - a restart with the same model requirements finds the environment and starts quickly; - a new model version with a changed pin builds a new environment, and the start is slow again; - a VM whose home directory is wiped on reboot rebuilds every time; - **`mlflow models prepare-env -m <uri>`** builds the environment ahead of time, so the next serve start is fast. Each distinct set of requirements produces a complete environment on disk. A VM that has served many model versions accumulates one environment per requirement set under the root directory, so pruning old ones belongs in the host's maintenance, and pointing `MLFLOW_ENV_ROOT` at a volume with room for them avoids filling the home directory. ## `local`: fast and unchecked `--env-manager local` (also what the older `--no-conda` flag selects) runs the server in the current interpreter. It is the right choice inside an image that already installed the model's requirements, which is what a `build-docker` image does at build time. On a shared VM it is a trap: if the interpreter's scikit-learn differs from the training one, the model may fail to load, or load and score differently, with no environment step to catch it. ## The other flags on the same command The flags beside `--env-manager` also have defaults that surprise people: - **`--host`** defaults to `127.0.0.1`, so the web form on another machine cannot connect until you pass `--host 0.0.0.0`. The help text says this is network binding, not a security setting. - **`--port`** defaults to `5000`. - **`--workers`** (or `MLFLOW_MODELS_WORKERS`) defaults to **1** uvicorn worker for `mlflow models serve`. - **`--timeout`** defaults to 60 seconds per request. - **`--install-mlflow`** installs the CLI's own MLflow version into the built environment. `GET /version` reports the MLflow version importable in the serving environment, which can differ from the one that ran the CLI. ## Choosing on the VM, then in the container 1. On a VM serving one model long-term, install **uv** and pass `--env-manager uv`, or keep `virtualenv` with pyenv installed; run `prepare-env` during provisioning so a restart never installs packages. 2. In a container, install the dependencies at build time and run with `local`, so start-up is predictable and nothing is downloaded at boot. 3. Avoid `local` on a general-purpose interpreter shared with other tools.

  • The server started on the VM, but the web form on another host gets connection refused. Which `mlflow models serve` default is responsible?
    `--host` defaults to `127.0.0.1`, so uvicorn listens only on the loopback interface. Pass `--host 0.0.0.0` (or set `MLFLOW_HOST`) to listen on all interfaces. The CLI help says this controls binding only, so access control still belongs in front of the server.
  • How do you keep the first request after a VM reboot from waiting on a package install?
    Build the environment before the server is needed: `mlflow models prepare-env -m <uri>` with the same `--env-manager` creates it, and serve then reuses it from `~/.mlflow/envs` (or `MLFLOW_ENV_ROOT`), provided that directory survives the reboot. `uv` shortens any rebuild that still happens.

The default environment manager is like a caterer who brings their own kitchen to every venue: the first event takes a while to set up, the next one at the same venue reuses the kitchen, and local means cooking in whatever kitchen is already there.

  • mlflow models serve runs in the shell's current Python by default
  • The pyenv error means the model artifact is corrupted
  • Each start rebuilds the environment from scratch even when unchanged
  • --env-manager local re-checks the model's pinned versions
  • The server listens on all interfaces by default