Setting Up an Elixir Development Environment
By: Jeffrey Borchert
Tags: Getting Started, Programming, Elixir, Software Development, Vscode

I’ve recently begun learning Elixir and the core concepts of functional programming. The Elixir community seems very passionate about transmitting their language to more programmers so there is no shortage of awesome tutorials and educational sources out there. Even so, I thought I would share a simple project setup with Mix and Visual Studio Code on Windows 10 for those just getting started in Elixir.

Install Elixir:

The officially recommended installation method for Windows is through the installer package from

Download and run the installer, which will start the Elixir Setup Wizard.

If you accept all the default options, the latest release version of Elixir and Erlang OTP(also required) will be installed and the required executables will be added to your Windows PATH.

Once your installation is complete, you can check that it was successful with the elixir -v command on the command line.

Interactive Mode:

From here you can start experimenting with Elixir commands right away with the Interactive Elixir executable; just enter iex at the command line. This will enter Elixir’s interactive mode where you can execute Elixir statements on the fly. To exit interactive mode, hit ctrl^C twice.

Running Scripts:

If you want to run multiple statements at once, you can create and run a script using the elixir executable.

First, write your Elixir code in a text file and save with the extension .exs.

Then, at the command line, run the elixir command followed by the file path of your script. In this case, I am running the hi.exs file from the root of my c:\ drive.

Mix:

Single-line statements and script files may be good enough for learning the basics of Elixir, but eventually you’re going to want to create a project. Fortunately, Elixir also comes with the mix executable, which will handle building, compiling, testing and dependency management for Elixir projects. It’s similar to Webpack for Javascript or Maven and Gradle for Java.

To create a Mix project just run the mix command with the following syntax:

Make sure the project name is all lowercase. You should see output like this:

This will create a folder containing:

A lib folder which contains a yourprojectname.ex file where you can put your code

A test folder which contains the unit test scripts for your project

Running Your Project:

To run the code you write in your .ex file, navigate to your project folder in the command line and run the command iex -S mix

This will compile your code and load it into interactive mode.

From here you can call any of the functions defined in your .ex file by running Projectname.functionname

My myproject.ex file looks like this:

I have defined two functions: hello and welcome. Here I call the functions in iex. Note the capitalization of Myproject.

If you change your code, you’ll need to recompile before you can run it. You can do this either by exiting and restarting your iex session, or by running the recompile command.

VSCode:

Now you have a project and can run functions, but it would be nice to use an IDE to edit your code rather than just using notepad. My favorite IDE for Elixir so far is Visual Studio Code. If you don’t already have it installed, you can download and install it from here:

Once VSCode is installed, you’ll probably want to install an Elixir extension. A language extension will usually provide you with some or all of:

Documentation

Code completion

Symbol lookup

Linting

Compiler warnings

Code highlighting/coloring

Debug support

The extension I use is ElixirLS Fork. To install, open up VSCode, click on the extension tab on the left navigation bar, and search for ElixirLS Fork using the search bar. Select the entry and press the install button. It will quickly install and you’re good to go!

You can now open and edit individual Elixir files with VSCode or simply use the Open Folder option to open your whole project.

Dependencies:

Like any good project builder, Mix lets us automatically download external libraries as dependencies, and uses the Hex Package Manager to do so.

To add an external dependency to your project, you will need to alter the mix.exs configuration file in your project folder. If you open mix.exs for editing, you will find a function called deps.

Here I’ve added the highlighted line to the deps function to add the Plug HTTP library to the project dependencies. The ~> indicates that we want to grab Plug from the Hex repository, and the number specifies the version we want. Plug could instead be sourced from a git repository using:

I hope this brief guide helps you get going with a simple Elixir application. You can find all this information in greater detail and much more at these sites:

Happy coding!