安装 Ray#

Run Quickstart on Anyscale

Ray 目前官方支持 Linux 上的 x86_64、aarch64 (ARM) 以及 Apple Silicon (M1) 硬件。Windows 上的 Ray 目前处于 beta 阶段。

官方发布版本#

通过 Wheel 安装#

您可以通过选择最适合您的用例的选项,从 PyPI 在 Linux、Windows 和 macOS 上安装 Ray 的最新官方版本。

适用于机器学习应用

pip install -U "ray[data,train,tune,serve]"

# For reinforcement learning support, install RLlib instead.
# pip install -U "ray[rllib]"

适用于通用 Python 应用

pip install -U "ray[default]"

# If you don't want Ray Dashboard or Cluster Launcher, install Ray with minimal dependencies instead.
# pip install -U "ray"

命令

安装的组件

pip install -U "ray"

核心

pip install -U "ray[default]"

核心, Dashboard, 集群启动器

pip install -U "ray[data]"

核心, Data

pip install -U "ray[train]"

核心, Train

pip install -U "ray[tune]"

核心, Tune

pip install -U "ray[serve]"

核心, Dashboard, 集群启动器, Serve

pip install -U "ray[serve-grpc]"

核心, Dashboard, 集群启动器, 支持 gRPC 的 Serve

pip install -U "ray[rllib]"

核心, Tune, RLlib

pip install -U "ray[all]"

核心, Dashboard, 集群启动器, Data, Train, Tune, Serve, RLlib。不推荐使用此选项。请改为按下方所示指定所需的额外组件。

提示

您可以组合安装额外的组件。例如,要安装支持 Dashboard、集群启动器和 Train 的 Ray,您可以运行

pip install -U "ray[default,train]"

每日构建版本 (Nightlies)#

您可以通过以下链接安装每日构建的 Ray wheel。这些每日构建版本通过自动化测试进行测试,但未经过完整的发布流程。要安装这些 wheel,请使用以下 pip 命令和 wheel

# Clean removal of previous install
pip uninstall -y ray
# Install Ray with support for the dashboard + cluster launcher
pip install -U "ray[default] @ LINK_TO_WHEEL.whl"

# Install Ray with minimal dependencies
# pip install -U LINK_TO_WHEEL.whl

注意

在 Windows 上,对多节点 Ray 集群的支持目前是实验性的且未经测试。如果您遇到问题,请在 ray-project/ray#issues 提交报告。

注意

每日构建的 wheel 默认启用使用情况统计收集(可以禁用),包括通过 ray.init() 启动的本地集群和通过 CLI 启动的远程集群。

从特定提交安装#

您可以使用以下模板安装 master 分支上任何特定提交的 Ray wheel。您需要指定提交哈希、Ray 版本、操作系统和 Python 版本

pip install https://s3-us-west-2.amazonaws.com/ray-wheels/master/{COMMIT_HASH}/ray-{RAY_VERSION}-{PYTHON_VERSION}-{PYTHON_VERSION}-{OS_VERSION}.whl

例如,以下是 Ray 3.0.0.dev0 对应 Python 3.9、macOS 的 wheel,提交哈希为 4f2ec46c3adb6ba9f412f09a9732f436c4a5d0c9

pip install https://s3-us-west-2.amazonaws.com/ray-wheels/master/4f2ec46c3adb6ba9f412f09a9732f436c4a5d0c9/ray-3.0.0.dev0-cp39-cp39-macosx_10_15_x86_64.whl

wheel 文件名格式有一些细微差异;最好与每日构建版本部分列出的 URL 中的格式匹配。以下是差异摘要

  • 对于 macOS,早于 2021 年 8 月 7 日的提交文件名中将包含 macosx_10_13,而不是 macosx_10_15

M1 Mac (Apple Silicon) 支持#

Ray 支持运行 Apple Silicon 芯片的机器(例如 M1 Mac)。多节点集群未经测试。要开始本地 Ray 开发

  1. 安装miniforge

    • wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh

    • bash Miniforge3-MacOSX-arm64.sh

    • rm Miniforge3-MacOSX-arm64.sh # 清理。

  2. 确保您正在使用 miniforge 环境(您应该在终端中看到 (base))。

    • source ~/.bash_profile

    • conda activate

  3. 像平常一样安装 Ray。

    • pip install ray

Windows 支持#

