Setting up Ubuntu WSL to develop on Windows
Introduction
I develop in the Windows Subsystem for Linux (WSL 2). Microsoft recommends using WSL 2 if you are using Node.js professionally.
If you are using Node.js professionally, find performance speed and system call compatibility important, want to run Docker containers that leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts, or just prefer using a Bash command line, then you want to install Node.js on the Windows Subsystem for Linux (more specifically, WSL 2).
Using Windows Subsystem for Linux (WSL), enables you to install your preferred Linux distribution (Ubuntu is our default) so that you can have consistency between your development environment (where you write code) and production environment (the server where your code is deployed).
Read more about the Windows Subsystem for Linux if you aren't sure about whether you want to use it or not.
Read through each section before you start the steps in it.
This page is mostly links to other docs accompanied by annotations. Reading the annotations can help you get through the other docs.
Install WSL
- Update to the latest version of Windows 10. Some earlier versions of Windows do not support WSL 2
- Install WSL on Windows
- Remember to update the WSL kernel
- Choose Ubuntu 20.04 LTS
- Your username doesn't have to be the same as your Windows username. I like to choose
me
because it's short and saves space on the terminal - Choose a password that's easy to type. Save it in your password manager in case you forget it
- Install Windows Terminal
- Windows Terminal provides nice customization and works better than the terminal built into the Ubuntu WSL
- Choose Ubuntu 20.04 as your default profile for Windows Terminal
- Open Windows Terminal
- Press Ctrl+, to open the Settings
- Click Startup on the left
- Change your Default profile to Ubuntu-20.04
- Update your starting directory for Ubuntu 20.04
- Open Windows Terminal
- Press Ctrl+, to open the Settings
- Click Ubuntu-20.04 on the left
- Change your Starting directory to
\\wsl$\Ubuntu-20.04\home
Install Node.js on WSL
Follow Microsoft's docs on how to install Node.js on Windows Subsystem for Linux (WSL2)
- In step 3, use the latest install scripts for nvm
- After step 3, add nvm to your
.bash_profile
. The script to install nvm adds it to your.bashrc
, but you need it in your.bash_profile
as well- Type
sudo nano ~/.bash_profile
into your terminal and press Enter - Paste the code snippet at the bottom
Add to ~/.bash_profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion - Press Ctrl+O to initiate writing the file out
- Press Enter to write the file out
- Press Ctrl+X to close the file
- See the difference between
.bash_profile
and.bashrc
- Type
- Skip step 6. Follow step 7 by running
nvm install --lts
Install VS Code Extensions
- Remote Development pack
- Includes Remote-WSL, which is essential for using your WSL
- Nodejs Extensions pack for Node.js development
- Includes ES Lint for linting JavaScript
- Prettier for code formatting
- Code spell checker for spellchecking
- Rewrap for a keyboard shortcut to hard wrap files (Alt+Q)
Set up Git
Git comes pre-installed on Ubuntu 20.04. If you use a different Linux distribution, you may need to set up Git
- Tell Git your name
git config --global user.name "Your Name"
- Go to github.com/settings/emails, scroll to Keep my email addresses private to find your GitHub email, then tell Git your GitHub email
git config --global user.email "[email protected]"
- Set up commit signature verification
- Generate a new GPG key for commit signing
- Add the key to your GitHub account
- Tell Git about your signing key
Remember to do step 5 by adding your GPG signing key to your bash profile
git config --global user.signingkey 4AEE18F83AFDEB23
- Type
sudo nano ~/.bash_profile
into your terminal and press Enter - Paste
export GPG_TTY=$(tty)
- Press Ctrl+O to initiate writing the file out
- Press Enter to write the file out
- Press Ctrl+X to close the file
- Type
- Tell Git to always sign commits
git config --global commit.gpgsign true
- If you need a reminder on how to use Git, read the GitHub Guides
Set up your SSH key
Follow GitHub's docs on how to set up your SSH key
- Generate a new SSH key
- Choose a password that's easy to type. Save it in your password manager in case you forget it
- Add a new SSH key to your GitHub account
- Test your SSH key
Check the configs
- Open Windows Terminal
- Type
cd ~
and press Enter to return to the home directory - Type
code .
and press Enter to open Visual Studio Code
.bash_profile
Your .bash_profile
should have at least these lines
export GPG_TTY=$(tty)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
.gitconfig
Your .gitconfig
should have at least these lines
[user]
email = [email protected]
signingkey = 4AEE18F83AFDEB23
name = Theodore Chu
[commit]
gpgsign = true
Optional
- If you need to use Docker, Set up Docker for WSL 2. You need to run WSL as Administrator if you want to use Docker 8.