Getting Started with Windows Sub System for Linux (WSL)-Installation & Command Line

Mostafa Farrag
8 min readJun 10, 2023

WSL (Windows Subsystem for Linux) is a feature in Windows that enables you to run a Linux environment directly within Windows. WSL offers a way to bridge the gap between Windows and Linux environments, providing developers and users with the flexibility to leverage the strengths of both operating systems on a single machine.

Why do you need WSL?

  1. Compatibility: Many software development tools and frameworks are primarily designed for Linux or Unix-like systems. By using WSL, you can have access to a native Linux environment on your Windows machine, allowing you to run and develop Linux-specific software without the need for a separate physical or virtual machine.
  2. Command-line utilities: Linux provides a rich set of command-line utilities and tools that may not be available or have limited functionality in the Windows Command Prompt or PowerShell. With WSL, you can utilize popular Linux command-line tools, shell scripts, and programming languages in their native environment.
  3. Development environment: If you are a developer working on projects that target Linux servers or environments, WSL can provide a convenient and consistent development environment. You can run web servers, databases, and other server-side software within WSL to closely simulate a Linux environment.
  4. Learning and experimentation: WSL offers an opportunity to learn and experiment with Linux without having to install a separate Linux distribution or dual-boot your machine. It allows you to familiarize yourself with Linux commands, shell scripting, and software development practices within a controlled and integrated Windows environment.
  5. File system integration: WSL provides integration between the Windows file system and the Linux file system, allowing you to access and manipulate files seamlessly. You can work with files and directories from both systems interchangeably, making it convenient to edit files using both Windows and Linux tools.
  6. Open-source development: If you contribute to open-source projects or work with open-source software that is primarily developed and tested on Linux, WSL can provide a more streamlined experience for building, testing, and debugging.

In this article, I will go through the following list

  • Check if there is already an installed version of WSL
  • Activate the WSL feature
  • Installation WSL
  • launch WSL
  • Change the Home directory
  • Most used terminal commands

Prepare for installation

Check if there is already an installed version of WSL

To check if there is an installed version on your machine, type the following command in a terminal

wsl -l -v
  • If there is no WSL installed in your machine, you will get a similar response to the following.
  • If you get the “Error code: Wsl/WSL_E_WSL_OPTIONAL_COMPONENT_REQUIRED” error message, you need to activate the WSL optional component
WSL_E_WSL_OPTIONAL_COMPONENT_REQUIRED error
  • To activate it, you need to go to “Control pane”, then “Programs and Features”, click on “Turn Windows features on or off”, then search for “Hyper-V” and mark the tick box beside it.
activating WSL optional components
  • In case there is an installed version on your machine, the name, state, and version of the installed versions will be displayed

Now supposed that we found out that there is not WSL installed in our machine, then now we need to install it, the first step to install WSL, is to activate it first.

Activate the WSL feature

First, you have to activate the Windows sub-system (WSL) for Linux feature in your computer as it might not be activated, to activate it copy and paste the following command in your powershell terminal and press enter (make sure to open the terminal as an admin)

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart/

After activating WSL, reboot your computer.

Installation WSL

to install WSL, in your terminal type the following command

wsl --install

It will take some time to complete the whole installation, at the end the installation will ask you for a username and a password.

now if you check the installed versions, you have to see “Ubuntu”

launch WSL

  • To start WSL, type wsl in your terminal.
  • You will find that the username you used followed by your Windows username, and then a path to a directory pointing at your Windows user folder.

Change the Home directory

  • Now to change the home directory of your WSLwe are going to do the following, type the following command, then press enter, you have to enter the password you used to install the WSL
sudo vim /etc/passwd
  • what we have done is that er opened the passed file which is a is a plain text-based database that contains information for all user accounts on the system.
  • vim is a text editor that is installed in the WSL, to reach the line where the home directory is installed, go down to the last row.
  • Now we can change the /home/<username> to whatever directory we want, however we have to use the WSL notation.
  • To follow the WSL notation, we have to prepend any windows path with mnt/, like /mnt/c for C:/, /mnt/d for D:/, etc, and use forward slach.
  • Now to enter the edit mode of vim press i (you will see — insert — appeared at the bottom once you press i), then replace the /home/<username> with the new path.
  • Now that is how the file looks like (I created a folder in the following directory “C:\MyComputer\wsl” for my WSL).
  • To save what we have changed in the file, press Escape on your keyboard, then write :wq and press enter.
  • Now exit WSL and open it again
  • Now to check where is the home directory type pwd and press enter, you have to see the path you have entered in the etc/passwd file, if not, you probably have entered it incorrectly.
  • If you check the directory you have entered as the home directory for your WSL, you will find some files that are created new.
  • Now you can browse anywhere in your Windows folders from inside WSL, now if I go back two levels, I can see the content of my c partition.
  • Next time you open WSL, you have to trigger a bash script called .bashrc, this script configures the terminal for you, and you have to trigger it every time you open WSL.
source ~/.bashrc

Now You have WSL installed on your machine, and you can reach any directory in your Windows system from WSL.

Most used terminal commands

To be able to work with the WSL from the terminal, you have to know some basic bash commands to make your life easier.

There are a variety of shells available, including Bash, Korn shell (ksh), C shell (csh), and Z shell (zsh). While each shell has its own unique syntax and features, they all serve the same purpose of providing users with a command-line interface to interact with the operating system. Bash (Bourne-Again SHell) is one of the most commonly used Unix/Linux shells and is the default shell in many Linux distributions.

Generally, commands follow this syntax:

command [OPTIONS] arguments
  • To check the path to the current work directory, use the command pwd
  • To browse to a certain path use the cd command followed by the path, the ~ takes you to the home directory.
cd ~
  • mkdir creates a directory.
mkdir <folder-name>
  • ls lists the content of the current directory.
  • touch command creates a file.
touch <file-name>
  • rm removes a file.
rm <file-name>

if you use rm with a flag -f you can remove a directory with all its content.

rm -r <directory-path>
  • cp to copy file to another directory.
cp <file-name> <target-directory>
  • Similarly, mv command moves the file entirely to the target directory.
  • To get all the descriptions of any commands, you can type “man” then the command.
man <command>
  • then a description of the command will be opened in the terminal, to get out of the description type q.
  • Similarly, you can use the help flag.
<command> --help

In the next article, I will explain how to deal with environment variables, and the bashrc file, store aliases, and install all the needed software and packages to make your life easier when using WSL.

--

--