Set up WSL2, PostgresQL and Phoenix LiveView on Windows
Tags: wsl, postgres, phoenix, liveview
Published: 2020-09-08 09:07:34

Recently I’ve been sucked into the world of Elixir and spreading the word of functional programming. So to stay true to that I thought I’d share my process of setting up from ground up Elixir, Postgress and LiveView project, as there is some setup. And by share, I mean I’ll point you to the docs/articles that helped me and write a tldr;

# WSL
Here are the [official WSL docs](https://docs.microsoft.com/en-us/windows/wsl/install-win10) with lots of information. 

Here is a rundown on how to set it up plus some bonuses. (do check the docs out as this might get outdated at some point). If you already have it, by all means skip this part. 

There is a lot of restarting so buckle up:

1. If you want WSL2 — update your windows to version ^1903, build 18362. You can check it by running: Windows logo key + R, type winver, select OK

2. Open up PowerShell, as Administrator, in any directory and paste in these commands one by one:

```bash
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```

3. Restart your computer

4. Now if you chose to go with WSL2, you need to open up PowerShell as Administrator again in any directory and specify that you want to use it:

```bash
wsl --set-default-version 2
```

5. It might prompt you to install additional software, which can be acquired here

6. Restart your computer

7. Get yourself a Linux distribution of your choosing. I went with newest Ubuntu LTS, as it doesn’t really matter on WSL (unless you care about some packages available only on AUR or other, but then I guess you know what you’re doing).

Do this by opening Microsoft Store and searching for Ubuntu and getting 20.04 LTS or newer. After it installs open it up and follow the setup process, by typing in your username and a password of your choosing.

8. After that you should be good to go to use WSL in the Ubuntu software, but what I like to do is install Windows Terminal from Microsoft Store that can open up multiple shells and subsystems so you can switch to whatever you like. Aaaand it looks nice… [Here is the link](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?activetab=pivot:overviewtab)

9. After you install it, won’t default to Ubuntu, which is not too optimal, so lets change that. Click on the arrow on the top bar and then click on settings. It will open up the json file with settings. Here you can change, keybindings, fonts and other fancy things, but what we’re interested in is the “defaultProfile” which we need to set to guid of the Ubuntu. You can get it from sthe profiles > list and looking for an object with “Ubuntu” in it and copying its guid and pasting it in the “defaultProfile”. After that save the changes and reopen the Terminal. It should now default to Ubuntu and if you ever want to open something else up
10. We’ve done all the setup, now we just need to update the distro and we’re good to move on to Postgres. Here are the commands to update (pro-tip add them to your .bash-profile as a shortcut so you can quickly update)

```bash
sudo apt-get update && sudo apt-get upgrade -y
```

# PostgreSQL

Now that took me a while to get, but here is the process.
First of all there are two links that made it all work: [Windows docs on installing PostgreSQL (and other DBs)](https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database) and a super helpful step by step Medium article by [Harshit Yadav](https://medium.com/@harshityadav95?source=follow_footer--------------------------follow_footer-) on [PostgreSQL In Windows Subsystem for Linux (WSL)](https://medium.com/@harshityadav95/postgresql-in-windows-subsystem-for-linux-wsl-6dc751ac1ff3)

1. Open up your terminal, make sure you’re in Linux distro, and run this command to install postgres

```bash
sudo apt-get update && sudo apt install postgresql postgresql-contrib
```

2. After that finishes up we need to set up the password for postgres by running this command and typing in the password
sudo passwd postgres

3. Notice that this is just like setting up a password for any other user, as this is how PostgreSQL chose to set up.

4. After we set the password we need to start up our database
sudo service postgresql start

```bash
# you can check the status of the db by running:
sudo service postgresql status
# or you can stop it with
sudo service postgresql stop
```

5. Now to an actual setup for normal usage, we need to create a user with this command

```bash
sudo -u postgres createuser <your_username>
```

6. And we need to create the database

```bash
sudo -u posgres createdb <your_db_name>
```

7. And now we need to set up a password for that user that we’ve just created and grant him privilages. Note that when we get to psql you MUST finish all commands with a semicolon

```bash
# opens up psql
sudo -u posgres psql 
# sets up a password for the user that you created earlier
alter user <username> with encrypted password 'your_new_password';
# we need to grant privilages to the user, so we can use the db that we created on step 6
grant all privileges on database <db_name> to <username>
```

8. Ctrl + d to quit the shell and we’re set! Now you can use postgres in any project, but since I’m into Phoenix lately here is a next part about getting that sorted.


# Installing Elixir and Phoenix LiveView

Please refer to these docs if something doesnt work anymore in my instructions:

- How to install Elixir (part about ubuntu): [https://elixir-lang.org/install.html#gnulinux](https://elixir-lang.org/install.html#gnulinux)
- How to install Phoenix framework (with LiveView included): [https://hexdocs.pm/phoenix_live_view/installation.html](https://hexdocs.pm/phoenix_live_view/installation.html)

1. We need download the erlang installer (refer to the docs linked above if you want the newest version)

```bash
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
```

2. Refresh Ubuntu’s ‘update queue’ and install erlang and elixir

```bash
sudo apt-get update 
sudo apt-get install esl-erlang elixir
```

3. You should have working Elixir now and their ‘mix’ manager, to cofirm that run ‘irc’ and it should open up an elixir shell. Double ctrl+c will get you out of it.

4. To get started with Phoenix LiveView is as simple as running this command in the folder that you want to work on
mix phx.new my_app --live
The ‘ — live’ flag is whats telling mix to set up a LiveView project, not a simple Elixir project.

NOTE that if you want to install javascript related things you do need to have node installed. For that I strongly suggest using nvm, for which instructions can be found here.

# Aaight, that’s it.

Probably lot’s of extra stuff, but maybe will help some of you set up some things. Go and make something gorgeous!

## Edit
Sorry for the weird numbering. Code snippets mess everything up and I'm not sure how would I fix the numbering anyway.