Create Your Custom WSL from any Linux Distribution (Part-1)

The Complete Tutorial to Install and Setup any Linux Distribution in WSL

Create Your Custom WSL from any Linux Distribution (Part-1)

Summary of the Content Prior to Reading the Article

Ever wanted Arch or Void Linux as your WSL distro for Windows? Do you know that you can actually (YES ACTUALLY!!!) install any Linux distribution as your WSL distro? This guide covers how to import any Linux distro to WSL2 using a tar file. We'll use a Docker container to get the tar file, import it to WSL, and set up Void Linux as an example. Follow the steps to download the Docker image, export it to a tar file, and import it to WSL. We'll walk through post-installation configurations like creating user accounts, setting up default shell and user, updating the system, and making WSL accessible as a Windows desktop app. By the end, you'll have a fully functional, custom Linux distro on your Windows machine.

Initial Discussion

If you run wsl -l -o in your windows terminal (cmd or PowerShell), you'll see an output like this -

List of valid distributions that can be installed using

This list is really disappointing. I mean there are many more distros out there with different package management systems and different useful features. Like - fedora, Arch, Void, Artix, Alpine, etc.

Now you may also think that the options are very limited in case of WSL. Which is not actually that true.

If you even search at MS Store, You'll see some third party linux distributions that are specifically developed for WSL (WSL Only Linux Distributions).

While the ArchWSL and Fedora WSL at MS Store may seem great at first before installing, these distros have often showed compatibility issues and sometimes very weird bugs; even conflicts with scoop or chocolatey apps.

How to install other distros then?

WSL2 provides us a way to import any linux distro as a WSL instance from a tar file(or say backup) of the Linux OS in a machine.

For example you have a laptop where you have fully installed some linux distribution as the operating system. Then you can use the tar command to make a compressed tar file replicating your whole OS starting from the / (root) directory point as one file system.

Now you can transfer the tar file in your windows machine using a USB drive. Next, you use the --import flag of the wsl command and, a new WSL instance with a filesystem (virtual hard drive) & a provided name gets registered within the subsystem.

Let's walk through a complete tutorial to get you covered.

Installing Void Linux in WSL

Well by far the easiest way to get a tar file of an OS is to use a docker container. Follow the steps I describe below.