Windows 支持处于 Beta 阶段。Ray 支持在 Windows 上运行,但有以下注意事项(只有第一项是 Ray 特有的,其余在任何使用 Windows 的地方都成立)

  • 多节点 Ray 集群未经测试。

  • 文件名在 Windows 上很棘手,并且可能仍然有一些地方 Ray 假定使用 UNIX 文件名而不是 Windows 文件名。下游包中也可能存在这种情况。

  • 已知 Windows 上的性能较慢,因为在 Windows 上打开文件比在其他操作系统上慢得多。这会影响日志记录。

  • Windows 没有写时复制 (copy-on-write) 的 fork 模型,因此启动新进程可能需要更多内存。

将您遇到的任何问题提交到GitHub

在 Arch Linux 上安装 Ray#

注意:在 Arch Linux 上安装 Ray 未经 Ray 项目开发者测试。

Ray 可通过 Arch 用户仓库 (AUR) 在 Arch Linux 上作为 python-ray 包获取。

您可以按照Arch Wiki 上的说明手动安装软件包,或者使用AUR 助手,例如 yay(推荐,安装更方便),如下所示

yay -S python-ray

要讨论与此软件包相关的任何问题,请参阅 python-ray 的 AUR 页面此处的评论区。

从 conda-forge 安装#

Ray 也可以作为 conda 包在 Linux 和 Windows 上安装。

# also works with mamba
conda create -c conda-forge python=3.9 -n ray
conda activate ray

# Install Ray with support for the dashboard + cluster launcher
conda install -c conda-forge "ray-default"

# Install Ray with minimal dependencies
# conda install -c conda-forge ray

要安装 Ray 库,可以使用上面的 pipconda/mamba

conda install -c conda-forge "ray-data"   # installs Ray + dependencies for Ray Data
conda install -c conda-forge "ray-train"  # installs Ray + dependencies for Ray Train
conda install -c conda-forge "ray-tune"   # installs Ray + dependencies for Ray Tune
conda install -c conda-forge "ray-serve"  # installs Ray + dependencies for Ray Serve
conda install -c conda-forge "ray-rllib"  # installs Ray + dependencies for Ray RLlib

有关 Conda-forge 上可用 ray 库的完整列表,请查看 https://anaconda.org/conda-forge/ray-default

注意

Ray conda 包由社区维护,而非 Ray 团队。在使用 conda 环境时,建议在新创建的环境中使用 pip install ray 从 PyPi 安装 Ray。

从源码构建 Ray#

对于大多数 Ray 用户来说,从 pip 安装应该就足够了。

但是,如果您需要从源码构建,请按照这些构建 Ray 的说明进行操作。

Docker 镜像#

用户可以从 rayproject/rayDocker Hub 仓库拉取 Docker 镜像。这些镜像包含 Ray 和所有必需的依赖项。它附带了 anaconda 和各种版本的 Python。

镜像使用 tagged 格式为 {Ray 版本}[-{Python 版本}][-{平台}]Ray version 标签可以是以下之一

Ray 版本标签

描述

latest

最新的 Ray 发布版本。

x.y.z

特定的 Ray 发布版本,例如 2.31.0

nightly

最新的 Ray 开发构建版本(Github master 的近期提交)

可选的 Python version 标签指定了镜像中的 Python 版本。Ray 支持的所有 Python 版本均可用,例如 py39py310py311。如果未指定,该标签将指向 Ray 版本支持的最低 Python 版本的镜像。

可选的 Platform 标签指定了镜像适用的平台

平台标签

描述

-cpu

这些基于 Ubuntu 镜像。

-cuXX

这些基于指定 CUDA 版本的 NVIDIA CUDA 镜像。它们需要 Nvidia Docker Runtime。

-gpu

特定 -cuXX 标签镜像的别名。

<无标签>

-cpu 标签镜像的别名。

示例:对于基于 Python 3.9 且不支持 GPU 的 nightly 镜像,标签是 nightly-py39-cpu

如果您想调整这些镜像的某些方面并在本地构建它们,请参考以下脚本

cd ray
./build-docker.sh

通过列表查看镜像

docker images

输出应该类似于以下内容

REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
rayproject/ray                      dev                 7243a11ac068        2 days ago          1.11 GB
rayproject/base-deps                latest              5606591eeab9        8 days ago          512  MB
ubuntu                              22.04               1e4467b07108        3 weeks ago         73.9 MB

在 Docker 中启动 Ray#

首先启动部署容器。

docker run --shm-size=<shm-size> -t -i rayproject/ray

<shm-size> 替换为适合您系统的限制值,例如 512M2G。一个好的估算值是使用您可用内存的大约 30%(这是 Ray 内部用于对象存储的空间)。这里的 -t-i 选项是支持容器交互式使用所必需的。

如果您使用 GPU 版本的 Docker 镜像,请记住添加 --gpus all 选项。在以下命令中将 <ray-version> 替换为您目标 Ray 版本

