Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Adhjie/e8622c74cd4cf0ddd9f1b47774a2e5fd to your computer and use it in GitHub Desktop.
Save Adhjie/e8622c74cd4cf0ddd9f1b47774a2e5fd to your computer and use it in GitHub Desktop.
Install and configure Ubuntu WSL

Install and configure Ubuntu WSL

Installation

  1. Enable WSL2 as Administrator via Power Shell (information is from link1, link2):
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    then restart and call after wsl --set-default-version 2
  2. Update WSL from console opened as Administrator wsl --update and restart then using wsl --shutdown.

    If you have problem with self-update, try to use WSL kernel installer or check another tips from this article

  3. Install Ubuntu
    • from Microsoft Windows Store or
    • via cli
      • wsl -l --online
      • wsl --install -d Then check WSL version for installed Ubuntu distribution wsl --list --verbose. It must be 2. If distribution is not default it can be run so wsl -d <Distro>
    • NOTE: You can install not only Ubuntu, but other Linux family OS:
  4. Set up default username with a password
    Installing, this may take a few minutes...
    Please create a default UNIX user account. The username does not need to match your Windows username.
    For more information visit: https://aka.ms/wslusers
    Enter new UNIX username: gorshkov
    New password: ***
    Retype new password: ***
    passwd: password updated successfully
    Installation successful!
    To run a command as administrator (user "root"), use "sudo <command>".
    See "man sudo_root" for details.
      
    Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
     
      System information as of Fri Nov  5 00:28:25 +04 2021
    
      System load:    0.52      Processes:              7
      Usage of /home: unknown   Users logged in:        0
      Memory usage:   53%       IPv4 address for eth3:  10.236.151.120
      Swap usage:     0%        IPv4 address for wifi0: 192.168.1.7
    
    1 update can be applied immediately.
    To see these additional updates run: apt list --upgradable
    
    
    The list of available updates is more than a week old.
    To check for new updates run: sudo apt update
    
    
    This message is shown once a day. To disable it please create the
    /home/gorshkov/.hushlogin file.
    
  5. Update all packages
    sudo apt update
    sudo apt upgrade
  6. Optionally upgrade Ubuntu release. It takes usually 1-2 hours
    sudo LANG=C do-release-upgrade
    and check
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 21.04
    Release:        21.04
    Codename:       hirsute

In you have a problem like Command terminated with exit status 1 check this tip

  1. In order to run in current folder use command ubuntu.exe run . It can be usefull to add run icons to Total Commander

Configuration

  1. Edit /etc/wsl.conf (see documents)
    [wsl2]
    localhostForwarding=true
    [interop]
    appendWindowsPath=false # To avoid collisions. Any necessary paths can be set manually
    [automount]
    options="metadata" # To avoid this error https://askubuntu.com/questions/1115564/wsl-ubuntu-distro-how-to-solve-operation-not-permitted-on-cloning-repository
    # [user]
    # default=user # can be important to change default user
    then call wsl --shutdown

X-Server

  • Implementation
    • WSLG it should be the fastest solution but only on windows 11 (check if you have win11)
    • GWSL: the simpliest solution for windows 10
      • Run GWSL and click on icon in the status bar
      • Choose GWSL Distro Tools in opened menu
      • Tick Display/Audio auto exporting to set environment variables DISPLAY/PULSE_SERVER in WSL (see ~/.bashrc)
      • To make GWSL start on Windows startup use receipt
    • VcXsrv Windows X Server: classic, that is used by other solutions under hood, as well as in GWSL
      • Run VcXsrv and follow the suggestions
      • Add to ~/.bashrc this line
        export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
        
  • How to check
    • sudo apt install x11-apps
    • xeyes

Scripts

  • Mount flash drive
mountdrv() {
  sudo mkdir -p  /mnt/$1
  sudo mount -t drvfs $1: /mnt/$1 -o uid=$(id -u $USER),gid=$(id -g $USER),metadata
}

Convert Docker Container to WSL Distribution

set containerName=centos7
set tar="c:/WSLs/%containerName%.tar"
set DIR=c:/WSLs
set DIR_CENTOS=%DIR%/Centos
mkdir "%DIR_CENTOS%"
wsl --terminate centos7
wsl --unregister centos7
rmdir /S /Q c:\WSLs\Centos
docker run -d -t --rm --name %containerName% -it centos:7 /bin/bash
docker export %containerName% -o %tar%
wsl --import %containerName% "%DIR_CENTOS%" "%tar%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment