Compiling Elixir Packages on Windows — The “optimal” Setup

Spaceman
5 min readJun 20, 2021
Photo by Sigmund on Unsplash

Setting up Elixir to be able to compile packages, like Argon2 or bcrypt, can be a challenge. The fastest way to get everything up and running is to do a complete install of Visual Studio, but you don’t need all that to just compile some Elixir packages. After completing this tutorial, Elixir is able to compile any normal packages with just the bare minimum of build tools installed.

Getting Build Tools for Visual Studio 2019

First you need to get the installer for just the Build Tools for Visual Studio 2019. You can get them from https://visualstudio.microsoft.com/downloads/. There you need to expand the “Tools for Visual Studio 2019” section and look for “Build Tools for Visual Studio 2019”.

Where to download Build Tools for Visual Studio 2019
Where to download Build Tools for Visual Studio 2019

Installing the required components

Once you have downloaded and started the installer, you need to select the following from the “Individual components” tab:

  • Windows Universal C Runtime
  • C++ Build Tools core features
  • MSVC v142 — VS 2019 C++ x64/x86 build tools (Latest)
  • Windows 10 SDK (10.0.19041.0)
  • C++ CMake tools for Windows
What components to install
What components to install

Install them after you have selected the correct ones.

Setting up environment variables

Now you have everything you need installed, but building any Elixir package would still fail. This is because Visual Studio Installer doesn’t set up the environment variables automatically. There are two options for setting them. The first option needs to be done, even if you want to go with option 2.

Option 1

The first option uses vcvars64.bat that came with the Build Tools installation. This batch file sets all the environment variables with just one command, but it only lasts until you close the terminal in which the batch file was executed.

First of all, you need to locate vcvars64.bat. In my version of the Build Tools it’s located in C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build folder, but might be in another folder for you. If that is the case, just use the File Explorer search feature to find it. You might get multiple results, but they should all work the same way.

Once the file is located, navigate to the folder that has the vcvars64.bat file in it and execute the file.

Output of vcvars64.bat
Output of vcvars64.bat

Now this terminal has all the required environment variables set. You can confirm it by running the command nmake.

Screenshot of a terminal showing the output of “nmake” command
Running the “nmake” command

If the command shows an error saying that “nmake” is not recognized, then something went wrong and you need double check all the previous steps. Otherwise, you are now ready to compile things. If you want everything to work in Visual Studio Code, you need to start it from the terminal in which you ran the vcvars64.bat file. You can do so by running “code” or “code-insiders” in the terminal.

If you are planning on also using Option 2, don’t close this terminal window yet.

Option 2

This second option sets the environment variables manually, but the commands always work everywhere. You must have already done Option 1 successfully, before attempting this option.

Open the environment variable editor by searching for “environment” in the Windows 10 search and open “Edit the system environment variables”.

Here you can edit the environment variables

Once the “System Properties” window opens click the “Environment Variables…” button at the lower right corner. There under “User variables for <your username>”, click “New”. Now you need to get contents of the INCLUDE environment variable from the terminal you used in Option 1. To do that, you need to run “echo %INCLUDE%”.

Screenshot of a terminal showing the output of “echo %INCLUDE%” command
Output of “echo %INCLUDE%”

Copy the long list of paths that gets printed to the terminal, open the window that opened after clicking “New” in the last step. Set “variable name” to “INCLUDE” and paste the list of paths to “variable value”.

Screenshot of a window showing how to set up the INCLUDE environment variable
INCLUDE environment variable

Click “OK”. Now you need to repeat these same steps for the “LIB” variable.

Once you have “INCLUDE” and “LIB” environment variables set up, the last thing we need is to add one path to the Path environment variable. The path for me is “C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30037\bin\Hostx64\x64”, but it might be different for you. If that is the case, use the search feature of File Explorer to search for “nmake”.

“Path” environment variable is already defined, so you just need to select it, click “Edit”, click “New”, and finally paste the path and press enter. After that, you can close all the environment variable related windows.

Once you open a new terminal, you should be able to run the “nmake” command like you could in Option 2. Also, you should get no errors when compiling Elixir packages. This time it works out of the box with Visual Studio Code and new terminals, without running any extra commands.

Conclusion

It’s quite a hassle to get compiled Elixir packages working on Windows, but once you get it working, you never have to do it again. The way I showed you was the “optimal” way, but you can get compilation working on Windows by many other means as well, including Cygwin and WLS.

This is my first tutorial of this kind, so any feedback is appreciated.

--

--