The following guide might help you to initialize Ubuntu distributions due to different purposes (e.g. Go or .NET Core development, running Docker containers, etc.) in WSL2 without using Microsoft Store where Ubuntu and other distributions are available. I originally made it to help me remember the settings I use. Later maybe I will automatize with a PowerShell script.
The guide assumes, that you are running Windows 10 which is updated to version 2004 (build 19041 or higher) and you have WSL2 already installed.
Before everything you have to download the optimised and certified Ubuntu server images from the Ubuntu Cloud portal's image repository. Please find the lastest versions below:
- Ubuntu Server 20.04 LTS (amd64):
http://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz - Ubuntu Server 19.10 (amd64):
http://cloud-images.ubuntu.com/eoan/current/eoan-server-cloudimg-amd64-wsl.rootfs.tar.gz - Ubuntu Server 18.04 LTS (amd64):
http://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64-wsl.rootfs.tar.gz
According to the WSL Documentation, the best way to interact with the Windows Subsystem for Linux is to use the wsl.exe
command.
To list the existing distributions, issue the wsl -l -v
command. This will help to avoid to try deploying a new distribution with the name of an existing one.
NAME STATE VERSION
* distribution1 Running 2
distribution2 Stopped 2
Deploying a distribution can be done with the wsl --import <distro> <deployment_folder> <image>
command. Let's assume, that we would like to use the new distribution for developing .NET Core application with Ubuntu 20.04 LTS, therefore select ubuntu-20.04-dotnetcore
as the name of the instance.
wsl --import ubuntu-20.04-dotnetcore E:\WSL\ubuntu-20.04-dotnetcore E:\Downloads\ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz
After the successful deployment, we can see the new distribution in the list.
NAME STATE VERSION
* distribution1 Running 2
distribution2 Stopped 2
ubuntu-20.04-dotnetcore Stopped 2
Since the new distribution is not the default one, it can be used with the wsl -d ubuntu-20.04-dotnetcore
command. If you would like to have it as default, it can be easily done by issuing the wsl --set-default ubuntu-20.04-dotnetcore
command.
By default there is no other users in the case of the new distribution just the root
. Due to security reasons I recommend to create a normal user which has sudoers permission. Run the new distribution on the Windows host:
wsl -d ubuntu-20.04-dotnetcore
First, you have to create a user. In the guide let's call it as normaluser
, and please issue the command below and follow the inctructions.
adduser normaluser
As a next step give the sudoers privileges to the normaluser
:
adduser normaluser sudo
Set normaluser
as the default one via the /etc/wsl.conf
:
tee /etc/wsl.conf <<BLOCK
[user]
default=normaluser
BLOCK
The change will be effective only in a new session, so let's exit from the current one with the logout
command, and shut down the distribution in the WSL.
wsl --shutdown ubuntu-20.04-dotnetcore
When you run the new distribution again with the wsl -d ubuntu-20.04-dotnetcore
command the normaluser
will be used by default.
Powerline is a statusline plugin for vim. Based on its idea Powerline-Go has been created to have a beautiful Bash prompt.
To install it you have to issue the following commands within the new distribution (after issuing wsl -d ubuntu-20.04-dotnetcore
command):
sudo su
cd /usr/local/bin
wget https://github.com/justjanne/powerline-go/releases/download/v1.17.0/powerline-go-linux-amd64
chmod +x powerline-go-linux-amd64
tee -a /etc/bash.bashrc > /dev/null <<BLOCK
# Enable Powerline-Go
function _update_ps1() {
PS1="$(/usr/local/bin/powerline-go-linux-amd64 -error $?)"
}
if [ "$TERM" != "linux" ] && [ -f "/usr/local/bin/powerline-go-linux-amd64" ]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi
BLOCK
Remark: Powerline-Go requires fonts which contain some special characters which are related for example to Git. In case of Windows Terminal I recommend to use Cascadia Code, which can be set in settings.json
file with adding "fontFace": "Cascadia Code PL"
to the actual terminal profile.
If you have dynamic profiles disabled in the case of Windows Terminal by having "disabledProfileSources": ["Windows.Terminal.Wsl"]
configuration in the settings.json
file, you will have to create a new profile for the new distribution.
First, you have to generate a new GUID which will be the new profile's identifier. I prefer to use PowerShell via the following command:
[guid]::NewGuid()
Guid
----
1e5aab99-7d9f-44c8-be87-4b5f89e27702
Now you can put the following configuration to the "profiles"
block in the settings.json
file. You can set any image as icon of the new profile, I used the one from yanglr's GitHub repository. Intallation of the Cascadia Code font has been already described above.
{
"guid": "{1e5aab99-7d9f-44c8-be87-4b5f89e27702}",
"hidden": false,
"name": "Ubuntu 20.04 (.NET Core)",
"icon": "ms-appdata:///roaming/ubuntu_32px.png",
"commandline": "wsl.exe -d ubuntu-20.04-dotnetcore",
"startingDirectory": "//wsl$/ubuntu-20.04-dotnetcore/home/normaluser",
"fontFace": "Cascadia Code PL"
}
If you would like to avoid the message of the day (known as MOTD) than you have to issue the following command in the case of normaluser
:
/home/normaluser/.hushlogin
In case of using the new distribution with Git, then some basic configuration should be used within the new distribution (after issuing wsl -d ubuntu-20.04-dotnetcore
command):
git config --global user.name "Your Name"
git config --global user.email "yourname@email.com"
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
git config credential.helper store