快速入门

本快速入门指南将帮助您在本地机器上启动一个 Airflow 单实例。

注意

成功安装需要 Python 3 环境。从 Airflow 3.2.0 起,Airflow 支持 Python 3.10、3.11、3.12、3.13、3.14。

官方支持的安装方式是使用 pipuv

运行 pip install apache-airflow[EXTRAS]==AIRFLOW_VERSION --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-AIRFLOW_VERSION/constraints-PYTHON_VERSION.txt",例如 pip install "apache-airflow[celery]==3.0.0" --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-3.0.0/constraints-3.10.txt" 以可重现的方式安装 Airflow。您也可以使用速度更快的 uv——只需在命令前添加 uv

虽然使用其他工具如 poetrypip-tools 也取得了一些成功,但它们的工作流与 pipuv 不同——尤其是在约束文件与需求管理方面。目前不支持通过 Poetrypip-tools 安装。

如果您希望使用这些工具安装 Airflow,应该使用约束文件并将其转换为您的工具所需的相应格式和工作流。

本指南将帮助您使用 uv 快速搭建 Apache Airflow,uv 是一个快速且现代的 Python 环境与依赖管理工具。uv 简化了安装过程,提供流畅的设置体验。

如果您使用 Windows,则必须使用 WSL2(Windows 的 Linux 环境)。

wsl --install
  1. 设置 Airflow 主目录(可选):

    Airflow 需要一个主目录,默认使用 ~/airflow,但如果您愿意可以设置其他位置。使用 AIRFLOW_HOME 环境变量来告知 Airflow 所需的位置。应在安装 Airflow 之前设置此环境变量,以便安装过程知道将必要文件存放在哪里。

    export AIRFLOW_HOME=~/airflow
    
  2. 使用 uv 在虚拟环境中安装 Airflow,因为它是一种更快的方案,会自动隐式创建 venv。这是相较于使用 pipvenv 更高效的替代方案。

    安装 uv:uv 安装指南

    关于使用 uv 创建虚拟环境,请参阅此文档:使用 uv 创建和维护本地虚拟环境

使用 pipvenv 安装时,请执行以下步骤。在 Debian/Ubuntu 系统上,Python 可能会强制使用外部管理的环境(PEP 668),因此在运行 pip install 命令之前请先创建虚拟环境。

# For Windows after WSL2 install, restart computer, then in WSL Ubuntu terminal
sudo apt update
sudo apt install python3-pip python3-venv

bash # Go to Linux home directory (not Windows mount)
cd ~

# Create airflow directory
mkdir -p ~/airflow
cd ~airflow

# Create virtual environment
python3 -m venv airflow_venv

# Activate
source airflow_venv/bin/activate

# Upgrade pip
pip install --upgrade pip

# Install Airflow with correct Python version constraints
pip install apache-airflow[celery]==3.1.0 --constraint https://raw.githubusercontent.com/apache/airflow/constraints-3.1.0/constraints-3.12.txt

# Verify installation
airflow version
  1. 使用约束文件安装 Airflow,约束文件依据我们提供的 URL 确定。

    AIRFLOW_VERSION=3.1.1
    
    # Extract the version of Python you have installed. If you're currently using a Python version that is not supported by Airflow, you may want to set this manually.
    # See above for supported versions.
    PYTHON_VERSION="$(python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
    
    CONSTRAINT_URL="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERSION}/constraints-${PYTHON_VERSION}.txt"
    # For example this would install 3.0.0 with python 3.10: https://raw.githubusercontent.com/apache/airflow/constraints-3.2.0/constraints-3.10.txt
    
    uv pip install "apache-airflow==${AIRFLOW_VERSION}" --constraint "${CONSTRAINT_URL}"
    
  2. 运行 Airflow Standalone

    airflow standalone 命令会初始化数据库、创建用户并启动所有组件。

    airflow standalone
    
  3. 访问 Airflow UI

    在浏览器中打开 localhost:8080 并使用终端中显示的管理员账号登录。 在首页启用 example_bash_operator DAG。

执行这些命令后,Airflow 将创建 $AIRFLOW_HOME 文件夹,并生成默认的 “airflow.cfg” 文件,帮助您快速启动。您可以通过环境变量覆盖默认配置,参见 配置参考。可以在 $AIRFLOW_HOME/airflow.cfg 中查看该文件,或通过 UI 的 Admin → Configuration 菜单查看。Web 服务器的 PID 文件将存放在 $AIRFLOW_HOME/airflow-api-server.pid,如果由 systemd 启动,则位于 /run/airflow/airflow-webserver.pid

随着您扩展并将 Airflow 部署到生产环境,您会希望摆脱这里使用的 standalone 命令,改为分别运行各组件。更多信息请参阅 生产部署

下面列出一些命令,可触发若干任务实例。执行以下命令时,您应该能在 example_bash_operator DAG 中看到作业状态的变化。

# run your first task instance
airflow tasks test example_bash_operator runme_0 2015-01-01
# run a backfill over 2 days
airflow backfill create --dag-id example_bash_operator \
    --from-date 2015-01-01 \
    --to-date 2015-01-02

如果您想手动运行 Airflow 的各个组件,而不是使用一键式的 standalone 命令,可以改为运行

airflow db migrate

airflow users create \
    --username admin \
    --firstname Peter \
    --lastname Parker \
    --role Admin \
    --email spiderman@superhero.org

airflow api-server --port 8080

airflow scheduler

airflow dag-processor

airflow triggerer

注意

airflow users 命令仅在启用了 Flask AppBuilder (FAB) 认证管理器 时可用。

下一步?

从此可以前往 教程 部分获取更多示例,或在已准备好动手实践时前往 操作指南 部分。

此条目是否有帮助?