29

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
ThangNguyen
  • 501
  • 2
  • 5
  • 13

3 Answers3

63

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).

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
  • 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. – ThangNguyen Nov 19 '17 at 23:12
  • 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. – Francis Gagné Nov 19 '17 at 23:39
  • Francis: I just edited my question. I still got command not found when run either rustc and cargo – ThangNguyen Nov 20 '17 at 00:35
  • When I run `$ rustup default stable-x86_64-pc-windows-gnu bash: rustup: command not found` – ThangNguyen Nov 20 '17 at 00:51
  • 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? – ThangNguyen Nov 20 '17 at 01:34
  • 1
    You'll want to add a line like `export PATH="/c/Users/Thang/.cargo/bin:$PATH"` in your `~/.profile`, then restart your shell. – Francis Gagné Nov 20 '17 at 02:14
  • 2
    just a note, install gcc into msys using following command: `pacman -S mingw-w64-x86_64-toolchain` ( or x86 variant ) – Aleksander Fular Jun 20 '18 at 17:37
  • 1
    +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` though – Zoe Dec 22 '18 at 12:54
  • 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... – Plegeus Oct 08 '22 at 09:53
3

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!
Solaris
  • 674
  • 7
  • 22
user3519714
  • 41
  • 1
  • 4
  • 3
    instead of manually editing settings.toml, you can also just use `rustup default stable-x86_64-pc-windows-gnu` – Mark Jan 27 '22 at 19:50
1

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://stackoverflow.com/a/68835925/4230643

Throw Away Account
  • 2,593
  • 18
  • 21
pclinux
  • 119
  • 1
  • 3