Elixir Development on Windows 10

How to develop with Elixir and Phoenix on the Windows platform.

Often when developing on Windows, at least when not using Microsoft’s own languages and tooling, you run into any number of difficulties that do not seem to plague those in the Linux and Mac world. I’ve had those problems myself using Python, Ruby and others. I am pleased to tell you that developing Elixir/Phoenix apps on Windows is as easy as on other platforms.

Between the work Microsoft has done to become more welcoming to open source languages and the ubiquity of Erlang, you can have a first-rate development environment on a Windows machine. Here are the steps I followed to set up mine.

Step 1: Install Chocolaty

This step is optional, but recommended. Install Chocolaty to make the following steps simple and quick. Rather than download several packages from all over the intertubes, let Chocolaty do that work for you.

We’ll follow the instructions on the Chocolaty install page to install the Chocolaty package manager with which you can install all the tools used below. Open the Windows Command Prompt in administrator mode. To do so, click the Start menu (or press the Windows key) and then type cmd.exe. The item labeled “Command Prompt” should appear at the top of the search list. Right click it and choose “Run as administrator”.

Right click Command Prompt and chose “Run as administrator”

Then enter the following command at the Command Prompt:

@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Say yes when asked and this will install Chocolaty.

You don’t need to use Chocolaty. So I’ll include links to download the regular installers.

Step 2: Install Nodejs

Open the Command Prompt in administrator mode and enter:

choco install nodejs

