How to Install Elixir and Phoenix Framework on Ubuntu 16.04

Published on: Fri, Dec 21, 2018 at 10:42 am EST
This article is a port of "How to Install the Phoenix Framework on CentOS 7" for Ubuntu 16.04.

This tutorial will show you how to install Elixir and Phoenix frameworks on a Vultr Ubuntu 16.04 server instance for development purposes.

Prerequisites

  • A new Vultr Ubuntu 16.04 server instance
  • Logged in as a non-root sudo user.

Update the system:

sudo apt-get update

Install Erlang

Install Erlang with the following commands:

cd ~
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb 
sudo dpkg -i erlang-solutions_1.0_all.deb
sudo apt-get update
sudo apt-get install esl-erlang

You can verify the installation:

erl

This will take you to the Erlang shell with following output:

Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]

Eshell V10.1  (abort with ^G)
1>    

Press CTRL + C twice to exit the Erlang shell.

Install Elixir

Install Elixir with apt-get:

sudo apt-get install elixir

Now you can verify the Elixir installation:

elixir -v

This will show the following output:

Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]

Elixir 1.7.3 (compiled with Erlang/OTP 20)

Now you have Elixir 1.7.3 installed on your system.

Install Phoenix

If we have just installed Elixir for the first time, we will need to install the Hex package manager as well. Hex is necessary to get a Phoenix app running, and to install any extra dependencies we might need along the way.

Type this command to install Hex:

mix local.hex

Now we can proceed to install Phoenix:

mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez

Install Node.js

Phoenix uses brunch.io to compile static assets, (javascript, css and more), so you will need to install Node.js.

The recommended way to install Node.js is via nvm (node version manager).

To install nvm we run this command:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

To find out the versions of Node.js that are available for installation, you can type the following:

nvm ls-remote

This will output:

Output
...
     v8.8.1
     v8.9.0   (LTS: Carbon)
     v8.9.1   (LTS: Carbon)
     v8.9.2   (LTS: Carbon)
     v8.9.3   (LTS: Carbon)
     v8.9.4   (LTS: Carbon)
    v8.10.0   (LTS: Carbon)
    v8.11.0   (LTS: Carbon)
    v8.11.1   (LTS: Carbon)
    v8.11.2   (LTS: Carbon)
    v8.11.3   (LTS: Carbon)
    v8.11.4   (LTS: Carbon)
->  v8.12.0   (Latest LTS: Carbon)      
...

Install the version you would like with the following command:

nvm install 8.12.0

Note: If you would like to use a different version, replace 8.12.0 with the version you would like.

Tell nvm to use the version we just downloaded:

nvm use 8.12.0

Verify node has successfully installed:

node -v

Install PostgreSQL

You can install PostgreSQL easily using the apt packaging system.

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Open the PostgreSQL shell:

sudo -u postgres psql

Change the postgres password to a secure password:

\password postgres    

After successfully changing the password, you can exit the PostgreSQL shell:

\q

Restart the PostgreSQL service:

sudo systemctl restart postgresql.service

Install inotify-tools

This is a Linux-only filesystem watcher that Phoenix uses for live code reloading:

sudo apt-get install inotify-tools

Create a Phoenix application

Create a new application:

mix phoenix.new ~/phoenix_project_test

If the command returns the following error:

** (Mix) The task "phx.new" could not be found

You can fix it with the following command:

mix archive.install https://raw.githubusercontent.com/phoenixframework/archives/master/phx_new.ez

Now rerun the command to create a test Phoenix app:

mix phoenix.new ~/phoenix_project_test

Change the PostgreSQL password in the config file with the password you set in the previous step:

nano config/dev.exs    

The application will now be successfully created. Move to the application folder and start it:

cd ~/phoenix_project_test
mix ecto.create
mix phx.server

Now the Phoenix application is up and running at port 4000.