UPDATE 2020-10-25 This post is over 4 years old. You don’t need it. Go to rustup.rs. Do whatever that tells you. You don’t need the rest of this.
Installing Rust can be as easy as pulling down an installer and double clicking. For developers working with more complex tools or who need to build unsafe C/C++ libraries from source, there’s a little bit of extra work that needs to be done, but it’s nothing that a savvy person can’t handle. Throughout this whole process, make sure you keep installing the right version of libraries - Rust is currently 64-bit only for MSVC. You’ll get bizarro errors at various points if you mess up and try to use 32-bit libraries or prereqs with Rust. Trust me. Again - if all you need is a Rust compiler, head over to https://www.rust-lang.org/ and click “Install”. If you plan on working with natively compiled C/C++ libraries, read on!
Why Do I Need A C Compiler for Rust?
You might need a C/C++ compiler if you’re going to be using libraries that were written in C or C++. As an example - if you’re using a web library like hyper, which uses OpenSSL by default, you’ll need to link to the OpenSSL library on your system.
OK, I Don’t Need a C Compiler. Now What?
Well, just pick a Rust binary and download it. I prefer the Rust MSVC version, but that’s just me. If you’re using rustup.rs, it isn’t hard to change which Rust toolchain you’re using. After you’ve installed your rust compiler, you’re done. There’s nothing more to it that than. Just have fun writing code. You can skip down to Additional Rust Tools to get more of your environment set up.
I Think I Need a C Compiler
If you think you might be linking to native libraries, then you’ll want to go ahead and install one. Sure, it takes up disk space and takes some time up front, but it will save you some time and frustration later on if you already have your compiler ready to go when you need it. [caption id="attachment_1014” align="alignright” width="225”] Getting Rust on Windows takes less time than it did for this window grill.[/caption]
Rust on Windows - Prerequisites
A C Compiler!
You could use GCC, but that involves MSYS and MinGW. Configuring MSYS2 on Windows has bitten me too many times to count, so I avoid it these days. Instead, I used Microsoft’s compiler - MSVC. You can get MSVC through either of these:
Either option is cool - Microsoft make Visual C++ free to use for open source development. At least they did on the day that I was writing this (2016-03-22). Now for the tricky part, you need to get the Visual C++ magic working in your environment. Visual C++ uses a large number of environment variables and has some complex set up scripts to make that easier for you. It’s possible to use the command shell that Microsoft ships with both VS and VC++ (the “Visual C++ 2015 x64 Native Build Tools Command Prompt”). Everything else we do below will work if you use this option. I use
cmder, which uses PowerShell, so I had to jump through one more hoop to get the command line tools working in my environment. I used the recipe Retain Changes to Environment Variables Set by a Batch File in the PowerShell Cookbook. (You should totally buy the book if you like that code snippet, there’s lots more PowerShell in there that you won’t have to write.) Anyway - after saving that recipe to a file, I had to add the following to my PowerShell profile:
Invoke-CmdScript.ps1 "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" amd64.
If you want to use Rust as the server side language for web development, you’re going to need OpenSSL. The Win32 OpenSSL Installation Project maintains several installers. Hit up the URL and scroll down to “Download Win32 OpenSSL”. Pick the 64-bit non-Light version of OpenSSL. Install it. You’re going to get a nagware screen saying “Hey, you should donate some money because we do this for free.” I donated. If you keep using OpenSSL for Windows, you should probably donate, too. Set yourself a reminder and donate if you keep using this set up in a month.
OpenSSL Environment Variables
You’re going to need to make sure the compiler is aware of OpenSSL. You need to configure the following environment variables:
Obviously, this assumes that you put OpenSSL in the default installation folder of C:\OpenSSL-Win64.
Similar instructions are also available for getting Freetype working with MSVC.
Installing Rust on Windows
There are two ways to do this - the Rust installer and using
Head over to the Rust downloads page and grab the “MSVC ABI” installer. This will install a single version of Rust (e.g. 1.7.0). When new versions of Rust are released, you’ll need to decide if you should upgrade and then change an environment variable or two once you’ve decided. It’s not a big deal, but with Rust on a six week release schedule, you might feel a bit frantic with the frequent changes to the language.
Rustup is a tool for managing multiple Rust installations in the same OS. This makes it possible to use different versions of Rust for different projects or to even evaluate the nightly version of Rust to check out new features! Right now, head over to the rustup.rs and get the current Windows installer. Once it downloads, double click and wait. Once that finishes, open up a brand new terminal and run
rustup default stable-x86_64-stable. We need to double check that an environment variable got set. I’m assuming you know how to find these. Once you’ve found your environment variables, check the
PATH to see if
~/.cargo/bin has been added to your path. I use
~ as an abbreviation for your home folder. On my computer, I’m looking to see if
C:\Users\jeremiah\.cargo\bin is on the
PATH. If it’s not there, add it.
Additional Rust Tools
At this point, we’ve got a working Rust installation. We could get started with
cargo and create software. But, there are some additional tools we can install to make our environment a bit nicer.
Rust Source Code
You could read this, but really we just want this for some additional tools we’re going to use. Head over to the Rust downloads page and grab the source. It’s a .tar.gz file, so you might need something like 7-zip installed. I’ll leave that up to you. Once you’ve downloaded the source, extract it to somewhere you probably won’t delete it. I use a src folder under my home - I can find the Rust sources at
C:\users\jeremiah\src\rust-1.7.0. Open up the environment variables again and create a new variable for
RUST_SRC_PATH and set the value to wherever you extracted the Rust source.
Racer is a utility to provide code completion for our software development tools. This can be really helpful as you’re learning a language, or when you’re digging around in code trying to figure out which parameters a function takes. If you’ve put
.cargo\bin in your
PATH and you’ve got the
RUST_SRC_PATH environment variable set up, installing Racer is easy. Just run
cargo install racer and wait for it to install.
rustfmt is a Rust code formatter. You’ll be happy to have this as you’re writing code. Not that you won’t be able to format your code correctly, but because it automatically styles your code. Arguing about code formatting is the worst. You should avoid it.
cargo install rustfmt DONE!
Personally, I’m using Visual Studio Code. I hear that Sublime Text just got good Rust support, and I know that tools exist for Atom and many other editors. The racer page has links to instructions for working with different editors. In Visual Studio Code, I just installed the Rusty Code extension and the vsc-rustfmt extensions.
That’s A Lot Of Stuff!
It might seem like a lot of steps, but it’s relatively easy to walk through and, frankly, it takes less time than waiting for Visual Studio to install itself and 90 other dependencies.