Obtaining The TAR file (Archive)

  • First of first, pull the official docker image of void linux from GitHub Container Registry. Make sure you already have a WSL instance (Ubuntu or openSUSE or any other) installed and setup for docker.

      docker pull ghcr.io/void-linux/void-glibc-full
    

    After pulling it should show up in the images list-

    installed docker images

  • Now run the container using -

      docker run -it ghcr.io/void-linux/void-glibc-full sh
    

    It should successfully enter the voidlinux container. You can run any command to check if the shell is really working or not. like shown below -

    Running the void linux docker container in interactive mode

  • Next keeping this terminal instance alive without exiting the container, open another WSL terminal instance and run this command.

    List of running containers

    You should be able to see the container included in running containers list like in the image.

    Run the following commands below -

    Getting the running container ID

    We've got our running container ID. Yoohooo!

  • Now run this final command to obtain the tar file. Change the path of the tar file based on your choice. In my case it is the VMs folder in my E: drive. (Yes you should read first before running any command. Blindly copying and pasting commands is the loser's choice)

      docker export $dockerContainerID > /mnt/e/VMs/voidlinux.tar
    

    You can stop the container and exit the WSL terminal afterwards.

Using the TAR file

If you go to the path where the tar file was created in windows explorer, you will see the tar file there.

seeing the voidlinux tar file

Now let's create a new folder 'WSLs'. Move the tar file in there. In there create a new folder Void.

Moved the tar file in a specific directory

Now open the folder 'WSLs' in your terminal (cmd or pwsh), and run this command -

wsl --import Void E:\VMs\WSLs\Void\ .\voidlinux.tar

Here,

  • The first argument after wsl --import is the one you choose to be the name of the distribution that is going to be imported.

  • The 2nd argument is the absolute path of the directory where the virtual hard disk image file(.vhdx) is going to created.

  • The 3rd argument is the path of the tar file.

After this command is successfully executed, you'll see the filesystem of Void in the Linux subsystem (open file explorer to see).

Void Linux successfully registered

Yeah! Cool!

💡
Don't delete the (.vhdx) file just got created. If you think now you can delete the virtual hard disk image file because Void is now imported and currently exist in your Linux subsystem, then you are totally wrong. The filesystem you can see (just as in the above image) is only accessible when you have the vhdx file and!!! exists in the same path.

You've successfully installed Void Linux in your Linux Subsystem. Huge Congratulations! ✨✨✨

Things to Do After Installing a Custom Distribution

Now if you are thinking, you're all done, then you're again making a mistake.

How?

Because the way we installed the OS in the subsystem, it was not like how automated we get the online available WSL distros. Generally, when you install ubuntu, kali or OpenSUSE via the commandline or MS Store, it automatically creates a user account (makes it the default one) for you with a password, with a bunch of configurations behind the scenes.

Now because we installed the OS in our subsystem with a bare import, we got nothing of setups out of the box. There's only one user account, which is the root user.

We will create a user account, provide it a password and add it in the sudoers file.

Prerequisites

Open the void linux shell with wsl -d Void in your powershell/cmd terminal. (Don't worry we will make a convenient way at the end for launching the app or say, shell)

Once it opens, you'll run the following commands.

But before creating user accounts let's perform some prerequisites.

First, run -

clear

you'll be shocked!

clear command NOT found

Because our OS is imported from a base install of void linux container. We don't have all important tools yet. Clearly, the clear command shouldn't work because we don't have 'ncurses" installed. "ncurses" is a library that provides terminal handling and user interface functions for C programs. Void Linux uses the xbps package manager to install, update and remove apps/softwares.

Info about the XBPS Package Manager

xbps-install -S ncurses

installing ncurses

Now the clear command should work successfully.

The second step should be updating the system. As Void Linux is a rolling release distribution, it gets frequent updates. You should update the system as often as you can (like every day if possible). Update it with -

xbps-install -Syu

The -S flag means sync, -y means yes and -u means update. We combine these flags together and write it as -Syu . The system update may take some time if you are having poor internet connection.

After the full system upgrade, install less and bash. Bash (Bourne Again Shell) is not currently installed in your system. It is not provided by default. The shell you are using to run commands is sh (Bourne Shell), the predecessor of Bash.

xbps-install -S less bash
💯 😎 Pro Tip
Pipe those commands into less which take more height in their STDOUT than your terminal height. Thus, you would be able to scroll through the output with arrow keys and search words with a forward slash (/). Particularly useful when viewing help texts of a program. No need to touch the mouse!

Now change the default shell from SH to BASH. -

chsh -s $(which bash)

Now if you exit the shell and re-enter in it, you'll see that the prompt string of the commandline has been changed indicating that, - the terminal is running in its default shell has been changed to bash from sh.

default $PS1 of bash

And it should look something like above.

Creating a user account

Now it is the time to create a user account which will be the default user.

xbps-install -S sudo

This will install sudo in your voidlinux system.

Installing sudo

Now run this command below, to create a user account with home directory.

useradd -m <your-username>

replace <your-username> with the username you want.

Run the below command to list all available groups.

cat /etc/group

Listing all the currently available groups in the system

Now append the groups you want, to your user using the usermod command.

This is the syntax -

usermod -aG <group_name> <username>

Heh! BTW you can place multiple groups in place of <group_name> by keeping them separated by commas. For example, I think I would do this -

usermod -aG wheel,audio,video,kvm,tty,storage,plugdev,lp,dialout,users <username>

For satisfaction, you can run -

groups <username>

to see that the user actually got access to the groups we specified in the command.

Displaying all the groups, the newly created user has acess to

In case you don't know, -

GroupsTheir Meaning ( Usecase )
wheelto grant users the ability to execute commands as the superuser (root) using sudo.
ttyrelated to terminal devices. Group for access to terminal devices if needed.
storageThis group is typically used for users who need access to storage devices, like - external drives.
audioGrants access to audio devices.
videoGrants access to video devices.
dialoutProvides access to serial ports.
lpGrants access to printer devices.
kvmfor users who need to manage virtual machines using KVM (Kernel-based Virtual Machine).
plugdevAllows access to removable devices like USB drives.
usersThis is a general group for regular users.

Neat! We're almost done creating a regular user account. But the last crucial step is not done yet!

We need to add the user to the sudoers file so that it can get superuser access (admin privilege) using the sudo command. Also, for that we should first set a password to both the regular and root user. You can set the same password for both users if the WSL we installed is not going to be used/touched by anyone else whom you don't trust, and you also tend to forget things.

Setting A Password

To add or change passwords for a user we need to have the passwd utility installed in the system. If not already available, install it -

xbps-install -S passwd -y

type passwd and you'll be prompted for setting the password for the root user.

Next, -

passwd <username>

Replace <username> with the user we just created now.

Adding user to Sudoers File

Now we need to edit the sudoers file to properly grant superuser access to our user. For that we need an editor. If you are comfortable with nvim install it otherwise install nano for text editing.

xbps-install -S neovim

or

xbps-install -S nano

Now do sudo visudo to edit the sudoers file.

EDITOR=nvim sudo -E visudo

Replace nvim with nano if you want to use nano.

user privilege specification in the sudoers file

Find the uncommented line shown in the image, in your file. And write this line (look below) under there.

<username> ALL=(ALL) ALL

It would look like this -

Specifying the user privilege of the new user in the sudoers file

Now save the file and exit.

Finally set the new user as the default user.

myUsername=<username>
echo -e "[user]\ndefault=$myUsername" >> /etc/wsl.conf

Now we are all set. Exit the Linux shell and terminate the distro by running -

wsl --terminate Void

Then open it again with -

wsl -d Void

So, yeah! We did it!

Optional Extra Configurations

As of now without any configuration, the prompt string (PS1) would look like this -

Default prompt string of the commandline in the session of the current user

which is really reallly ugly to me. I mean, I would need to enter pwd every time whenever I need to check which directory I am currently in, which absolutely sucks!

So, let's change the prompt string.

open your .bashrc file ( assuming you are using nvim )

nvim ~/.bashrc

Go to the last line.

Add this line of code to the next line to change the prompt string.

PS1="\[\e[32m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[36m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\[\e[30;46m\]\\$\[\e[m\] "

I know the string may seem unreadable obfuscated some nonsensical gibberish, but it is only because of the ANSI Escape codes heavily used here.

Save the file, exit and do

source ~/.bashrc

to make the changes take effect and HURRAYY! Now you have an elegant and useful prompt string before the commandline.

Voidlinux WSL set up with a fancy colorful prompt string

Also if you read man pages a lot you will need to have the MANPATH environment variable set on the startup.

Setting the MANPATH env variable in the .bash_profile file

Add this line (as shown in the image above)-

export MANPATH=/usr/share/man

Now if you have man and man-db installed, you can successfully access man pages from the commandline with the man command.

Keeping this WSL as a Desktop App

As we all clearly understand, launching the WSL instance by opening any cmd shell and running wsl -d Void is not a very convenient approach.

Most probably after a reboot of your PC, you'll see a new terminal profile has been automatically added, in which our void linux shell resides. If not, then create a new terminal profile in your windows terminal for void.

Now go through the following steps:

  1. Right click in your Desktop Background and select the option of new desktop shortcut. You'll see a popup like below.

    Typing in the given input area, the command/location of the file/process we are creating a desktop shortcut for

    You have to write the appropriate command in the empty input area provided, that will open the newly created terminal profile of void linux in its $HOME directory of the default user.

  2. Now, type - C:\Users\user\AppData\Local\Microsoft\WindowsApps\wt.exe nt -p Void --tabColor #27e336 in there and click next. (make sure the path of wt.exe is correct. If you have wt which is windows terminal in a different path, then type that one)

  3. Now type the name of the shortcut as you want. I'm giving it the name - void Now click Finish.

Congrats! Another milestone achieved! Now you can access this WSL from your windows search bar directly by typing void. How amazing!

I suggest you change the icon of the app to anything else that will enable it to get easily caught in sight (you may need to download an image and convert it to an ico file if you want it to have the void linux logo as the icon).

This is how my desktop app look like -

Void Linux Desktop Shortcut

And, that's all! Really! You finally got a new custom Linux distribution in your subsystem which is not readily available in the MS Store or online WSL registries, configured and ready to use.

Final Thoughts

Soo, how are you feeling! Tell me in the comments!

If must feel chaotic good to get access to most of the needed cutting edge development softwares/tools right within the terminal. One Package Manager to rule them all. If you are tired of getting old or outdated packages in Debian/Ubuntu then this is going to be an overwhelming experience.

In case you couldn't follow the steps to produce the tar file, or somehow faced any kind of trouble and thus not getting the tar (archive). Don't worry.

I am attaching the mega link of the Void Linux tar file I created for you so that you can at least try it out! ;)

This is the decrypt key of the mega file - du3JdIqxLCPjXKIcLJ9Gn0UFcmQKVEz3rADwe4_MQi4

Thank you for giving this article a read!

If you found it useful, please consider to share this article with your other developer friends.

Feel free to connect with me on Twitter, LinkedIn, and GitHub.

Linux users are hackers! Happy Hacking! 🐱‍💻

Did you find this article valuable?

Support Debajyati's Blogs by becoming a sponsor. Any amount is appreciated!