Elixir is a meta-programming language based on Erlang and is available on all major operating systems. In this guide we will cover how to get Elixir installed, use the interactive console, and expand on the libraries available.
By the end of this guide you will be able to have a working development environment for creating Elixir apps. If you are interested in a framework for creating web apps, please look at using Phoenix.
Getting Elixir
The easiest and preferred way to install Elixir is via a distribution, or by using an installer.
Erlang 18.0 or later is used by Elixir and will be installed by default by the distribution installer.
Precompiled Packages
If you wish to install from source or a precompiled package, Erlang will need to be installed separately; for that, please check this guide.
MacOS
- Homebrew
- Update your homebrew to latest:
brew update
- Run:
brew install elixir
- Update your homebrew to latest:
- Macports
- Run:
sudo port install elixir
Unix (and Unix-like)
- Arch Linux (Community repo)
- Run:
pacman -S elixir
- Run:
- openSUSE (and SLES 11 SP3+)
- Add Erlang devel repo:
zypper ar -f http://download.opensuse.org/repositories/devel:/languages:/erlang/openSUSE_Factory/ erlang
- Run:
zypper in elixir
- Add Erlang devel repo:
- Gentoo
- Run:
emerge --ask dev-lang/elixir
- Run:
- GNU Guix
- Run:
guix package -i elixir
- Run:
- Fedora 21 (and older)
- Run:
yum install elixir
- Run:
- Fedora 22 (and newer)
- Run
dnf install elixir
- Run
- FreeBSD
- From ports:
cd /usr/ports/lang/elixir && make install clean
- From pkg:
pkg install elixir
- From ports:
- Ubuntu 12.04/14.04/16.04 or Debian 7
- Add Erlang Solutions repo:
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
- Run:
sudo apt-get update
- Install the Erlang/OTP platform and all of its applications:
sudo apt-get install esl-erlang
- Install Elixir:
sudo apt-get install elixir
- Add Erlang Solutions repo:
Windows
- Web installer
- Download the installer
- Click next, next, …, finish
- Chocolatey
cinst elixir
Docker
For ease of portability, you can also use the official elixir docker image. This is easy to work with just by running one of the following commands:
- Enter interactive mode
- Run:
docker run -it --rm elixir
- Run:
- Enter bash within container with installed
elixir
- Run:
docker run -it --rm elixir bash
Installing From Git
Ensure you have Erlang installed first by visiting the Erlang download page and getting a distribution for your Operating System.
Once Erlang is installed on your machine, please use the following commands:
$ git clone https://github.com/elixir-lang/elixir.git $ cd elixir $ make clean test
Testing Your Installation
Once you have installed Elixir, please run the following at your terminal prompt:
$ elixir --version
If you are getting an error, please ensure the binary is in your PATH environmental variable.
export PATH="$PATH:/path/to/elixir/bin"
Windows users can read this guide.
Interactive Development
Akin to other languages such as Node, Ruby, and Java, Elixir has an interactive mode, which we can access via the command-line prompt as so:
$ iex Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help) iex> c "my_file.ex" # Compiles a file iex> t Enum # Prints types defined in the module Enum iex> h IEx.pry # Prints the documentation for IEx pry functionality iex> i "Hello, World" # Prints information about the given data type
Windows users will need to run iex.bat
to access the interactive console.
When we enter this mode, we can type any Elixir
code and get the return instantly, so it’s good for starting to learn the language.
Let’s do some basic expressions:
ie> 2 + 2 4 ie> round(3.58) 4 iex> "hello" <> " world" "hello world"
When we are running a script, we do that from the shell terminal as so:
$ elixir scriptName.exs
Setting Up Your IDE
If you are using JetBrains’ IntelliJ, Sublime or Atom then you will be happy to know there are several integration plugins available which will make code completion, syntax highlighting, linting your code and displaying errors and warnings easy.
- Atom users please check this package.
- Sublime users check this one.
- IntelliJ /RubyMine users can use this plugin.
Vim Users
If you are using Vim or Emacs, then you can configure support for Elixir such as automatic filetype detection, syntax highlighting and automatic indentation.
Installation can be done with a Vim plugin manager such as pathogen.vim inside ~/.vim/bundle
:
git clone https://github.com/elixir-lang/vim-elixir.git ~/.vim/bundle/vim-elixir
Adding Packages With Hex
Take full advantage of the thousands of packages available for the Elixir ecosystem via Hex.pm.
Search for packages and install them via the mix dependency manager—usage on how to setup mix is in the documentation. Once you have a mix.exs
set up, adding this line would install the popular JSON library poison:
{:poison, "~> 3.1"}
There are many packages ready for your use on Hex which can provide a plethora of solutions such as JSON and XML support, SSL cryptography functions, database abstraction and caching, to name a few.
If you are looking for powerful packages, as mentioned before, check out Poison, a very fast pure Elixir JSON library!
Also worth checking is hackney, a simple HTTP client, and plug, a specification for composable web modules which can easily port between your applications and save you a lot of time.
Conclusion
Elixir is easily available via all of the distribution channels accessible and some rather exotic ones also, such as Raspberry Pi. Installing from source is easy, as is using a package manager such as the popular brew
on macOS.
Going forward, you may wish to review the Erlang libraries available for Elixir, and also, as mentioned earlier, get further into taking advantage of using Hex packages.
If you are looking for some excellent packages that expand Elixir then you will enjoy this curated list on GitHub.