(or go to https://nodejs.org/en/ to download the normal Windows installer).

Answer ‘yes’ when prompted to run the install script. Once it has completed, restart the Command Prompt to load the new environment variables that the install script added. To confirm node was installed, enter

node -v
npm -v

to check the installed version number. As of this post’s date, the version is 6.4.0 for Node. Check for npm as well by entering ‘npm -v’. I get version 3.10.3.

Step 3: Install Erlang and Elixir

Next, from a Command Prompt in administrator mode, enter

choco install erlang

That will install version 19 of the Erlang/OTP ecosystem. Once the Erlang install has completed, enter

choco install elixir

to start the Elixir install script.

(Nota bene: Currently, Chocolaty installs version 1.3.1. If you want the absolute latest version, you can download the elixir windows installer from elixir-lang.org to get version 1.3.2.)

Whichever you install, restart the Command Prompt and enter

elixir -v

to confirm the install worked. I get Elixir version 1.3.1 and Erlang/OTP 19, as expected.

Step 4: Install Microsoft C++ Build Tools

One pain point with running Elixir on Windows is supporting NIFs — libraries that use native C modules from the OS. Phoenix developers on Windows who have ever tried to use the Comeonin package will have run into this. But the solution is simple. Use Microsoft’s Visual C++ Build Tools package.

(Mind you, if you have a recent version of Visual Studio already installed, you likely already have the build tools installed. I don’t use Visual Studio, so you’re on your own in this case.)

To install the build tools, again, Chocolaty will be useful. Open a administrator command shell and enter

choco install VisualCppBuildTools

(or use the normal installer from Microsoft.) This will install the tools needed to compile module with NIFs on Windows.

To confirm the install, look in

c:\program files (x86)\Microsoft Visual Studio 14.0\VC

You should see a batch file called ‘vcvarsall.bat’. If so, all is well.

Step 5: Install Atom editor

Again, fire up an administrator command shell and enter

choco install atom

(or get the normal installer from Atom.io.)

This will install the Atom editor and its package manager, apm.

Unfortunately, the Chocolaty installation does not update the path environment variable for Atom, so we have to add the path to Atom (and apm) to the system environment.

Open up Control Panel, select System and Security, and then System. On the left, the last item on the list is Advanced System Settings. Click it. Then find Environment Variables near the bottom. Open it. Under System Variables, double click the Path variable and select New. Now add the path to Atom. On my system it is

C:\Users\<your user name>\AppData\Local\atom\bin

where your username is the name of your account on Windows. Once the path is added, restart the Command Prompt and enter

apm -v

to confirm the path can be found. I get version 1.10. Now you can open Atom from a shell prompt as well as use apm to install packages.

Step 6: Install Git for Windows

Spin up an administrator Command Prompt and run

choco install git

(or use the normal installer from the Git for Windows website.)

This will install git. To confirm the install, restart the shell and run

git --version

I get 2.9.3.windows.1 for my git version.

Step 7: Setup up Atom

The next step is to get our editor setup for Elixir/Phoenix development. Here is a curated list of packages to install in Atom to make developing Elixir in Atom on Windows a pleasure. The first list contains the basics you’ll definitely need.

atom-elixir — for autocomplete, goto, and documentation etc.
language-elixir — for syntax highlighting

A number of other packages make working with Elixir/Phoenix more convenient:

phoenix-elixir-snippets
elixir-cmd
linter-elixir-credo

A few other packages not specific to Elixir but that make the Atom editor more useful include the following:

autoclose-html
file-icons
git-plus
git-time-machine
highlight-line
highlight-selected
indent-guide-improved
minimap
minimap-pigments
platformio-ide-terminal

We’ll come back to the Platformio-ide-terminal package a bit later. All of these packages can be installed within Atom through the setting/install page or at the Command Prompt using apm — the atom package manager, e.g., ‘apm install language-elixir’.

Step 8: Get a better Command Prompt

Working at the standard Windows Command Prompt is a less than wonderful experience. Thankfully there are a number of alternatives available that are much better. I’ve found Cmder to be my favourite. I’ll describe how to set up Cmder.

Fire up the command prompt in admin mode and enter the command

choco install cmder

(or get the installer from the Cmder web site. Are you beginning to see why I recommended Chocolaty?)

Once it’s finished, you’ll be able start Cmder from the start menu. One nice thing about Cmder is that it finds all the eligible shells on your system, including the Windows Command Prompt (cmd.exe), Powershell and the Git Bash shell. All of them can be run within Cmder’s own shell. To get the most out of Cmder, we’ll do some tweaking of the settings.

First of all, let’s add Cmder to the environment path. Chocolaty installs Cmder to c:\tools\cmder. So let’s add that path to the system environment. Follow the instructions from above, but this time add:

c:\tools\cmder

as a new entry in your system path. Now we can start Cmder from the command line as well.

Next, start Cmder and open the settings. Here is how I like to set up Cmder to make its use really simple and quickly available. These are my preferences; you may not like them, but I think they’re kinda cool.

Go to the Appearance setting and check the Quake style checkbox. While your there, also tick the Autoclose when lost focus. Fire up Cmder and it now appears as a slide down from the top of the screen. Toggle it with ctl-` (that's a backtick, by the way)

With autoclose set, it rolls back up as soon as it loses focus. Really convenient. Now Cmder will open Quake-style when you hit the key combination ctl-`. Try it out. Note as well that the keyboard shortcut works from within Atom, so you can hit ctl-` in Atom and the console slides down over Atom.

Step 9: Set up your build environment

As mentioned above, Elixir packages that make use of NIFs require a compiler on your system. You’ve already installed it, but let’s make it easier to use.

As it is, each time you wish to compile a module that requires the Microsoft C compiler, you will need to do so in a shell that has the correct environment variables for your platform initialized. That’s what ‘vcvarsall.bat’ is all about. But, it’s a pain to find and load that file every time you compile a module with a NIF.

Let’s make that easier. At the Cmder prompt, navigate to a folder that exists on your path (I simply use ‘c:\tools\cmder’ since I already added that path — but I’m a wee bit lazy). In that folder, create a new batch file — I call mine dev.bat since it’s short and pertinent. Open the dev.bat file in Atom from the Cmder prompt by entering:

atom dev.bat

In that file place the following command:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" AMD64

Please note that the path to vcvarsall.bat must be in quotation marks due to the spaces in the file path. My system is x64 so we send the option ‘AMD64’ (on 32bit OS, you can skip the optional command as it defaults to ‘x86’).

Save dev.bat. Now when you need to compile a module with a NIF, you can start Cmder and then simply run ‘dev.bat’ to set up the proper build environment.

You should have no problems compiling native modules now. But we’ll test that shortly.

Now it’s time for some great extras.

Step 10: Enable ANSI colours

Open up Atom and create a new file called ‘.iex.exs’ and save it to

c:\users\<your user name>

Then add this to the file:

Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [enabled: true, eval_result: [ :cyan, :bright ] ],
default_prompt: [
"\e[G", # ANSI CHA, move cursor to column 1
:blue,
"%prefix", # IEx prompt variable
">", # plain string
:reset
] |> IO.ANSI.format |> IO.chardata_to_string
)

This does two things. First, it enables ANSI colors in your shell for Elixir output, but only after the first command is issued (thanks to OvermindDL1 on Elixirforum for catching that). Second, the eval_result setting adds color to the output from ‘mix test’. Be sure to enable colors when you start ExUnit in your test script:

ExUnit.start(colors: [enabled: true])

Step 11: Start a terminal in atom itself

Earlier I recommended you install Platformio-ide-terminal. Here’s why. This allows you to run a terminal directly in the Atom editor. By default, it uses Powershell, but you can tell it to run whatever terminal you want. We’ll change this to use the ordinary Command Prompt.

(Why the Command Prompt? It’s a workaround, for the moment. Only the regular Command Prompt (cmd.exe) seems to work with vcvarsall.bat. I was not able to compile NIFs from Powershell even after running vcvarsall.bat. Maybe someone else can answer why.)

Go to the setting for the terminal package in Atom, click Packages and then enter ‘terminal’ in the search field. Find Platformio-ide-terminal and click its Settings button.

First we wan’t to replace the default Powershell with Command Prompt. In the Shell Override field, put

C:\windows\system32\cmd.exe

Next, go to the Auto Run Command field. Enter:

dev.bat

Now when you start the terminal in Atom, it will run the Command Prompt and then your dev.bat batch file and automatically setup the build environment in case your compile operation requires a NIF. Awesome! Now you have choice. Use the terminal in Atom or use Cmder — just a keystroke away.

Step 12: Make sure we can compile

Let’s make sure we can compile. Fire up Cmder (or just hit ctl-` if its already running). Create a new folder to put a test mix project in. From that folder, enter:

