Skip to content

Instantly share code, notes, and snippets.

@bartdorsey
Last active September 16, 2024 21:13
Show Gist options
  • Save bartdorsey/d015b4f9739dcd66175bec857d6429fb to your computer and use it in GitHub Desktop.
Save bartdorsey/d015b4f9739dcd66175bec857d6429fb to your computer and use it in GitHub Desktop.
WSL Quick Dev Setup

WSL Quick Dev Setup

Make sure virtualization is enabled:

  1. Open Task Manager
  2. Performance Tab
  3. Select CPU
  4. Check for "Virtualization Enabled"

If virtualization is not enabled

You will need to boot into your EFI/BIOS setup screen and enable it. This is different for every manufacturer.

Install Ubuntu WSL

wsl --install

Reboot

This usually requires a reboot. When the PC reboots be looking for an Ubuntu window to automatically open.

Don't close it by accident!

Set up username/password

Set a username and password for your Linux system. Remember this username and password!

Change default terminal profile

  1. Press Control+, to get to the settings
  2. Change the default profile to Ubuntu
  3. Click Save
  4. Close the settings tab

Install apt packages

sudo apt update
sudo apt install -y build-essential git python-is-python3 python3-venv zip unzip tree

Setup git name and email address

Replace the bits in <> with your own information

git config --global user.name "<firstname lastname>"
git config --global user.email "<youremail@yourdomain.com>"

Install and switch to zsh

sudo apt install -y zsh
chsh -s /bin/zsh

Close the terminal and reopen it to use ZSH

zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses ---

At this prompt choose option 2.

Install fnm and nodejs

# installs fnm (Fast Node Manager)
curl -fsSL https://fnm.vercel.app/install | bash

# download and install Node.js
fnm use --install-if-missing 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.14.0`

# verifies the right NPM version is in the environment
npm -v # should print `10.7.0`

Create a new projects directory for WSL.

mkdir ~/projects

From now on you will put any new projects in this new folder.

Instal VSCode WSL extension and reinstall other extension inside WSL

  1. open VSCode
  2. Search for the extension: WSL by microsoft
  3. Install it.
  4. Install any other extensions you were previously using in WSL (Python extension etc)
  5. Close vscode

To test, create a sample project in your projects directory

# Change into yoru projects dir
cd ~/projects
# make a directory
mkdir test-project
# Change into it
cd test-projects
# create an empty python file
touch hello.py
# Open vscode
code .

Put some python code into the hello.py file and verify you can run the code.

You can also test making a virtual environment. From now on you will use the same commands as the macOS user have been using.

python -m venv .venv
source .venv/bin/activate

Test pip installing something, perhaps rich or you could also pip install django or anything else really.

Once you've tested this you can remove this test-project folder.

Customize your prompt (Optional)

There's two options for this, one is using the built in capabilities of zsh to configure a prompt

The other is to install starship, a customizable prompt program and replace the zsh prompt.

Only choose one of the following options:

Setup a zsh prompt to show git info (Option 1)

You can do this or setup the starship prompt below

Edit your ~/.zshrc file and add the following lines:

# Load the vcs_info module, which shows version control system info (like git branch, status, etc.)
autoload -Uz vcs_info

# Hook the vcs_info function into the "precmd" function, which runs right before the prompt is displayed.
# This ensures the version control information is updated before each new prompt.
precmd() { vcs_info }

# Enable vcs_info for git repositories. It tells vcs_info to gather information specifically for git.
zstyle ':vcs_info:*' enable git

# Customize the format of the vcs_info display.
# %F{cyan} sets the text color to cyan. %s shows the VCS (version control system) type.
# %b shows the branch name, and %f resets the text color. %u and %c show the staged and unstaged changes.
zstyle ':vcs_info:*' formats "%F{cyan}(%s) %b%f %u %c "

# Check for uncommitted changes in the working directory before each prompt.
zstyle ':vcs_info:*' check-for-changes true

# Customize the display for staged changes in green.
zstyle ':vcs_info:*' stagedstr '%F{green}(staged)%f'

# Customize the display for unstaged (modified) changes in red.
zstyle ':vcs_info:*' unstagedstr '%F{red}(modified)%f'

# Enable the prompt to be re-evaluated every time it is displayed. This allows dynamic content like vcs_info to be updated.
setopt prompt_subst

# Update the shell prompt (PS1) to include the vcs_info output.
# ${vcs_info_msg_0_} contains the vcs_info message (like git branch and status).
# %F{green}%/%f shows the current working directory in green. 
# %F{yellow}%#%f shows the prompt symbol (# for root, % for normal users) in yellow.
PS1='${vcs_info_msg_0_}
%F{green}%/%f
%F{yellow}%#%f '

After restarting your terminal you should see a nicer more colorful prompt

Install starship prompt (Option 2)

curl -sS https://starship.rs/install.sh | sh

Open your ~/.zshrc file in vscode

code ~/.zshrc

Remove these lines from the top of your .zshrc file

autoload -Uz promptinit
promptinit
prompt adam1:w

And add the following line in it's place

eval "$(starship init zsh)"

Save the file, close vscode and your terminal, and open a new terminal to activate starship.

Accessing your files from Windows.

Check out this link from Microsoft https://learn.microsoft.com/en-us/windows/wsl/filesystems

Accessing old projects

This is easy, you can just clone them down again in your new projects folder.

cd ~/projects
git clone <git clone url here>

Other odds end ends

Setting up SSH key

If you've been using SSH keys for git, you will need to go through the process of generating a new key.

ssh-keygen -t ed25519

You can set a passphrase or leave it empty, it's up to you.

Then grab the public key and put it on your clipboard

cat ~/.ssh/id_ed25519.pub

Then paste it into github or gitlab or whereever you need to use it.

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