docker run --shm-size=<shm-size> -t -i --gpus all rayproject/ray:<ray-version>-gpu

注意: Ray 需要大量共享内存,因为每个对象存储都将其所有对象保存在共享内存中,因此共享内存的数量将限制对象存储的大小。

您现在应该会看到一个类似于以下的提示符

root@ebc78f68d100:/ray#

测试安装是否成功#

要测试安装是否成功,请尝试运行一些测试。这假设您已经克隆了 git 仓库。

python -m pytest -v python/ray/tests/test_mini.py

已安装的 Python 依赖项#

我们的 Docker 镜像预装了 Ray 及其库所需的 Python 依赖项。

我们发布了 Python 3.9 ray Docker 镜像中安装的依赖项。

Ray 版本: 2.46.0 (c3dd2ca)

aiohappyeyeballs==2.6.1
aiohttp==3.11.16
aiohttp-cors==0.7.0
aiosignal==1.3.1
annotated-types==0.6.0
anyio==3.7.1
archspec @ file:///home/conda/feedstock_root/build_artifacts/archspec_1737352602016/work
async-timeout==4.0.3
attrs==25.1.0
boltons @ file:///home/conda/feedstock_root/build_artifacts/boltons_1733827268945/work
boto3==1.26.76
botocore==1.29.76
Brotli @ file:///home/conda/feedstock_root/build_artifacts/brotli-split_1725267488082/work
cachetools==5.5.2
certifi @ file:///home/conda/feedstock_root/build_artifacts/certifi_1739515848642/work/certifi
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
cloudpickle==2.2.0
colorama @ file:///home/conda/feedstock_root/build_artifacts/colorama_1733218098505/work
colorful==0.5.5
conda @ file:///home/conda/feedstock_root/build_artifacts/conda_1744220015350/work/conda-src
conda-libmamba-solver @ file:///home/conda/feedstock_root/build_artifacts/conda-libmamba-solver_1745834476052/work/src
conda-package-handling @ file:///home/conda/feedstock_root/build_artifacts/conda-package-handling_1736345463896/work
conda_package_streaming @ file:///home/conda/feedstock_root/build_artifacts/conda-package-streaming_1729004031731/work
cryptography==42.0.5
cupy-cuda12x==13.1.0
Cython==0.29.37
Deprecated==1.2.18
distlib==0.3.7
distro @ file:///home/conda/feedstock_root/build_artifacts/distro_1734729835256/work
dm-tree==0.1.8
exceptiongroup==1.2.2
Farama-Notifications==0.0.4
fastapi==0.115.0
fastrlock==0.8.2
filelock==3.17.0
flatbuffers==23.5.26
frozendict @ file:///home/conda/feedstock_root/build_artifacts/frozendict_1728841359971/work
frozenlist==1.4.1
fsspec==2023.5.0
google-api-core==1.34.0
google-api-python-client==2.111.0
google-auth==2.23.4
google-auth-httplib2==0.1.1
google-oauth==1.0.1
googleapis-common-protos==1.61.0
grpcio==1.66.2
gymnasium==1.0.0
h11==0.14.0
h2 @ file:///home/conda/feedstock_root/build_artifacts/h2_1733298745555/work
hpack @ file:///home/conda/feedstock_root/build_artifacts/hpack_1733299205993/work
httplib2==0.20.4
httptools==0.6.4
hyperframe @ file:///home/conda/feedstock_root/build_artifacts/hyperframe_1733298771451/work
idna==3.7
importlib-metadata==6.11.0
Jinja2==3.1.2
jmespath==1.0.1
jsonpatch @ file:///home/conda/feedstock_root/build_artifacts/jsonpatch_1733814567314/work
jsonpointer @ file:///home/conda/feedstock_root/build_artifacts/jsonpointer_1725302957584/work
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
libmambapy @ file:///home/conda/feedstock_root/build_artifacts/mamba-split_1745833723603/work/libmambapy
lz4==4.3.3
markdown-it-py==2.2.0
MarkupSafe==2.1.3
mdurl==0.1.2
memray==1.10.0
menuinst @ file:///home/conda/feedstock_root/build_artifacts/menuinst_1731146985033/work
msgpack==1.0.7
multidict==6.0.5
numpy==1.26.4
opencensus==0.11.3
opencensus-context==0.1.3
opentelemetry-api==1.26.0
opentelemetry-exporter-otlp==1.26.0
opentelemetry-exporter-otlp-proto-common==1.26.0
opentelemetry-exporter-otlp-proto-grpc==1.26.0
opentelemetry-exporter-otlp-proto-http==1.26.0
opentelemetry-proto==1.26.0
opentelemetry-sdk==1.26.0
opentelemetry-semantic-conventions==0.47b0
ormsgpack==1.7.0
packaging==23.0
pandas==1.5.3
platformdirs==3.11.0
pluggy @ file:///home/conda/feedstock_root/build_artifacts/pluggy_1733222765875/work
prometheus-client==0.19.0
propcache==0.3.0
protobuf==3.20.3
psutil==5.9.6
py-spy==0.4.0
pyarrow==14.0.2
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycosat @ file:///home/conda/feedstock_root/build_artifacts/pycosat_1732588390546/work
pycparser==2.21
pydantic==2.9.2
pydantic_core==2.23.4
Pygments==2.18.0
pyOpenSSL==24.2.1
pyparsing==3.1.1
PySocks @ file:///home/conda/feedstock_root/build_artifacts/pysocks_1733217236728/work
python-dateutil==2.8.2
python-dotenv==1.1.0
pytz==2022.7.1
PyYAML==6.0.1
ray @ file:///home/ray/ray-2.46.0-cp39-cp39-manylinux2014_x86_64.whl#sha256=0bbcda7817735e3524f52819c1a7dce1ac1c8cce488bb111cecfbaa514ad780c
redis==4.4.2
referencing==0.36.2
requests==2.31.0
rich==13.3.2
rpds-py==0.22.3
rsa==4.7.2
ruamel.yaml @ file:///home/conda/feedstock_root/build_artifacts/ruamel.yaml_1736248037007/work
ruamel.yaml.clib @ file:///home/conda/feedstock_root/build_artifacts/ruamel.yaml.clib_1728724456970/work
s3transfer==0.6.2
scipy==1.11.4
six==1.16.0
smart-open==6.2.0
sniffio==1.3.1
starlette==0.37.2
tensorboardX==2.6.2.2
tqdm @ file:///home/conda/feedstock_root/build_artifacts/tqdm_1735661334605/work
typing_extensions==4.12.2
uritemplate==4.1.1
urllib3==1.26.19
uvicorn==0.22.0
uvloop==0.19.0
virtualenv==20.29.1
watchfiles==0.19.0
websockets==11.0.3
wrapt==1.14.1
yarl==1.18.3
zipp==3.19.2
zstandard==0.23.0