mix new sample

Once mix is done, enter:

atom sample

Atom will open the folder containing your new project. Next open mix.exs and add the Comeonin dependency:

defp deps do
[ { :comeonin, "~> 2.5" } ]
end

Save the file and spin up your terminal (ctl-` again) or open a terminal in Atom itself.

Navigate to your project folder (‘cd sample’) and pull in the dependency:

mix deps.get

Next, set up the build environment:

dev.bat

Of course, if your using the terminal inside Atom, the dev.bat file has already been run for you. And let’s compile:

mix compile

Everything should have worked and you now have the Comeonin NIF compiled and functioning.

Step 13: Linting

I love on-the-fly linting. So we’re going to set up the Credo linter for Atom.

Credo is a static code analysis tool to help produce clean and consistent code. I love the tool, but you may not, so this step is optional. If you do like it, here’s how to enable it.

First, Credo is an elixir tool. To install Credo for use in our sample project, open mix.exs in Atom again. Add the dependency for Credo:

defp deps do
[
{ :comeonin, "~> 2.5" },
{ :credo, "~> 0.4", only: [:dev, :test] }
]
end

Now pull in the credo module:

mix deps.get

Now at any point, you can run

mix credo

to analyze your code. If you choose, you can also get strict:

mix credo --strict

This generates a longer list of errors and or style problems in your code organized by category. It’s great tool, but Atom makes it even better.

If you haven’t already, use Atom Settings\Packages or apm to install the linter-elixir-credo package for atom:

apm install linter-elixir-credo

Once installed, when you open a project folder with a mix.exs that includes Credo as a dependency, you’ll get on-the-fly linting of your code. To test it out, create a new Elixir file in our sample project. Once open, add the following to the file:

defmodule TestCredo do

end

You quickly see elixir-credo has analyzed your code and Atom has placed suggestions for fixes at the bottom of the window with highlights on lines with code that violate the Credo style guide.

Well, that’s it. Once you’ve set all this up, programming Elixir and Phoenix on Windows is a joy.

Like what you read? Give Rod Watkins a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.