Skip to content

Lesson 2 ยท Getting Started

Install MLflow and Log Your First Run

Install MLflow in a virtual environment, log a first run, open the web UI, and understand where your tracking data is saved on your own computer.

In this lesson you set up MLflow on your own machine, write a short script that logs a run, and open the web interface to look at it. Run these steps in your own terminal, because the runner on this site cannot install packages.

Create a clean environment

A virtual environment is a private folder of Python packages for one project. It stops projects from breaking each other. As of December 2026, the latest release on PyPI is MLflow 3.18.0, and it needs Python 3.10 or later. Check PyPI for the current requirement.

Terminal
python -m venv .venv
source .venv/bin/activate      # Windows PowerShell: .venv\Scripts\Activate.ps1
pip install mlflow scikit-learn
mlflow --version

For real projects, pin the version in your requirements file, such as mlflow==3.18.0. Then your teammates get the same behavior you tested.

Write your first script

Save this as first_run.py:

Python
import mlflow

mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("first-steps")

with mlflow.start_run(run_name="hello"):
    mlflow.log_param("greeting", "hello")
    mlflow.log_metric("length", 5)
    mlflow.set_tag("stage", "learning")

Run it with python first_run.py. Each line has one job:

Call What it does
set_tracking_uri Chooses where tracking data goes
set_experiment Picks the experiment, creating it if needed
start_run Opens a run; the with block closes it
log_param Stores one input setting
log_metric Stores one numeric result
set_tag Adds a label for searching

Use the with form. If your code raises an error inside the block, the run is still closed and marked as failed instead of staying open.

Open the web interface

From the same folder, start a local server that reads the same database:

Terminal
mlflow server --backend-store-uri sqlite:///mlflow.db --port 5000

Open http://localhost:5000 in a browser. Choose the first-steps experiment, then open the run named hello. You should find your parameter, metric and tag. Stop the server with Ctrl+C.

Where is the data?

According to the official docs, since MLflow 3.7.0 the default tracking backend is a SQLite database file (mlflow.db) instead of the older ./mlruns folder. If MLflow finds an existing ./mlruns folder with experiment data, it keeps using it. Setting the tracking URI explicitly, as in the script, removes the guesswork: your script and your server point to the same place.

The most common beginner problem is "my run is not in the UI". Usually the script wrote to one location and the server read another, often because they were started from different folders. A relative SQLite path depends on the current folder.

You can also set the location outside your code with the MLFLOW_TRACKING_URI environment variable. That is handy when the same script runs on a laptop and on a team server:

Terminal
export MLFLOW_TRACKING_URI=http://localhost:5000
python first_run.py

If you want the variable to control the location, remove the set_tracking_uri line, because a URI set in code takes priority.

What happens when you log

This stand-in imitates the life of a run: open it, add values, and close it with a status. Try changing fail to True.

Python Stand-in: the life of a run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With fail = True the run keeps the parameter you logged before the crash. That is useful: a failed run still tells you what you tried.

A short checklist

  1. Create and activate a virtual environment.
  2. Install MLflow and note the version.
  3. Set the tracking URI on purpose.
  4. Choose an experiment name that describes the problem.
  5. Add mlflow.db, mlruns/ and .venv/ to .gitignore.

Tracking files can contain data samples and file contents, so do not push them to a public repository by accident.

Run it a second time

Run first_run.py again. You now have two runs in the same experiment, even though the code did not change. MLflow never overwrites an earlier run when you start a new one. That is the point: history accumulates, and you decide later what to delete.

To tell runs apart, change something meaningful and record it:

Python
import sys
import mlflow

mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("first-steps")

greeting = sys.argv[1] if len(sys.argv) > 1 else "hello"

with mlflow.start_run(run_name=greeting):
    mlflow.log_param("greeting", greeting)
    mlflow.log_metric("length", len(greeting))

Now run python first_run.py "good morning". In the UI, add the greeting parameter and the length metric as columns, and compare the rows.

Troubleshooting installation

Problem What to check
ModuleNotFoundError: mlflow Is the virtual environment active in this terminal?
mlflow command not found Try python -m mlflow --version inside the environment
Port already in use Choose another port with --port
Install fails on dependencies Check your Python version and upgrade pip
Page does not load Is the server still running in its terminal?

Corporate computers sometimes block package downloads or local ports. In that case, ask your IT team which package index and ports you may use. Do not turn off security tools to get around the block.

Use a notebook if you like

All the same calls work in a Jupyter notebook. Be careful with cells: if a cell calls start_run without a with block and then fails, the run can stay open, and the next call complains that a run is already active. Call mlflow.end_run() to close it, or use the with form from the start.

Before you move on

Make sure you can answer three questions about your own setup: Which MLflow version is installed? Where is the tracking data stored? How do you start the interface that reads it? Write the answers in a README file in your project folder. A teammate, or you in a few weeks, can then get running without guessing.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why is the form with mlflow.start_run() inside a with block recommended?

    The context manager ends the run and records its status when the block exits.
  2. 2. Your script ran, but the UI shows no run. What should you check first?

    Different folders or URIs mean the server is reading a different store.
  3. 3. Which environment variable sets the tracking location?

    MLFLOW_TRACKING_URI tells the client where to send tracking data.

Summary

  • Install MLflow in a virtual environment and pin the version.
  • set_experiment, start_run and the log_ functions are enough for a first run.
  • mlflow server shows runs in a browser.
  • Set the tracking URI explicitly so the script and server agree.

Next: Runs, Experiments and Stores

Your progress is saved in this browser. No account needed.