Setting Up Development In Wsl

August 09, 2020

By the end of this guide, you’ll have the following installed:

WSL

This guide will use “Ubuntu”. So your mileage may vary.

Enable the feature

You will be prompted to restart your machine after enabling.

Search for “Turn Windows features on or off”.

enable-feature

And check the option that says “Windows Subsystem for Linux”.

enable-wsl

Download Ubuntu from the store

get-ubuntu-store

As of this writing, the “Ubuntu” with no version number is 20.04.

ubuntu-start

Converting to WSL 2

Open powershell and wsl --set-version <name> 2 on the distro you want to upgrade.

ubuntu-wsl-2

To move your projects to the WSL network, just do explorer.exe . on the directory like ~/projects to open it in the File Explorer and copy your files.

To open your projects in VSCode, navigate to the directory and do code ..

Copy and Pasting

Right click on the title bar of the Ubuntu terminal and click on Properties. In the Options tab, check “Use Ctrl+Shift+C/V as Copy/Paste”

wsl-copy-paste

Fonts

Some characters will not work out of the box. I’m currently using “DejaVu Sans Mono for Powerline”.

Download is available here ↗️

Git

Install Git
Terminal window
sudo apt-get update
sudo apt-get install git
# verify
git --version
Set your identity
Terminal window
git config --global user.name "John Doe"
git config --global user.email "john.doe@gmail.com"
A better git log
Terminal window
git config --global alias.lg "log --color --graph --pretty=format:'%C(bold yellow)%h%Creset -%C(bold cyan)%d%Creset %s %C(bold cyan)(%cr) %C(bold red)<%an>%Creset' --abbrev-commit"

better git log
Left: git log; Right: git lg

Use it by running git lg

Colors available are: normal, black, red, green, yellow, blue, magenta, cyan, white

Attributes available: bold, dim, ul, blink, reverse

Configure Git to push on current branch

To avoid pushing to the wrong branch, especially master.

Terminal window
git config --global push.default current

When doing a push, no need to specify to which branch. Just run git push origin and it will push to whatever branch you’re currently on.

Hub

This is optional. Hub is a superset of git like how TypeScript is a superset of JavaScript.

Terminal window
# Install binary and documentation
wget https://github.com/github/hub/releases/download/v2.2.9/hub-linux-amd64-2.2.9.tgz
tar zvxvf hub-linux-amd64-2.2.9.tgz
sudo ./hub-linux-amd64-2.2.9/install
# Skipped autocomplete and alias
# Cleanup
rm -rf hub-linux-amd64-2.2.9

With hub you can do a pull request to github by running hub pull-request -b <branch> -m <pr_message>.

Generate SSH key

Terminal window
ssh-keygen -t rsa -C "your_email@example.com"
# You can just press Enter when it asks to enter a file to save the key
# Copy key to clipboard
cat ~/.ssh/id_rsa.pub
# Add the ssh key on github or bitbucket

ssh-keygen

ZSH Shell

I prefer zsh but you can replace it with others like fish or just the default bash shell.

Install ZSH
Terminal window
sudo apt-get install zsh
# verify
zsh --version
# set as default
chsh -s $(which zsh)
# reopen the terminal

You’ll be greeted with this screen, select the (q) option.

zsh-start

Installing Oh my ZSH
Terminal window
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

zsh-install

Auto suggest
Terminal window
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Using plugins in ZSH

Edit your ~/.zshrc file and find the plugins section

~/.zshrc
# find this line and add the following
plugins=(git ssh-agent zsh-autosuggestions)

Refresh zsh to apply the changes

Terminal window
source ~/.zshrc

Node

This will install Node using nvm to avoid using sudo

Terminal window
# you can replace zsh with bash
# this will add some scripts to the .zshrc or .bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh
# Note: On Linux, after running the install script, if you get nvm: command not found or see no feedback from your terminal after you type:
command -v nvm
# If that doesn't work, you can try this command instead
source ~/.nvm/nvm.sh
# To verify nvm installation
nvm --version
# install latest lts
nvm install --lts # or `nvm install 12` for specific version
# To verify node installation
node --version
The normal way which will require sudo
This is not recommended
Terminal window
# replace "setup_8.x" with the version you want
# like "setup_7.x" if you want to install version 7.x
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
# verfiy
node -v
npm -v
# to update npm (if necessary)
npm i -g npm

Reinstalling node

Terminal window
sudo apt-get remove nodejs
sudo apt-get remove npm
sudo apt-get update
# verify by running
which nodejs npm node
# should return a 'not found'
# then install again

tmux

This is just an optional addition which will improve quality of life in development.
Install tmux
Terminal window
sudo apt-get update
sudo apt-get install tmux
# verify
which tmux
# /usr/bin/tmux
# start tmux
tmux
Configuring tmux

Create a tmux configuration file

Terminal window
# Create .tmux.conf in your home directroy
# This will hold tmux configurations
cd ~
nano .tmux.conf

Add the 1st part on the config for now

~/.tmux.conf
# remap prefix from C-b to C-a
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# set pane using mouse
set -g mouse on
# make tmux use your preffered shell
set-option -g default-shell /bin/zsh
# use 256 term for pretty colors
set -g default-terminal "screen-256color"
# decrease command delay (increases vim responsiveness)
set -sg escape-time 1
# color status bar
set -g status-bg colour235
set -g status-fg white
# new pane on the same path
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
# add tmux prefix key pressed indicator
set -g status-right '#{prefix_highlight} | %a %Y-%m-%d %H:%M'
# customize indicator
set -g @prefix_highlight_bg 'colour201'
set -g @prefix_highlight_fg 'black'
Terminal window
# to apply modifications
tmux source-file .tmux.conf
Installing plugins
This is optional

This will install the following plugins:

  • tpm
  • tmux-sensible
  • tmux-resurrect
  • tmux-prefix-highlight

Add the rest of the config

~/.tmux.conf
# list of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
# keep this at the bottom of .tmux.conf
# initialize tmux plugin manager
run '~/.tmux/plugins/tpm/tpm'

Terminal window
# to install tmux plugins
prefix + I
#ctrl+a, shift+i

There’s no progress indicator but once complete this screen will show

tmux-plugins-install

tmux key combinations
prefix means crtl+a or whichever prefix you have set.
  • prefix % - split horizontally
  • prefix " - split vertically
  • prefix ctrl+s - save layout for tmux-resurrect
  • prefix ctrl+r - reload layout
  • prefix c - create a window
  • prefix w <window_number> - switch to a window
  • prefix x - close a pane
  • prefix z - make pane fullscreen
prefix z

is important because you aren’t able to highlight multiple lines in a vertical pane because the other panes will be highlighted too.

other key combinations

To highlight texts inside WSL while using tmux you’ll have to hold down shift and highlight.

  • ctrl+shift+c - copy highlighted text
  • ctrl+shift+v - paste to terminal while using tmux

Refer to #copy-and-pasting for the combinations above

If you’re using Windows Terminal ↗️, you still need to hold shift when highlighting but you can use the normal ctrl+c and ctrl+v.


Update history:

  • Jan 20, 2021 - updated nvm section, add source ~/.nvm/nvm.sh
  • Jan 20, 2021 - updated tmux section, add tmux command to go to tmux
  • Feb 09, 2021 - add setopt HIST_IGNORE_SPACE
  • Aug 09, 2022 - remove setopt HIST_IGNORE_SPACE, update stuff