mikemaccana / how-to-install-a-linux-development-environment-on-windows

How to install a Linux development environment on Windows using Ubuntu and WSL2
125 stars 7 forks source link

How to install a Linux development environment on Windows

Using Ubuntu and WSL2

Why this guide

This a guide for people familiar with Linux and Unix environment, to create a development environment on Windows using WSL2 and Ubuntu.

I don't work for Microsoft or Canonical - I develop web apps and before that was a Linux admin. For most of this time, my main machines have been either Linux or macOS. This means:

In short, my developer profile is very Linux and Unix oriented. Much like you, I don't want to throw away that knowledge and muscle memory to use something different.

Why WSL2?

Finally: Windows is nice. The inbuilt tools are modern and uncluttered, my desktop unlocks when I sit down due to Windows Hello, I can transfer files back and forth between my desktop and my phone easily.

Why not WSL2?

In addition, the Windows version of Visual Studio Code has an extension for WSL2 to do file operations on the Linux side, making them much faster.

Why this guide

The aims for this guide as follows:

Right now it's a great time to try WSL

There's a new Ubuntu release (20.04) and a new Windows release (20.04), both with a bunch of new features.

What goes where

Enable WSL2

Click Start Type "Powershell". Click "Run as Administrator"

Run these two commands to enable WSL:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot (and enable Virtualisation in UEFI/BIOS if you need to)

Surface devices have virtualisation turned on already,so you can just reboot.

Otherwise, you may have to go into your UEFI/BIOS and enable Virtualisation. You can reboot to your UEFI/BIOS from inside Windows. Open the Settings app, search for "Recovery Options", and "Restart Now".

When your PC reboots, it will go into Advanced startup. Go to “Troubleshoot -> Advanced options" and click on the "UEFI Firmware Settings" option.

Set WSL to WSL2

Currently, the older WSL1 is still the default. The new WSL2 (which uses a real Linux kernel):

wsl --set-default-version 2

Open Ubuntu from the start menu and and let it install.

Have a message about Virtualisation needing to be enabled? See the next step.

This ensures distros you install subsequently will run on WSL 2.

If you've already installed Ubuntu, just convert our existing WSl1 Ubuntu to WSL2 run:

wsl -l -v
wsl --set-version Ubuntu 2
wsl --set-default-version 2

Get apps via Winget

Get App Installer (which includes the winget command) if you don't have it already.

Let's install:

Note: 'Ubuntu 20.04' isn't available by Winget, but will be soon.

foreach ($app in 'DockerDesktop', 'Git', 'Power Toys', 'Powershell', 'Visual Studio Code', 'Windows Terminal') {  winget install -e  $app }

Configure Terminal for Unix settings

Open terminal settings and change:

"copyOnSelect": true,

Then look at the profiles. Find the Ubuntu profile. See the Ubuntu profile has a GUID? Change the default profile GUID to the Ubuntu GUID.

Also, because transparent terminals are cool, add this to the Ubuntu profile:

"useAcrylic": true,
"acrylicOpacity": 0.9

Browse iterm2colorschemes.com and download the equivalent named Windows Terminal file from their Windows Terminal color schemes. Open Settings and paste it into the schemes section, then select that scheme name for the profile you want to use it with in profiles.

Make git use Windows Credential Manager

edit ~/bin/git-credential-manager

And make the file's contents be:

#!/bin/bash
exec '/mnt/c/Program Files/Git/mingw64/libexec/git-core/git-credential-manager.exe' $@

Set the modes:

chmod +x ~/bin/git-credential-manager

Then edit ~/.gitconfig and add

[credential]
    helper = manager

Set up Windows for development in WSL2

Ensure Windows git always uses LF line endings (Unix-style)

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

Ensure VScode always uses LF line endings (Unix-style)

In vscode, open 'Preferences', and search for 'eol'. Set The default end of line character. to /n

Add Ubuntu to Windows Explorer

Pin \\wsl$\Ubuntu to Quick Access

Map Ubuntu to Windows Drive

This allows you to access Linux's file system from older Windows applications which cannot access network paths.



Use a tiling window manager

If you're the tiling window manager, check out FancyZones. It's part of PowerToys.

Set up Ubuntu for development in WSL2

Open Ubuntu from Windows Terminal and set up your development environment:

Make VScode the default editor, and open files using file:linenumber syntax

A lot of Linux commands use $EDITOR to be the preferred editor. Let's set that to VScode.

code -g allows vscode to open a filename:linenumber and immediately to go that line number in a file. VSCode doesn't do this by default, because some files can have a colon in them. I don't put colons in file names, and I like to be able to launch my editor with the alias edit, so I add this to my .bashrc

export EDITOR=code
alias edit='code -g'

Install Linux developer Tools

sudo apt install build-essential

Quiet Ubuntu's login

touch /home/mike/.hushlogin

Use sudo without a password

Typing your own password can get boring.

sudo visudo

And edit the line below to as follows:

# Allow members of group sudo to execute any command
# %sudo ALL=(ALL:ALL) ALL
%sudo ALL=(ALL) NOPASSWD:ALL

After you save the file, sudo will no longer ask for passwords.

Make tab completion work with any case

Open .inputrc (create it if needed) and add:

set completion-ignore-case On
$include /etc/inputrc

Add other development tools

Your command line developer tools are generally Linux ones, and will live in the WSL2 filesystem.

I code primarily on node.js, so I'd install

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

Add an open command to open files with Windows apps

WSL2 comes with a Debian package called wslu which contains a command to open files using Windows, called wslview. Personally, I prefer the name open so:

alias open='wslview'

Add ~/bin to your PATH

I like to add personal scripts and files to ~/bin

export PATH=$PATH:~/bin

Add any databases or other Linux services

WSL2's Ubuntu uses a custom /init, which uses /etc/init.d style scripts.

You can start, stop, check the status of these with:

sudo /etc/init.d/postgresql start

or

sudo service postgresql start

Replacing start with stop or status etc.

Configure git projects to ignore file mode changes

Checking out a repo on your Linux box, Windows git tools may report some files seem to have changed, with no actual modification of their contents:

What has changed is their Unix file modes (the mod in chmod) - let's disable this. Inside a project:

git config core.fileMode false

Conclusion

About the author

I've been using Linux for a little over 20 years, including at places like Red Hat and IBM's dedicated Linux group. I'm the example sudo user in /etc/sudoers in a couple of distros.