INSTALL.md MARKDOWN 311 lines View on github.com → Search inside
1# Installing from Source23**Note: This document describes _building_ Rust _from source_.4This is _not recommended_ if you don't know what you're doing.5If you just want to install Rust, check out the [README.md](README.md) instead.**67The Rust build system uses a Python script called `x.py` to build the compiler,8which manages the bootstrapping process. It lives at the root of the project.9It also uses a file named `bootstrap.toml` to determine various configuration10settings for the build. You can see a full list of options in11`bootstrap.example.toml`.1213The `x.py` command can be run directly on most Unix systems in the following14format:1516```sh17./x.py <subcommand> [flags]18```1920This is how the documentation and examples assume you are running `x.py`.21See the [rustc dev guide][rustcguidebuild] if this does not work on your22platform.2324More information about `x.py` can be found by running it with the `--help` flag25or reading the [rustc dev guide][rustcguidebuild].2627[gettingstarted]: https://rustc-dev-guide.rust-lang.org/getting-started.html28[rustcguidebuild]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html#what-is-xpy2930## Dependencies3132Make sure you have installed the dependencies:3334* `python` 3 or 2.735* `git`36* A C compiler (when building for the host, `cc` is enough; cross-compiling may37  need additional compilers)38* `curl` (not needed on Windows)39* `pkg-config` if you are compiling on Linux and targeting Linux40* `libiconv` (already included with glibc on Debian-based distros)4142To build Cargo, you'll also need OpenSSL (`libssl-dev` or `openssl-devel` on43most Unix distros).4445If building LLVM from source, you'll need additional tools:4647* `g++`, `clang++`, or MSVC with versions listed on48  [LLVM's documentation](https://llvm.org/docs/GettingStarted.html#host-c-toolchain-both-compiler-and-standard-library)49* `ninja`, or GNU `make` 3.81 or later (Ninja is recommended, especially on50  Windows)51* `cmake` version listed on [LLVM's documentation](https://llvm.org/docs/GettingStarted.html#software)52* `libstdc++-static` may be required on some Linux distributions such as Fedora53  and Ubuntu5455On tier 1 or tier 2 with host tools platforms, you can also choose to download56LLVM by setting `llvm.download-ci-llvm = true`.57Otherwise, you'll need LLVM installed and `llvm-config` in your path.58See [the rustc-dev-guide for more info][sysllvm].5960[sysllvm]: https://rustc-dev-guide.rust-lang.org/building/new-target.html#using-pre-built-llvm616263## Building on a Unix-like system6465### Build steps66671. Clone the [source] with `git`:6869   ```sh70   git clone https://github.com/rust-lang/rust.git71   cd rust72   ```7374[source]: https://github.com/rust-lang/rust75762. Configure the build settings:7778   If you're unsure which build configurations to use and need a good default, you79   can run the interactive `x.py setup` command. This will guide you through selecting80   a config profile, setting up the LSP, configuring a Git hook, etc.8182   With `configure` script, you can handle multiple configurations in a single83   command which is useful to create complex/advanced config files. For example:8485   ```sh86   ./configure --build=aarch64-unknown-linux-gnu \87      --enable-full-tools \88      --enable-profiler \89      --enable-sanitizers \90      --enable-compiler-docs \91      --set target.aarch64-unknown-linux-gnu.linker=clang \92      --set target.aarch64-unknown-linux-gnu.ar=/rustroot/bin/llvm-ar \93      --set target.aarch64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \94      --set llvm.link-shared=true \95      --set llvm.thin-lto=true \96      --set llvm.libzstd=true \97      --set llvm.ninja=false \98      --set rust.debug-assertions=false \99      --set rust.jemalloc \100      --set rust.bootstrap-override-lld=true \101      --set rust.lto=thin \102      --set rust.codegen-units=1103   ```104105   If you plan to use `x.py install` to create an installation, you can either106   set `DESTDIR` environment variable to your custom directory path:107108   ```bash109   export DESTDIR=<path>110   ```111112   or set `prefix` and `sysconfdir` in the `[install]` section to your custom113   directory path:114115   ```sh116   ./configure --set install.prefix=<path> --set install.sysconfdir=<path>117   ```118119   When the `DESTDIR` environment variable is present, the `prefix` and120   `sysconfdir` values are combined with the path from the `DESTDIR`121   environment variable.1221233. Build and install:124125   ```sh126   ./x.py build && ./x.py install127   ```128129   When complete, `./x.py install` will place several programs into130   `$PREFIX/bin`: `rustc`, the Rust compiler, and `rustdoc`, the131   API-documentation tool. By default, it will also include [Cargo], Rust's132   package manager. You can disable this behavior by passing133   `--set build.extended=false` to `./configure`.134135[Cargo]: https://github.com/rust-lang/cargo136137### Configure and Make138139This project provides a configure script and makefile (the latter of which just140invokes `x.py`). `./configure` is the recommended way to programmatically141generate a `bootstrap.toml`. `make` is not recommended (we suggest using `x.py`142directly), but it is supported and we try not to break it unnecessarily.143144```sh145./configure146make && sudo make install147```148149`configure` generates a `bootstrap.toml` which can also be used with normal `x.py`150invocations.151152## Building on Windows153154On Windows, we suggest using [winget] to install dependencies by running the155following in a terminal:156157```powershell158winget install -e Python.Python.3159winget install -e Kitware.CMake160winget install -e Git.Git161```162163Then edit your system's `PATH` variable and add: `C:\Program Files\CMake\bin`.164See165[this guide on editing the system `PATH`](https://www.java.com/en/download/help/path.html)166from the Java documentation.167168[winget]: https://github.com/microsoft/winget-cli169170There are two prominent ABIs in use on Windows: the native (MSVC) ABI used by171Visual Studio and the GNU ABI used by the GCC toolchain. Which version of Rust172you need depends largely on what C/C++ libraries you want to interoperate with.173Use the MSVC build of Rust to interop with software produced by Visual Studio174and the GNU build to interop with GNU software built using the MinGW/MSYS2175toolchain.176177### MinGW178179[MSYS2][msys2] can be used to easily build Rust on Windows:180181[msys2]: https://www.msys2.org/1821831. Download the latest [MSYS2 installer][msys2] and go through the installer.1841852. Download and install [Git for Windows](https://git-scm.com/download/win).186   Make sure that it's in your Windows PATH. To enable access to it from within187   MSYS2, edit the relevant `mingw[32|64].ini` file in your MSYS2 installation188   directory and uncomment the line `MSYS2_PATH_TYPE=inherit`.189190   You could install and use MSYS2's version of git instead with `pacman`,191   however this is not recommended as it's excruciatingly slow, and not frequently192   tested for compatibility.1931943. Start a MINGW64 or MINGW32 shell (depending on whether you want 32-bit195   or 64-bit Rust) either from your start menu, or by running `mingw64.exe`196   or `mingw32.exe` from your MSYS2 installation directory (e.g. `C:\msys64`).1971984. From this terminal, install the required tools:199200   ```sh201   # Update package mirrors (may be needed if you have a fresh install of MSYS2)202   pacman -Sy pacman-mirrors203204   # Install build tools needed for Rust. If you're building a 32-bit compiler,205   # then replace "x86_64" below with "i686". 206   # Note that it is important that you do **not** use the 'python2', 'cmake',207   # and 'ninja' packages from the 'msys2' subsystem.208   # The build has historically been known to fail with these packages.209   pacman -S make \210               diffutils \211               tar \212               mingw-w64-x86_64-python \213               mingw-w64-x86_64-cmake \214               mingw-w64-x86_64-gcc \215               mingw-w64-x86_64-ninja216   ```2172185. Navigate to Rust's source code (or clone it), then build it:219220   ```sh221   python x.py setup dist && python x.py build && python x.py install222   ```223224If you want to try the native Windows versions of Python or CMake, you can remove225them from the above pacman command and install them from another source. Follow226the instructions in step 2 to get them on PATH.227228Using Windows native Python can be helpful if you get errors when building LLVM.229You may also want to use Git for Windows, as it is often *much* faster. Turning230off real-time protection in the Windows Virus & Threat protections settings can231also help with long run times (although note that it will automatically turn232itself back on after some time).233234### MSVC235236MSVC builds of Rust additionally require an installation of:237238- Visual Studio 2022 (or later) build tools so `rustc` can use its linker. Older239  Visual Studio versions such as 2019 *may* work but aren't actively tested.240- A recent Windows 10 or 11 SDK.241242The simplest way is to get [Visual Studio], check the "C++ build tools".243244[Visual Studio]: https://visualstudio.microsoft.com/downloads/245246(If you're installing CMake yourself, be careful that "C++ CMake tools for247Windows" doesn't get included under "Individual components".)248249With these dependencies installed, you can build the compiler in a `cmd.exe`250shell with:251252```sh253python x.py setup user254python x.py build255```256257Right now, building Rust only works with some known versions of Visual Studio.258If you have a more recent version installed and the build system doesn't259understand, you may need to force bootstrap to use an older version.260This can be done by manually calling the appropriate vcvars file before running261the bootstrap.262263```batch264CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"265python x.py build266```267268### Specifying an ABI269270Each specific ABI can also be used from either environment (for example, using271the GNU ABI in PowerShell) by using an explicit build triple. The available272Windows build triples are:273- GNU ABI (using GCC)274    - `i686-pc-windows-gnu`275    - `x86_64-pc-windows-gnu`276- The MSVC ABI277    - `i686-pc-windows-msvc`278    - `x86_64-pc-windows-msvc`279280The build triple can be specified by either specifying `--build=<triple>` when281invoking `x.py` commands, or by creating a `bootstrap.toml` file (as described in282[Building on a Unix-like system](#building-on-a-unix-like-system)), and passing283`--set build.build=<triple>` to `./configure`.284285## Building Documentation286287If you'd like to build the documentation, it's almost the same:288289```sh290./x.py doc291```292293The generated documentation will appear under `doc` in the `build` directory for294the ABI used. That is, if the ABI was `x86_64-pc-windows-msvc`, the directory295will be `build\x86_64-pc-windows-msvc\doc`.296297## Notes298299Since the Rust compiler is written in Rust, it must be built by a precompiled300"snapshot" version of itself (made in an earlier stage of development).301As such, source builds require an Internet connection to fetch snapshots, and an302OS that can execute the available snapshot binaries.303304See https://doc.rust-lang.org/nightly/rustc/platform-support.html for a list of305supported platforms.306Only "host tools" platforms have a pre-compiled snapshot binary available; to307compile for a platform without host tools you must cross-compile.308309You may find that other platforms work, but these are our officially supported310build environments that are most likely to work.

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.