How to install Elixir on Ubuntu 22 using asdf

I decided to learn new technologies – Elixir and Phoenix, but I was close to giving it up because I couldn’t get them to work on my machine. I’m using Ubuntu 22, and every time I tried to install Elixir, something was wrong, and I could not run anything related to it. Finally, I figured it out, so here are the steps.

Interestingly, when you use good, old sudo apt-get install erlang elixir it won’t work, most probably. That’s because most often it’s just a partial installation, and there is still lots of dependencies missing.

When I did it for the first time, unaware of those issues, I encountered this error when trying to run Phoenix

== Compilation error in file lib/mix/tasks/phx.gen.cert.ex ==
** (RuntimeError) error parsing file /usr/lib/erlang/lib/public_key-1.11.3/include/OTP-PUB-KEY.hrl, got: {:error, :enoent}
    (elixir 1.12.2) lib/record/extractor.ex:84: Record.Extractor.read_file/2
    (elixir 1.12.2) lib/record/extractor.ex:50: Record.Extractor.extract_record/2
    lib/mix/tasks/phx.gen.cert.ex:146: (module)
    (stdlib 3.17) erl_eval.erl:685: :erl_eval.do_apply/6
could not compile dependency :phoenix, "mix compile" failed. You can recompile this dependency with "mix deps.compile phoenix", update it with "mix deps.update phoenix" or clean it with "mix deps.clean phoenix"

Looking for the solution over the web, I encountered this topic on Elixir forums: https://elixirforum.com/t/ubuntu-22-04-could-not-compile-dependency-phoenix/53177

They suggest you to use something called asdf and say everyone using Elixir uses asdf. Let’s try it then!

asdf

Think of asdf like nvm or pyenv, but more generic. So while nvm is a version manager for NodeJS, that allows you to install and use multiple NodeJS versions on your system, asdf is a version manager for many different tools. It’s got plugins for NodeJS, Python, Ruby, even Postgres if you wanted to have multiple versions!

Let’s install it. Note: I’m using version 0.11.1, which is the newest at the time of writing this article. For fresh instructions, check https://asdf-vm.com/guide/getting-started.html#_3-install-asdf

$ sudo apt-get install curl git # dependencies
$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.11.1 # actual asdf install into ~/.asdf dir

Now add those to your ~/.bashrc in order to be able to run asdf by typing it in your terminal, and to have autocompletions

. "$HOME/.asdf/asdf.sh" # this one to be able to type asdf
. "$HOME/.asdf/completions/asdf.bash" # this one for autocompletion

Now let’s verify if it works:

$ asdf --version
v0.11.1-27c8a10

Erlang

Now we need to install Erlang, which is a dependency for Elixir. If you follow Erlang plugin repository: https://github.com/asdf-vm/asdf-erlang you can see that we need to install a few dependencies in order to make sure it works.

$ asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git # adding erlang asdf plugin
$ sudo apt-get -y install build-essential autoconf m4 libncurses5-dev libwxgtk3.0-gtk3-dev libwxgtk-webview3.0-gtk3-dev libgl1-mesa-dev libglu1-mesa-dev libpng-dev libssh-dev unixodbc-dev xsltproc fop libxml2-utils libncurses-dev openjdk-11-jdk # erlang dependencies

Next, let’s list available Erlang versions:

$ asdf list-all erlang
...
25.0
25.0.1
25.0.2
25.0.3
25.0.4
25.1
25.1.1
25.1.2
25.1.2.1
25.2
25.2.1
25.2.2
25.2.3
26.0-rc1

Let’s pick the last stable version and install it:

$ asdf install erlang 25.2.3
Downloading OTP-25.2.3.tar.gz to 
...
Extracting source code
Building Erlang/OTP 25.2.3 (asdf_25.2.3), please wait...
...

Erlang 25.2.3 has been installed. Activate globally with:

    asdf global erlang 25.2.3

Activate locally in the current folder with:

    asdf local erlang 25.2.3

Elixir

To install Elixir with asdf, we will follow similar steps to Erlang installation. First, let’s install asdf Erlang plugin and list available versions:

$ asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
$ asdf list-all elixir
...
1.14.1
1.14.1-otp-23
1.14.1-otp-24
1.14.1-otp-25
1.14.2
1.14.2-otp-23
1.14.2-otp-24
1.14.2-otp-25
1.14.3
1.14.3-otp-23
1.14.3-otp-24
1.14.3-otp-25
main
main-otp-22
main-otp-23
main-otp-24
main-otp-25
master
master-otp-21
master-otp-22
master-otp-23
master-otp-24

IMPORTANT: Elixir versions are compiled for specific Erlang/OTP versions, so you need to pick the package which corresponds to your Erlang version. That is, if we installed Erlang v25, we will need Elixir with otp-25 in name. Let’s install the latest version:

$ asdf install elixir 1.14.3-otp-25

Activating asdf installations

You need to activate your installations from above, using these commands (to set them globally):

$ asdf global erlang 25.2.3
$ asdf global elixir 1.14.3-otp-25

Now you can verify your installation:

$ elixir -v
Erlang/OTP 25 [erts-13.1.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Elixir 1.14.3 (compiled with Erlang/OTP 25)

If you’ve got the output like that, you did it!

Phoenix

Let’s quickly install Phoenix to make sure we’ve got everything working as expected, following this guide: https://hexdocs.pm/phoenix/installation.html

$ mix local.hex # package manager for Elixir
$ sudo apt-get install inotify-tools # for Ubuntu users to use hot reload
$ sudo apt-get install postgresql-client # to install DB
$ mix phx.new hello # to create a Phoenix app named hello
* creating hello/config/config.exs
* creating hello/config/dev.exs
* creating hello/config/prod.exs
...

Fetch and install dependencies? [Yn]

$ cd hello
$ mix ecto.create
$ mix phx.server

And if you see something like that on your http://localhost:4000/ you did great!

Share this post

Leave a Reply

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