Skip to content

Instantly share code, notes, and snippets.

@rudolfb
Last active January 30, 2020 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudolfb/d86422355243f102664b5aaf750d75b9 to your computer and use it in GitHub Desktop.
Save rudolfb/d86422355243f102664b5aaf750d75b9 to your computer and use it in GitHub Desktop.

Installing Elixir and Phoenix under Windows 10 WSL Ubuntu 18.04 LTS

We need to install a few prerequisites for running Elixir under Ubuntu, notably nodejs and the Postgres client.

Ubuntu
sudo apt -y update && sudo apt -y upgrade

sudo apt -y install \
  git nano inotify-tools curl wget \
  traceroute net-tools whois libxml2-utils\
  default-jdk

sudo apt -y update
sudo apt -y install curl dirmngr apt-transport-https lsb-release ca-certificates
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

sudo apt -y install nodejs

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt -y install postgresql-client-12

WSL2 makes changes to the networking between the Ubuntu WSL and Windows. You install Postgres under Windows, and the WSL session connects from the WSL using the postgresql-client-12 to the Postgres server running under Windows. With WSL1 the networking is transparent. You see the IP 127.0.0.1 and the port 5432 of the Postgres server from Ubuntu, but in WSL2 Ubuntu gets it's own IP address, and accessing the Postgres server requires you to explicitly allow "foreign" access to your Postgres.

There is however a disadvantage to this setup: WSL2 does not have a fixed IP address for either the WSL session nor for the Windows session. You might want to install postgres directly inside the WSL2 session to avoid this issue.

Ubuntu
sudo apt -y install postgresql-12 postgresql-client-12
sudo service postgresql start
sudo su - postgres
psql -c "alter user postgres with password 'postgres'"

In the case that Postgres installed in the WSL2, you can reference the Postgres as localhost.

Ubuntu
rb@RB-WORK:~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.19.32.1
rb@RB-WORK:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.19.42.47  netmask 255.255.240.0  broadcast 172.19.47.255
        inet6 fe80::215:5dff:fea5:5758  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:a5:57:58  txqueuelen 1000  (Ethernet)
        RX packets 36390  bytes 3519964 (3.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24639  bytes 5245908 (5.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Short alternative:

Ubuntu
winip=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
wslip=$(ip addr | grep eth0 | grep inet | awk '{print $2}')
echo Windows ip addr: $winip
echo WSL ip addr: $wslip

The following are the important parts of the response:

nameserver 172.19.32.1 - this is the IP address of Windows. This is the IP you will use in your Ecto Repo settings:

Elixir config.exs
config :music_db, MusicDB.Repo,
  username: "postgres",
  password: "postgres",
  database: "music_db",
  hostname: "172.19.32.1"

inet 172.19.42.47 - this is the IP address that your Postgres server under Windows will see when the client trys to access the server. If your WSL session is of version 2, you will need to edit the Windows Postgres pd_hba.conf file. In order to find out what WSL version you are running, in a Windows CMD:

Windows CMD
C:\Users\rb>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2

If the VERSION is 2, then modify the file C:\Program Files\PostgreSQL\12\data\pg_hba.conf:

pg_hba.conf in Windows
...
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             172.19.42.47/32         md5
# IPv6 local connections:
...

Note the line that was added, using the inet 172.19.42.47 IP address above:

host    all             all             172.19.42.47/32         md5

To test the change, in Ubuntu:

Ubuntu
rb@RB-WORK:~$ psql --host=172.19.32.1 --port=5432 --username=postgres
Password for user postgres:
psql (12.1 (Ubuntu 12.1-1.pgdg18.04+1))
Type "help" for help.

postgres-# \q
rb@RB-WORK:~$
  • asdf - for Erlang and Elixir

Terminal as normal user

Ubuntu
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.7.6
# echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
# echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
# The following ensures the line is appended only if it does not exist
cd ~
FILE='.bashrc'
LINE='. $HOME/.asdf/asdf.sh'
grep -xqF -- "$LINE" "$FILE" || echo -e "\n$LINE" | tee --append "$FILE"
LINE='. $HOME/.asdf/completions/asdf.bash'
grep -xqF -- "$LINE" "$FILE" || echo -e "\n$LINE" | tee --append "$FILE"
source ~/.bashrc

Note, the terminal needs to re-load the .bashrc after writing the asdf paths, thus the source command above, otherwise any following asdf commands will fail. And because we are using our own session when running asdf commands, we have to ensure that we are in our user session and not in the root session when installing asdf.

The grep commands above search for the line that needs to be added to the .bashrc, and only if the text is not found the text is appended to the file.

https://stackoverflow.com/questions/3557037/appending-a-line-to-a-file-only-if-it-does-not-already-exist#comment49743511_3557165

  • Erlang

Prerequisites for Erlang asdf install

Ubuntu
sudo apt -y install build-essential autoconf \
  m4 libncurses5-dev libwxgtk3.0-dev libgl1-mesa-dev \
  libglu1-mesa-dev libpng-dev libssh-dev \
  unixodbc-dev xsltproc fop

Install Erlang

Ubuntu
asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git
asdf list-all erlang
asdf install erlang 22.2.1
asdf global erlang 22.2.1
  • Elixir
Ubuntu
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
asdf list-all elixir
asdf install elixir 1.9.4-otp-22
asdf global elixir 1.9.4-otp-22
  • Phoenix

https://hexdocs.pm/phoenix/installation.html

Ubuntu
mix do local.hex --force , \
  local.rebar --force , \
  archive.install hex phx_new 1.4.11 --force
  • VSCode - Visual Studio Code

You install Visual Studio Code under Windows, and then connect to the Ubuntu WSL session. You will be prompted to install an extention that gives you access to the WSL Ubuntu session from Windows:

Name: Remote - WSL
Id: ms-vscode-remote.remote-wsl
Description: Open any folder in the Windows Subsystem for Linux (WSL) and take advantage of Visual Studio Code's full feature set.
Version: 0.41.7
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

After installation we need to install a few VSCode extentions:

Name: ElixirLS Fork: Elixir support and debugger
Id: elixir-lsp.elixir-ls
Description: Elixir support with debugger, autocomplete, and more. Powered by ElixirLS.
Version: 0.2.28
Publisher: elixir-lsp
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=elixir-lsp.elixir-ls

Name: vscode-elixir
Id: mjmcloug.vscode-elixir
Description: Elixir support for VSCode
Version: 1.1.0
Publisher: Mat McLoughlin
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=mjmcloug.vscode-elixir

Name: vscode-elixir-formatter
Id: sammkj.vscode-elixir-formatter
Description: Elixir code formatting using Elixir 1.6 Code Formatter
Version: 0.2.0
Publisher: Kok J Sam
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=sammkj.vscode-elixir-formatter

In the Visual Studio Code settings (Edit >> Preferences >> Settings), search for the text format and check the field

Editor: Format On Save

Now, when you save a file in Visual Studio Code, the file will automatically be formatted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment