development.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. .. _building-ray:
  2. Building Ray from Source
  3. =========================
  4. For a majority of Ray users, installing Ray via the latest wheels or pip package is usually enough. However, you may want to build the latest master branch.
  5. .. tip::
  6. If you are only editing Python files, follow instructions for :ref:`python-develop` to avoid long build times.
  7. If you already followed the instructions in :ref:`python-develop` and want to switch to the Full build in this section, you will need to first uninstall.
  8. .. contents::
  9. :local:
  10. Clone the repository
  11. --------------------
  12. To build Ray locally you will need to have the Git repository, so first, fork it on GitHub. Then you can clone it to your machine:
  13. .. tab-set::
  14. .. tab-item:: Git SSH
  15. To clone the repository using Git with SSH (the default) run:
  16. .. code-block:: shell
  17. git clone git@github.com:[your username]/ray.git
  18. .. tab-item:: Git HTTPS
  19. To clone the repository using Git with HTTPS run:
  20. .. code-block:: shell
  21. git clone https://github.com/[your username]/ray.git
  22. Then you can enter into the Ray git repository directory:
  23. .. code-block:: shell
  24. cd ray
  25. Next make sure you connect your repository to the upstream (main project) Ray repository. This will allow you to push your code to your repository when proposing changes (in pull requests) while also pulling updates from the main project.
  26. .. tab-set::
  27. .. tab-item:: Git SSH
  28. To connect your repository using SSH (the default) run the command:
  29. .. code-block:: shell
  30. git remote add upstream git@github.com:ray-project/ray.git
  31. .. tab-item:: Git HTTPS
  32. To connect your repository using HTTPS run the command:
  33. .. code-block:: shell
  34. git remote add upstream https://github.com/ray-project/ray.git
  35. Every time you want to update your local version you can pull the changes from the main repository:
  36. .. code-block:: shell
  37. # Checkout the local master branch
  38. git checkout master
  39. # Pull the latest changes from the main repository
  40. git pull upstream master
  41. Prepare the Python environment
  42. ------------------------------
  43. You probably want some type of Python virtual environment. For example, you can use Anaconda's ``conda``.
  44. .. tab-set::
  45. .. tab-item:: conda
  46. Set up a ``conda`` environment named ``ray``:
  47. .. code-block:: shell
  48. conda create -c conda-forge python=3.9 -n ray
  49. Activate your virtual environment to tell the shell/terminal to use this particular Python:
  50. .. code-block:: shell
  51. conda activate ray
  52. You need to activate the virtual environment every time you start a new shell/terminal to work on Ray.
  53. .. tab-item:: venv
  54. Use Python's integrated ``venv`` module to create a virtual environment called ``venv`` in the current directory:
  55. .. code-block:: shell
  56. python -m venv venv
  57. This contains a directory with all the packages used by the local Python of your project. You only need to do this step once.
  58. Activate your virtual environment to tell the shell/terminal to use this particular Python:
  59. .. code-block:: shell
  60. source venv/bin/activate
  61. You need to activate the virtual environment every time you start a new shell/terminal to work on Ray.
  62. Creating a new virtual environment can come with older versions of ``pip`` and ``wheel``. To avoid problems when you install packages, use the module ``pip`` to install the latest version of ``pip`` (itself) and ``wheel``:
  63. .. code-block:: shell
  64. python -m pip install --upgrade pip wheel
  65. .. _python-develop:
  66. Building Ray (Python Only)
  67. --------------------------
  68. .. note:: Unless otherwise stated, directory and file paths are relative to the project root directory.
  69. RLlib, Tune, Autoscaler, and most Python files do not require you to build and compile Ray. Follow these instructions to develop Ray's Python files locally without building Ray.
  70. 1. Make sure you have a clone of Ray's git repository as explained above.
  71. 2. Make sure you activate the Python (virtual) environment as described above.
  72. 3. Pip install the **latest Ray wheels.** See :ref:`install-nightlies` for instructions.
  73. .. code-block:: shell
  74. # For example, for Python 3.8:
  75. pip install -U https://s3-us-west-2.amazonaws.com/ray-wheels/latest/ray-3.0.0.dev0-cp38-cp38-manylinux2014_x86_64.whl
  76. 4. Replace Python files in the installed package with your local editable copy. We provide a simple script to help you do this: ``python python/ray/setup-dev.py``. Running the script will remove the ``ray/tune``, ``ray/rllib``, ``ray/autoscaler`` dir (among other directories) bundled with the ``ray`` pip package, and replace them with links to your local code. This way, changing files in your git clone will directly affect the behavior of your installed Ray.
  77. .. code-block:: shell
  78. # This replaces `<package path>/site-packages/ray/<package>`
  79. # with your local `ray/python/ray/<package>`.
  80. python python/ray/setup-dev.py
  81. .. note:: [Advanced] You can also optionally skip creating symbolic link for directories of your choice.
  82. .. code-block:: shell
  83. # This links all folders except "_private" and "dashboard" without user prompt.
  84. python setup-dev.py -y --skip _private dashboard
  85. .. warning:: Do not run ``pip uninstall ray`` or ``pip install -U`` (for Ray or Ray wheels) if setting up your environment this way. To uninstall or upgrade, you must first ``rm -rf`` the pip-installation site (usually a directory at the ``site-packages/ray`` location), then do a pip reinstall (see the command above), and finally run the above ``setup-dev.py`` script again.
  86. .. code-block:: shell
  87. # To uninstall, delete the symlinks first.
  88. rm -rf <package path>/site-packages/ray # Path will be in the output of `setup-dev.py`.
  89. pip uninstall ray # or `pip install -U <wheel>`
  90. Preparing to build Ray on Linux
  91. -------------------------------
  92. .. tip:: If you are only editing Tune/RLlib/Autoscaler files, follow instructions for :ref:`python-develop` to avoid long build times.
  93. To build Ray on Ubuntu, run the following commands:
  94. .. code-block:: bash
  95. # Add a PPA containing gcc-9 for older versions of Ubuntu.
  96. sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
  97. sudo apt-get update
  98. sudo apt-get install -y build-essential curl gcc-9 g++-9 pkg-config psmisc unzip
  99. sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 \
  100. --slave /usr/bin/g++ g++ /usr/bin/g++-9 \
  101. --slave /usr/bin/gcov gcov /usr/bin/gcov-9
  102. # Install Bazel.
  103. ci/env/install-bazel.sh
  104. # Install node version manager and node 14
  105. $(curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh)
  106. nvm install 14
  107. nvm use 14
  108. For RHELv8 (Redhat EL 8.0-64 Minimal), run the following commands:
  109. .. code-block:: bash
  110. sudo yum groupinstall 'Development Tools'
  111. sudo yum install psmisc
  112. In RedHat, install Bazel manually from this link: https://docs.bazel.build/versions/main/install-redhat.html
  113. Preparing to build Ray on MacOS
  114. -------------------------------
  115. .. tip:: Assuming you already have Brew and Bazel installed on your mac and you also have grpc and protobuf installed on your mac consider removing those (grpc and protobuf) for smooth build through the commands ``brew uninstall grpc``, ``brew uninstall protobuf``. If you have built the source code earlier and it still fails with errors like ``No such file or directory:``, try cleaning previous builds on your host by running the commands ``brew uninstall binutils`` and ``bazel clean --expunge``.
  116. To build Ray on MacOS, first install these dependencies:
  117. .. code-block:: bash
  118. brew update
  119. brew install wget
  120. # Install Bazel.
  121. ray/ci/env/install-bazel.sh
  122. Building Ray on Linux & MacOS (full)
  123. ------------------------------------
  124. Make sure you have a local clone of Ray's git repository as explained above. You will also need to install NodeJS_ to build the dashboard.
  125. Enter into the project directory, for example:
  126. .. code-block:: shell
  127. cd ray
  128. Now you can build the dashboard. From inside of your local Ray project directory enter into the dashboard client directory:
  129. .. code-block:: bash
  130. cd dashboard/client
  131. Then you can install the dependencies and build the dashboard:
  132. .. code-block:: bash
  133. npm ci
  134. npm run build
  135. After that, you can now move back to the top level Ray directory:
  136. .. code-block:: shell
  137. cd ../..
  138. Now let's build Ray for Python. Make sure you activate any Python virtual (or conda) environment you could be using as described above.
  139. Enter into the ``python/`` directory inside of the Ray project directory and install the project with ``pip``:
  140. .. code-block:: bash
  141. # Install Ray.
  142. cd python/
  143. # You may need to set the following two env vars if your platform is MacOS ARM64(M1).
  144. # See https://github.com/grpc/grpc/issues/25082 for more details.
  145. # export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
  146. # export GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
  147. pip install -e . --verbose # Add --user if you see a permission denied error.
  148. The ``-e`` means "editable", so changes you make to files in the Ray
  149. directory will take effect without reinstalling the package.
  150. .. warning:: if you run ``python setup.py install``, files will be copied from the Ray directory to a directory of Python packages (``/lib/python3.6/site-packages/ray``). This means that changes you make to files in the Ray directory will not have any effect.
  151. .. tip::
  152. If your machine is running out of memory during the build or the build is causing other programs to crash, try adding the following line to ``~/.bazelrc``:
  153. ``build --local_ram_resources=HOST_RAM*.5 --local_cpu_resources=4``
  154. The ``build --disk_cache=~/bazel-cache`` option can be useful to speed up repeated builds too.
  155. .. _NodeJS: https://nodejs.org
  156. Building Ray on Windows (full)
  157. ------------------------------
  158. **Requirements**
  159. The following links were correct during the writing of this section. In case the URLs changed, search at the organizations' sites.
  160. - Bazel 4.2 (https://github.com/bazelbuild/bazel/releases/tag/4.2.1)
  161. - Microsoft Visual Studio 2019 (or Microsoft Build Tools 2019 - https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019)
  162. - JDK 15 (https://www.oracle.com/java/technologies/javase-jdk15-downloads.html)
  163. - Miniconda 3 (https://docs.conda.io/en/latest/miniconda.html)
  164. - git for Windows, version 2.31.1 or later (https://git-scm.com/download/win)
  165. You can also use the included script to install Bazel:
  166. .. code-block:: bash
  167. # Install Bazel.
  168. ray/ci/env/install-bazel.sh
  169. # (Windows users: please manually place Bazel in your PATH, and point
  170. # BAZEL_SH to MSYS2's Bash: ``set BAZEL_SH=C:\Program Files\Git\bin\bash.exe``)
  171. **Steps**
  172. 1. Enable Developer mode on Windows 10 systems. This is necessary so git can create symlinks.
  173. 1. Open Settings app;
  174. 2. Go to "Update & Security";
  175. 3. Go to "For Developers" on the left pane;
  176. 4. Turn on "Developer mode".
  177. 2. Add the following Miniconda subdirectories to PATH. If Miniconda was installed for all users, the following paths are correct. If Miniconda is installed for a single user, adjust the paths accordingly.
  178. - ``C:\ProgramData\Miniconda3``
  179. - ``C:\ProgramData\Miniconda3\Scripts``
  180. - ``C:\ProgramData\Miniconda3\Library\bin``
  181. 3. Define an environment variable ``BAZEL_SH`` to point to ``bash.exe``. If git for Windows was installed for all users, bash's path should be ``C:\Program Files\Git\bin\bash.exe``. If git was installed for a single user, adjust the path accordingly.
  182. 4. Bazel 4.2 installation. Go to Bazel 4.2 release web page and download
  183. bazel-4.2.1-windows-x86_64.exe. Copy the exe into the directory of your choice.
  184. Define an environment variable BAZEL_PATH to full exe path (example:
  185. ``set BAZEL_PATH=C:\bazel\bazel.exe``). Also add the Bazel directory to the
  186. ``PATH`` (example: ``set PATH=%PATH%;C:\bazel``)
  187. 5. Download ray source code and build it.
  188. .. code-block:: shell
  189. # cd to the directory under which the ray source tree will be downloaded.
  190. git clone -c core.symlinks=true https://github.com/ray-project/ray.git
  191. cd ray\python
  192. pip install -e . --verbose
  193. Environment variables that influence builds
  194. --------------------------------------------
  195. You can tweak the build with the following environment variables (when running ``pip install -e .`` or ``python setup.py install``):
  196. - ``RAY_INSTALL_JAVA``: If set and equal to ``1``, extra build steps will be executed
  197. to build java portions of the codebase
  198. - ``RAY_INSTALL_CPP``: If set and equal to ``1``, ``ray-cpp`` will be installed
  199. - ``RAY_DISABLE_EXTRA_CPP``: If set and equal to ``1``, a regular (non -
  200. ``cpp``) build will not provide some ``cpp`` interfaces
  201. - ``SKIP_BAZEL_BUILD``: If set and equal to ``1``, no Bazel build steps will be
  202. executed
  203. - ``SKIP_THIRDPARTY_INSTALL``: If set will skip installation of third-party
  204. python packages
  205. - ``RAY_DEBUG_BUILD``: Can be set to ``debug``, ``asan``, or ``tsan``. Any
  206. other value will be ignored
  207. - ``BAZEL_ARGS``: If set, pass a space-separated set of arguments to Bazel. This can be useful
  208. for restricting resource usage during builds, for example. See https://bazel.build/docs/user-manual
  209. for more information about valid arguments.
  210. - ``IS_AUTOMATED_BUILD``: Used in CI to tweak the build for the CI machines
  211. - ``SRC_DIR``: Can be set to the root of the source checkout, defaults to
  212. ``None`` which is ``cwd()``
  213. - ``BAZEL_SH``: used on Windows to find a ``bash.exe``, see below
  214. - ``BAZEL_PATH``: used on Windows to find ``bazel.exe``, see below
  215. - ``MINGW_DIR``: used on Windows to find ``bazel.exe`` if not found in ``BAZEL_PATH``
  216. Installing additional dependencies for development
  217. --------------------------------------------------
  218. Dependencies for the linter (``scripts/format.sh``) can be installed with:
  219. .. code-block:: shell
  220. pip install -r python/requirements_linters.txt
  221. Dependencies for running Ray unit tests under ``python/ray/tests`` can be installed with:
  222. .. code-block:: shell
  223. pip install -c python/requirements.txt -r python/requirements_test.txt
  224. Requirement files for running Ray Data / ML library tests are under ``python/requirements/``.
  225. Fast, Debug, and Optimized Builds
  226. ---------------------------------
  227. Currently, Ray is built with optimizations, which can take a long time and
  228. interfere with debugging. To perform fast, debug, or optimized builds, you can
  229. run the following (via ``-c`` ``fastbuild``/``dbg``/``opt``, respectively):
  230. .. code-block:: shell
  231. bazel build -c fastbuild //:ray_pkg
  232. This will rebuild Ray with the appropriate options (which may take a while).
  233. If you need to build all targets, you can use ``"//:all"`` instead of
  234. ``//:ray_pkg``.
  235. To make this change permanent, you can add an option such as the following
  236. line to your user-level ``~/.bazelrc`` file (not to be confused with the
  237. workspace-level ``.bazelrc`` file):
  238. .. code-block:: shell
  239. build --compilation_mode=fastbuild
  240. If you do so, remember to revert this change, unless you want it to affect
  241. all of your development in the future.
  242. Using ``dbg`` instead of ``fastbuild`` generates more debug information,
  243. which can make it easier to debug with a debugger like ``gdb``.
  244. Building the Docs
  245. -----------------
  246. To learn more about building the docs refer to `Contributing to the Ray Documentation`_.
  247. .. _Contributing to the Ray Documentation: https://docs.ray.io/en/master/ray-contribute/docs.html
  248. Using a local repository for dependencies
  249. -----------------------------------------
  250. If you'd like to build Ray with custom dependencies (for example, with a
  251. different version of Cython), you can modify your ``.bzl`` file as follows:
  252. .. code-block:: python
  253. http_archive(
  254. name = "cython",
  255. ...,
  256. ) if False else native.new_local_repository(
  257. name = "cython",
  258. build_file = "bazel/BUILD.cython",
  259. path = "../cython",
  260. )
  261. This replaces the existing ``http_archive`` rule with one that references a
  262. sibling of your Ray directory (named ``cython``) using the build file
  263. provided in the Ray repository (``bazel/BUILD.cython``).
  264. If the dependency already has a Bazel build file in it, you can use
  265. ``native.local_repository`` instead, and omit ``build_file``.
  266. To test switching back to the original rule, change ``False`` to ``True``.
  267. .. _`PR template`: https://github.com/ray-project/ray/blob/master/.github/PULL_REQUEST_TEMPLATE.md
  268. Troubleshooting
  269. ---------------
  270. If importing Ray (``python3 -c "import ray"``) in your development clone results
  271. in this error:
  272. .. code-block:: python
  273. Traceback (most recent call last):
  274. File "<string>", line 1, in <module>
  275. File ".../ray/python/ray/__init__.py", line 63, in <module>
  276. import ray._raylet # noqa: E402
  277. File "python/ray/_raylet.pyx", line 98, in init ray._raylet
  278. import ray.memory_monitor as memory_monitor
  279. File ".../ray/python/ray/memory_monitor.py", line 9, in <module>
  280. import psutil # noqa E402
  281. File ".../ray/python/ray/thirdparty_files/psutil/__init__.py", line 159, in <module>
  282. from . import _psosx as _psplatform
  283. File ".../ray/python/ray/thirdparty_files/psutil/_psosx.py", line 15, in <module>
  284. from . import _psutil_osx as cext
  285. ImportError: cannot import name '_psutil_osx' from partially initialized module 'psutil' (most likely due to a circular import) (.../ray/python/ray/thirdparty_files/psutil/__init__.py)
  286. Then you should run the following commands:
  287. .. code-block:: bash
  288. rm -rf python/ray/thirdparty_files/
  289. python3 -m pip install setproctitle