Skip to content

Instantly share code, notes, and snippets.

@symmetryninja
Last active July 27, 2020 12:56
Show Gist options
  • Save symmetryninja/0180ae11b03a40b63727c68560185dfc to your computer and use it in GitHub Desktop.
Save symmetryninja/0180ae11b03a40b63727c68560185dfc to your computer and use it in GitHub Desktop.
A basic howto for of the WSL setup I have now that I have no Apple products.

Setting up WSL v2 to do some linux on windows

This is a constant work in progress.

Why Windows with WSL

Because I became so accustom to having bash behind my VSCode app to run linux-like commands on my MacBookPro and now that I don't have or want another MacBook variant, I want to be able to have a linux-like command system behind visual studio code.

Why not just use linux? Because I use a lot of apps that don't run on linux.

Why not just have a Mac? OSX gets worse and worse as time goes on and the keyboards, lightbar and complete lack of ports drives me nuts.

Words to workflow.

I have 3 development focuses:

  1. Working soley in VSCode/Docker
    1. I checkout using git to the wsl file system
    2. I use VSCode's integration with WSL to edit everything
  2. Working mostly in VSCode but using other local IDE's like Arduino, OpenSCAD or similar
    1. I checkout and edit in Windows (with git bash)
    2. I use windows native stuff
  3. Working only in Windows, e.g. Android, Unity or .net dev
    1. I checkout and edit in Windows (with git bash)
    2. I use windows native apps

WSL and hacks

First off WSL2 is better than 1, install WSL2. You will need to be on Windows 10 2004 or newer.

Install WSL etc

Then install Ubuntu from the Microsoft app store.

A bit of powershell is all you need.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

wsl --set-default-version 2
wsl --set-version ubuntu 2
wsl shutdown

There's some weird things with pathing, by default, all drives including C are mounted in /mnt but for docker (and other things) to play nice we need to change c to be mounted in / - edit the /etc/wsl.conf (sudo)

sudo vim /etc/wsl.conf

This file should look like this.

[automount]
enabled     = true
crossDistro = true
root        = /

ssh into wsl on the network

For one reason or another you may want to ssh into your wsl2 instance from another computer on the network.

First, configure ssh to use a higher port - this is because windows has an ssh service running, i am not sur ehow to utilize it so i just circumvent it. I chose port 2222.

Put this line in the /etc/ssh/sshd_config file (ubuntu)

Port 2222

Then start the ssh server:

sudo service ssh start

Next open the port in windows, note the -Profile 'Private' point means only allowing access from your local network, run this in an elevated powershell.

New-NetFirewallRule -DisplayName 'SSH to WSL' -Profile 'Private' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 2222

Now create a proxy to the port in windows, run this in an elevated powershell.

netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=127.0.0.1

You may need to start ssh every time you reboot, i have left it this way as i don't ssh in much.

Docker

As of WSL2 you can run docker inside the wsl container/vm which is awesome.

Also to log into dockerhub (so you can push images) you will need to install pass

sudo apt install gnupg2 pass

Git and Bash/sh etc

WSL2 deals with file attributes in windows pretty well but it's not 100% and it may never be, Windows and linux filesystems are simply different.

I keep git repos either Windows or WSL, if it's webdev or anyting that I'm doing in VSCode, I usually clone it in WSL and leave it there. If it needs the GPU or if it's a windows or android app, it's better to clone code in Windows and commit there.

Also, don't forget about line endings....

If you want to besure your IDE's do not use crlf and use only lf - code pushes the line endings sometimes - you may need to run this in all your shells on WSL and on Windows (Powershell, gitbash, command line etc.

git config --global core.autocrlf false
git config --global core.eol lf

If you have trouble with the execute bit getting missed... you will have to run this in every repo that's a problem.

git config --global core.fileMode false

Other useful stuff

Of course install AWS cli.

pip install awscli

I use stackup quite a bit:

sudo gem install stackup

Python and pyenv

By default, wsl/ubuntu has python2.7.x as it's default python instance.

To get around this, i want to use pyenv sourced from here

Dependant packages

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

install pyenv

curl https://pyenv.run | bash

get some env based stuff

put this in your ~/.profile

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Install python 3.7 and set it to default

pyenv install 3.7.4
pyenv global 3.7.4

Sources

Other useful links

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