Installing Rust on Windows

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.

Getting Rust on Windows takes less time than it did for this window grill.

Getting Rust on Windows takes less time than it did for this window grill.

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.

OpenSSL

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:

  • DEP_OPENSSL_INCLUDE=C:\OpenSSL-Win64\include
  • OPENSSL_INCLUDE_DIR=C:\OpenSSL-Win64\include
  • OPENSSL_LIB_DIR=C:\OpenSSL-Win64\lib\VC
  • OPENSSL_LIBS=ssleay32MT:libeay32MT

Obviously, this assumes that you put OpenSSL in the default installation folder of C:\OpenSSL-Win64.

Freetype

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

Rust Installer

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

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 rustc and 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

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

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!

An Editor

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.


Window Grill” by urbansnaps – kennymc licensed with CC BY 2.0

 

8 Comments. Leave new

  • How does that cmder trick work? Could You specify more detils about it?
    My steps are:
    create PowerShell profile in Documents, create in that directory script from the link, and then add startup line in PS profile file. Unfortunately it deosn;t recognize the name. Should I add the script to PATH?

    Reply
    • Sure – install `cmder`. Problem solved 😉

      cmder replaces the Windows command line – it’s basically a wrapper that includes ConEmu and clink, which make the command line a delight to work with on Windows. cmder also includes a relatively well configured PowerShell profile inside its install path (by default it’s in `%CONEMUDIR%\vendor\`.

      I don’t recall the full context of how the PowerShell profile is executed – your shell is probably not launching in the same folder that contains the `Invoke-Command.ps1` script. You can either execute the script using the full path or add its location to `%PATH%`.

      Reply
      • Let me clear up what I had understood.

        If I don’t want to use shell provided by VC++, but cmder (PowerShell based), I need to load the script on every shell startup.

        I added script location to Path, and now it is loading now correctly – but only in PS, in cmder nothing happens , like it uses it’s own local PS profile. But the compilation (simple hello world) is working. Thus my polite request, could You provide me some use case in which compilation should fail (cause CL VC++ tools not working) ?

        Reply
        • VC++ will only be necessary if you compile a Rust library that needs to link to some C++ code. You could try something that uses Iron (there’s an OpenSSL dependency that needs to link to a DLL).

          cmder uses its own PowerShell profile – everything is normal there. If you want to change which profile it uses, the cmder docs should help you out.

          Reply
  • Ah, thanks, I won’t need at that stage C++ code. Thank You for quick response and help.

    Reply
  • Thanks a lot. The thing with the environment vars was tricky! 😉

    Reply
  • Bob Schnatterly
    2018-02-05 15:42

    I’m new to Rust. I know support in Visual Studio is still in its infancy, but can you actually write UWP apps for Windows? Or just desktop apps? Do you know if Microsoft is possibly looking into making Rust an official language for Windows development? Thanks in advance for your answer(s)…

    Reply
    • Rust in Visual Studio is an extension developed by a single person, IIRC, and they do not work at Microsoft. The current tooling used by the Rust community at large seems to be to develop in VSCode.

      I don’t work at Microsoft, so I don’t know what their stance on Rust is. Sadly, if I did work at Microsoft, I’d probably be bound by an NDA that meant I couldn’t tell you what the official stance on Rust was.

      I don’t do Windows development, so I can’t help with your question about UWP (which I just learned about from your question!)

      I’d suggest asking over at https://users.rust-lang.org to get more accurate answers to your question. Sorry I can’t help more.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu