Setup Windows 10 With WSL for Golang Development
Windows 10 now supports Windows Subsystem for Linux and it let you run GNU/Linux environment on top of the Windows system.
I am going to walk through the minimum setup for Golang development with Windows 10 + WSL.
The setups are:
- Installing WSL
- Installing Ubuntu for the WSL
- Installing Docker
- Installing VS Code
- Installing Golang in WSL
Before you start, check your windows version with Windows Key + R
and then type winver
.
In case you have Windows version older than version 2004 you can’t install WSL 2, but you still able to install WSL 1. You need to update your Windows 10 to version 2004 Build 19041 in order to install WSL 2. To update it, you can update from your Windows setting or use the Windows update assistant.
In case you saw this following screen from update assistant, that is fine and you can keep installing WSL and using Linux environment on your Windows.
In my case, it is not an issue with WSL 1 for my development enviroment. For full comparison between WSL and WSL 2 you can check it here.
Installing WSL
To install WSL, you need to enabling “Windows Subsystem for Linux” optional feature. There are two ways to doing it.
First way, open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Second way, goes to Turn Windows features on or off
setting and check Virtual Machine Platform
and Windows subsystem for Linux
Installing Ubuntu for WSL
Installing Ubuntu is pretty simple in Windows 10. You can go to Windows Store and search ubuntu
then click “Get”, it will install it on your WSL directly.
Once the installation is done, try to press Windows Key and type “Ubuntu”, then open the Ubuntu application listed there, you should be redirected to a linux terminal and your linux system already up and running.
Install Golang in WSL
In this example, we are going to install Go 1.14.4 version, in case you want to install the newer version, the steps should be identical you just need to replace the download link from Go official site. Open your WSL terminal and run these commands:
# Download the Golang source into ~/Download folder
wget -P ~/Download https://golang.org/dl/go1.14.4.linux-amd64.tar.gz # Extract the source code
sudo tar -C /usr/local/ -xzf ~/Download/go1.14.4.linux-amd64.tar.gz
To verify your installation you can type go version
and it should show the go version installed on WSL.
Installing VS Code
For working with VS Code, you should install it on your Windows side, not in the WSL. Go to VS Code site and download the Windows installer. Then open the downloaded .exe
file and finish the installation.
When prompted to Select Additional Tasks during installation, be sure to check the Add to PATH option so you can easily open a folder in WSL by typing code
command on the working directory.
After the installation is finish, install the “Remote Development Extension Pack” to your VS Code. You can install it from here, or you can search it on the VS Code extension market place.
After restarting you VS Code, you should see WSL on the bottom left corner of your VS Code. And now you are able to use your VS Code normally.
Installing Docker in WSL
This step might not required if you are not using docker for your development. In case you need it you can run these following command from your WSL. (in my case, I am installing Ubuntu 20.04 in my WSL)
# download .deb package for docker
wget -P ~/Download https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/docker-ce_19.03.12~3-0~ubuntu-focal_amd64.deb
wget -P ~/Download https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/docker-ce-cli_19.03.12~3-0~ubuntu-focal_amd64.debwget -P ~/Download https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/containerd.io_1.2.13-2_amd64.deb
# installing docker-cli and docker-ce
sudo dpkg -i ~/Downloads/docker-ce-cli_19.03.12\~3-0\~ubuntu-focal_amd64.deb sudo dpkg -i ~/Downloads/containerd.io_1.2.13-2_amd64.deb sudo dpkg -i ~/Downloads/docker-ce_19.03.12\~3-0\~ubuntu-focal_amd64.deb
Try verify your installation with docker info
, in case you found the output something like this one, try to use sudo
.
Client:
Debug Mode: falseServer:
ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/info: dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info
If you want to access docker without sudo
you can run this following command:
sudo usermod -aG docker $USER
Afterwards, close the terminal or logout your profile to take effect. That command is going to add your linux user into docker user group.
Further Setup
AutoHotkey (Key Mapper)
AutoHotkey will help you to remap your keyboard buttons for better experience with your development. Let say you are using vim
and you want to swap your CapsLock
into extra Esc
button.
Config for Swap CapsLock
into extra Esc
button:
;Remap hotkeys
CapsLock::Esc
Esc::CapsLock
PowerToys (App Launcher)
PowerToys is an app launcher that might help you productivity. This app launcher works like “Spotlight Search” in the MacOS.
References:
- Update Assistant for update Windows 10 to version 2004: https://support.microsoft.com/en-us/help/4028685/windows-10-get-the-update
- Install WSL and update to WSL 2: https://docs.microsoft.com/en-us/windows/wsl/install-win10
- Using VS Code for development in WSL https://code.visualstudio.com/docs/remote/wsl
- WSL documentation https://docs.microsoft.com/en-us/windows/wsl/