使用 Maven 安装 Ray Java#

注意

所有 Ray Java API 都是实验性的,仅由社区支持。

在使用 Maven 安装 Ray Java 之前,您应该先使用 pip install -U ray 安装 Ray Python。请注意,Ray Java 和 Ray Python 的版本必须匹配。另请注意,如果您想安装 Ray Java snapshot 版本,还需要 nightly Ray python wheel。

中央仓库中查找最新的 Ray Java 发布版本。要在您的应用中使用最新的 Ray Java 发布版本,请在 pom.xml 中添加以下条目

<dependency>
  <groupId>io.ray</groupId>
  <artifactId>ray-api</artifactId>
  <version>${ray.version}</version>
</dependency>
<dependency>
  <groupId>io.ray</groupId>
  <artifactId>ray-runtime</artifactId>
  <version>${ray.version}</version>
</dependency>

最新的 Ray Java snapshot 可以在sonatype 仓库中找到。要在您的应用中使用最新的 Ray Java snapshot 版本,请在 pom.xml 中添加以下条目

<!-- only needed for snapshot version of ray -->
<repositories>
  <repository>
    <id>sonatype</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    <releases>
      <enabled>false</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>io.ray</groupId>
    <artifactId>ray-api</artifactId>
    <version>${ray.version}</version>
  </dependency>
  <dependency>
    <groupId>io.ray</groupId>
    <artifactId>ray-runtime</artifactId>
    <version>${ray.version}</version>
  </dependency>
</dependencies>

注意

当您运行 pip install 安装 Ray 时,也会安装 Java jars。上述依赖项仅用于构建您的 Java 代码并在本地模式下运行您的代码。

如果您想在多节点 Ray 集群中运行您的 Java 代码,最好在打包代码时排除 Ray jars,以避免 Ray 版本(通过 pip install 安装的 Ray 和 maven 依赖项)不匹配时出现 jar 冲突。

安装 Ray C++#

注意

所有 Ray C++ API 都是实验性的,仅由社区支持。

您可以按如下方式安装和使用 Ray C++ API。

pip install -U ray[cpp]

# Create a Ray C++ project template to start with.
ray cpp --generate-bazel-project-template-to ray-template

注意

如果您从源码构建 Ray,在运行您的应用之前,请从 cpp/example/.bazelrc 文件中移除构建选项 build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"。相关问题是这个