• Stars
    star
    117
  • Rank 293,317 (Top 6 %)
  • Language
  • Created about 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

How to install a Linux development environment on Windows using Ubuntu and WSL2

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:

  • My fingers know bash's defaults. I type / to separate directories, ctrl R to search history, and edit lines with ctrl β—€ and ctrl β–Ά without thinking. I know grep, sed awk and the usual ways of scraping and manipulating text on the console.
  • I select things in a terminal to automatically copy them and I click a mouse button to paste.
  • The idea that anybody would want a file in anything other than 'LF' line endings is alien to me.

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?

  • It's familiar. Working with WSL2 isn't that much different than changing Linux distributions, or moving between Linux and macOS.

  • Windows has excellent hardware support. Whether you get high-end Surface hardware, or build your own rig from components, you'll get full featured Windows drivers from the hardware manufacturer.

  • apt-get has more packages than brew on macOS.

  • Design tools common in web development are usually available for Windows.

  • There are some useful Windows-based developer GUI tools. While I'm comfortable using git rebase -i (so don't @ me), the visual interactive rebase in Fork, a Windows git app, is a great way to clean up my commit history before pushing.

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?

  • Like setting up any new OS, there's work to do to make it your own.

  • Some things will be different if you're used to another OS.

  • While storing your code on Linux makes things nice and fast, accessing files on Linux from most Windows tools is still not as fast as accessing Windows files from Windows tools. In practice (as a node developer working on large TypeScript project) I find the current speed to not be an issue in my day to day work.

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:

  • To be as complete as possible, have this guide be the go-to place for people used to developing on a Unix environment.

  • To include developer focused settings of interest to Linux/Unix developers that make working on WSL2 easier, like .bashrc, .inputrc and git settings.

  • To save time when setting up a new development environment. Eg, setting WSL to version 2 before installing Ubuntu (so Ubuntu is already WSL version 2), and installing Ubuntu before Docker (so Docker and VScode detect Ubuntu and add their commands.)

  • Best practices, like keeping your code on the Linux side to make them faster.

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

  • Your code will be in the Linux filesystem, for speed.
  • Your language support (node, Python, Ruby, Rust. Go, Elixir etc) will in Linux.
  • Databases (I like Postgres) and Docker containers will be in Linux
  • IDEs will be in Windows (vscode installs a special WSL helper into Linux though)
  • Browsers will be in Windows (yes, WSL makes localhost work exactly like normal)

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:

  • DockerDesktop (the Windows apps adds docker-compose and other useful things to Linux)
  • Git (needed for Windows GUI tools)
  • Power Toys (for tiling window management)
  • Powershell (the latest one is faster)
  • Visual Studio Code
  • Windows Terminal

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.

More Repositories

1

python-docx

Reads, queries and modifies Microsoft Word 2007/2008 docx files.
Python
1,066
star
2

powershell-profile

Mike's Powershell Profile (and how to set up Windows console if you've been using *nix for 20 years)
PowerShell
384
star
3

compact-wsl2-disk

A small script for Windows 10 Home users to compact their WSL2 disks
PowerShell
233
star
4

outdated-browser-rework

Detects outdated browsers and advises users to upgrade to a new version. Handles mobile devices!
JavaScript
224
star
5

agave

Cleaner, simpler JavaScript for ES6
JavaScript
115
star
6

serverless-starter-kit

A fulllstack serverless app using TypeScript, Svelte and Architect Serverless
TypeScript
67
star
7

stickers

Various stickers I made, printed and gave away at jsconf 2015
63
star
8

whois-json

Whois with results in actual, structured, camelCased JavaScript!
JavaScript
61
star
9

styleselect

Simple, standalone styling for select boxes in modern browsers.
JavaScript
37
star
10

dynamic-template

Simple dynamic ES6 templates
JavaScript
22
star
11

eyes

Removes eyes from the internet
JavaScript
15
star
12

sorts

JavaScript
15
star
13

eslint-plugin-must-use-await

Flags use of callback functions and .then(), for users of ES2017's async/await
JavaScript
11
star
14

kind.js

Return the kind of a JS object (the same as what a human would).
JavaScript
9
star
15

amps-in-the-trunk

Transform HTML directly to Google AMP HTML
JavaScript
8
star
16

companies-house

A JavaScript client for the Companies House API
JavaScript
7
star
17

pyfilter

Python client for pifilter adult image detection service (www.pifilter.com). Useful for helping automate moderation on public forums.
7
star
18

sass-npm

Import SASS files from npm modules
JavaScript
7
star
19

express-geoip

Express 4 middleware to add req.countryCode, which is the ISO alpha 2 country code, to requests.
JavaScript
6
star
20

is-gmail-account-valid

Find out if a gmail address is used or not
JavaScript
5
star
21

boilerplatejs

JavaScript
4
star
22

pr-chart

Simple AWS SAM / TypeScript / Arc / Svelte app
TypeScript
3
star
23

aoftmurmurkeys

Start and pause A Soft Murmur using the F5 key on your keyboard.
JavaScript
3
star
24

japanese_mayonnaise

Minimal Chrome extension. Removes animated notification area from Google search results.
JavaScript
2
star
25

epilepsy

Make your own Enter the Void style credits. With apologies to Tom Kan.
JavaScript
2
star
26

meatspace-enterprise-edition

JavaScript
2
star
27

devils_pie

A tool to solve the 10 Fast Finger Speedtest http://speedtest.10fastfingers.com/
JavaScript
2
star
28

ringespiel.js

A modern carousel for JQuery
JavaScript
2
star
29

jquery-html5-validity

JavaScript
2
star
30

express-middleware-to-async

Express to Arc middleware converter (Experimental)
JavaScript
2
star
31

imeveryone

Realtime anonymous communication platform
Python
2
star
32

burn-everything-down

JavaScript
1
star
33

rolex

A better 'watch'
JavaScript
1
star
34

scalpels-and-aks

JavaScript
1
star
35

dotfiles

Mike's dotfiles.
Shell
1
star
36

staring_and_slurping

Starts/stops Soundcloud using the F5 key on your Mac
JavaScript
1
star
37

chrome-bluetooth

Chrome Bluetooth test
JavaScript
1
star
38

emptiness.js

JavaScript
1
star
39

formsed

A simple Chrome extension to quickly search/replace form data automatically when matched URLs are hit.
JavaScript
1
star
40

boring-config

Load a CSON config file
JavaScript
1
star
41

goodbye_horses

High performance CMS
JavaScript
1
star
42

solana-basics-for-everyone

The Keynote Presentation from my Solana Basics for Everyone presentation at Breakpoint 2023.
1
star
43

london-javascript-events

List of all JavaScript events in London
1
star
44

minimal-messaging-extension

A minimal manifest v3 extension using chrome.runtime.sendMessage()
JavaScript
1
star
45

quaderno-server

Helps calculate JSON web tokens (JWTs) for one-off charges in Quaderno.
JavaScript
1
star
46

rejectjs2013

Welcome our new ES5 Overlords
JavaScript
1
star
47

goodbye-horses

JavaScript
1
star
48

nodeplayground

A personal scratch playground for testing ideas in Node JS
1
star
49

guetta

Adds additional David Guetta to websites
CoffeeScript
1
star