DEV Community

Aleksander Brymora
Aleksander Brymora

Posted on

Set up WSL2, PostgresQL and Phoenix LiveView on Windows

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 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:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. Restart your computer

  2. 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:

wsl --set-default-version 2
  1. It might prompt you to install additional software, which can be acquired here

  2. Restart your computer

  3. 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.

  1. 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

  2. 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

  3. 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)

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) and a super helpful step by step Medium article by Harshit Yadav on PostgreSQL In Windows Subsystem for Linux (WSL)

  1. Open up your terminal, make sure you’re in Linux distro, and run this command to install postgres
sudo apt-get update && sudo apt install postgresql postgresql-contrib
  1. 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

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

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

# you can check the status of the db by running:
sudo service postgresql status
# or you can stop it with
sudo service postgresql stop
  1. Now to an actual setup for normal usage, we need to create a user with this command
sudo -u postgres createuser <your_username>
  1. And we need to create the database
sudo -u posgres createdb <your_db_name>
  1. 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
# 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>
  1. 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:

  1. We need download the erlang installer (refer to the docs linked above if you want the newest version)
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
  1. Refresh Ubuntu’s ‘update queue’ and install erlang and elixir
sudo apt-get update 
sudo apt-get install esl-erlang elixir
  1. 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.

  2. 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.

Oldest comments (1)

Collapse
 
weogrim profile image
weogrim

Thanks for article, it helped me setup postgres ;)

You have two typos in postgres name (posgres) here:

sudo -u posgres createdb <your_db_name>

and here:

sudo -u posgres psql