Step by step instruction to install Rust and Cargo for mingw with Msys2?
Asked Answered
A

3

32

I tried to install Rust on Cygwin but failed to be able link with mingw. Now I am trying to install it with Msys2. I already installed Msys2 and Mingw. I tried to follow this wiki page but I got lost at number 2:

Download and install Rust+Cargo using the installer but be sure to disable the Linker and platform libraries option.

Is it referring to the "rustup-init.exe" on the install page? Should I double click to run this file or run it from Msys2? I tried to run from Msys2 and got the options:

1) Proceed with installation (default)  
2) Customize installation  
3) Cancel installation

I don't know what to do next.

Aenneea answered 19/11, 2017 at 16:35 Comment(0)
A
67

The Using Rust on Windows page you linked to dates from before rustup replaced the installer as the default option to install Rust. Installers are still available, but you should use rustup if possible, because it makes it easy to update and to use multiple toolchains at once (e.g. stable, beta and nightly). If you must use the installer, just select the x86_64-pc-windows-gnu installer and follow the step from the Using Rust on Windows page. If you're using rustup, read on.

By default, rustup on Windows installs the compiler and tools targeting the MSVC toolchain, rather than the GNU/MinGW-w64 toolchain. At the initial menu, select 2) Customize installation. When asked for a host triple, enter x86_64-pc-windows-gnu. Then make a choice for the other questions, then proceed with the installation.

Note: If rustup is already installed, then rerunning rustup-init won't actually install the requested toolchain. Instead, run rustup toolchain install stable-x86_64-pc-windows-gnu if you already have the MSVC-based toolchain. Then run rustup default stable-x86_64-pc-windows-gnu to set the GNU-based toolchain as the default.

Rustup will install the MinGW linker and platform libraries automatically (as part of the rust-mingw component) and refuses to let you remove them. If you prefer to use the MinGW linker and libraries you installed with MSYS2, you'll need to create a .cargo/config file (either in your profile directory, i.e. C:\Users\you\.cargo\config, or in your project's directory if this configuration is specific to a project). The contents of that file might look like this:

[target.x86_64-pc-windows-gnu]
linker = "C:\\msys2\\mingw64\\bin\\gcc.exe"
ar = "C:\\msys2\\mingw64\\bin\\ar.exe"

Rustup will modify the PATH environment variable unless you told it not to. However, MSYS2 resets PATH by default when you launch, so when you try to invoke cargo or rustc from your MSYS2 shell, it might not find it. You'll need to edit your .profile/.bash_profile script to set the PATH correctly (you need to prepend /c/Users/yourname/.cargo/bin: to PATH).

Aerostation answered 19/11, 2017 at 18:33 Comment(9)
First thank you very much for helping me. I need to confirm by rustup you mean run ./rustup-init.exe file? I added edit to my original question what I tried.Aenneea
rustup-init.exe installs rustup. As rustup-init says at the end, you may need to restart your shell (or log off and log back in) to update your PATH environment variable. Then the rustc and cargo commands should work.Nainsook
Francis: I just edited my question. I still got command not found when run either rustc and cargoAenneea
When I run $ rustup default stable-x86_64-pc-windows-gnu bash: rustup: command not foundAenneea
I think the issue is at the Path. I cd to .cargo/bin and run ./rustup then it install. Could you please tell me how to edit the PATH of Msys?Aenneea
You'll want to add a line like export PATH="/c/Users/Thang/.cargo/bin:$PATH" in your ~/.profile, then restart your shell.Nainsook
just a note, install gcc into msys using following command: pacman -S mingw-w64-x86_64-toolchain ( or x86 variant )Bulwerlytton
+1 for the rustup default part -- it's the part I missed for it to work. I assumed it'd be automatically picked based on the console (similarily to how cmake works, though that's a different language). The compiler errors were fixed with rustup default thoughUnbounded
I have been looking for this for a year, I kid you not... It'd be nice if cargo could tell that one binary was compiled with one toolchain and another by another toolchain...Perigon
N
5

my problem resolved by following way

  1. Run rustup toolchain install stable-x86_64-pc-windows-gnu
  2. Second open .rustup folder
  3. Open settings.toml file
  4. Change first line with: default_toolchain = "stable-x86_64-pc-windows-gnu" done!
Nylons answered 12/11, 2021 at 10:40 Comment(1)
instead of manually editing settings.toml, you can also just use rustup default stable-x86_64-pc-windows-gnuMardellmarden
G
2

I wrote a complete guide on how to

install Rust on windows with Visual Studio Code and MSYS2 MinGW on the page found here:

https://mcmap.net/q/454252/-rust-installation-on-windows

Generic answered 20/8, 2021 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.