Setting Up WSL for Development

Dec 31, 2024 at 23:41:21

This post documents some of my commonly used WSL configurations.

Install Common Packages

Ubuntu:

sudo apt install build-essential

Debian:

sudo apt install gcc g++

Debian Common:

sudo apt install gdb valgrind git gpg curl wget zip unzip zsh

Configure Zsh

Set /bin/zsh as the default shell:

chsh -s /bin/zsh

Install oh-my-zsh and commonly used plugins:

wget https://gist.githubusercontent.com/dingwen07/1261eac531f7f080d72b78f931a8ca01/raw/omz-setup.sh
chmod +x ./omz-setup.sh
./omz-setup.sh

Edit ~/.zshrc to enable plugins:

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

Java Environment

Java environments are generally managed using SDKMAN!. Make sure the unzip package is installed:

curl -s "https://get.sdkman.io" | bash

Configure Windows Terminal for WSL

Enable Mouse Scrolling for CLI Programs

By default, CLI programs in WSL, such as less and nano, do not support mouse scrolling. After configuration, mouse operations will default to being received by the application. To select content, hold the Shift key while selecting.

less (Including man)

Save the following content as a text file:

#env
LESS = --mouse

Then run lesskey <filename> (for newer distributions, save the text directly to ~/.lesskey).

nano

Add the following to ~/.nanorc:

set mouse

vi[m]

Add the following to ~/.vi[m]rc:

set mouse=a

SSH Agent

Use npiperelay.exe to forward the Windows socket //./pipe/openssh-ssh-agent to WSL 12.

Make sure npiperelay.exe is in the Windows PATH. This allows WSL to directly access it. It can be installed via WinGet. Then install socat in WSL:

sudo apt install socat

Next, put the following script into ~/.win-ssh-agent/agent-bridge.sh:

# Code extracted from https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/ with minor modifications

# Configure ssh forwarding
export SSH_AUTH_SOCK=$HOME/.win-ssh-agent/agent.sock
# need `ps -ww` to get non-truncated command for matching
# use square brackets to generate a regex match for the process we want but that doesn't match the grep command running it!
ALREADY_RUNNING=$(ps -auxww | grep -q "[n]piperelay.exe -ei -s //./pipe/openssh-ssh-agent"; echo $?)
if [[ $ALREADY_RUNNING != "0" ]]; then
    if [[ -S $SSH_AUTH_SOCK ]]; then
        # not expecting the socket to exist as the forwarding command isn't running (http://www.tldp.org/LDP/abs/html/fto.html)
        echo "removing previous socket..."
        rm $SSH_AUTH_SOCK
    fi
    echo "Starting SSH-Agent relay..."
    # setsid to force new session to keep running
    # set socat to listen on $SSH_AUTH_SOCK and forward to npiperelay which then forwards to openssh-ssh-agent on windows
    (setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
fi

Add the following to .bashrc or .zshrc:

source $HOME/.win-ssh-agent/agent-bridge.sh

GnuPG

Refer to Using GPG Command-Line Tools in WSL2 and Signing Git Commits3.

1Password CLI

Use the op command-line tool for convenient and secure credential access. First, install it on Windows:

winget install AgileBits.1Password.CLI

Then add the following to .bashrc or .zshrc:

# Aliases
alias op="op.exe"
# alias sudo="sudo -S <<< $(op read "op://Personal/Account Password/password")"

# Completions
eval "$(op completion zsh)"; compdef _op op.exe

References

  1. Use 1Password SSH Agent in WSL - DEV Community

  2. Forwarding SSH Agent requests from WSL to Windows - stuartleeks.com

  3. Using GPG Command-Line Tools in WSL2 and Signing Git Commits