I am a newbie in tech. I have been using Linux and Windows side by side for quite some time now. This blog post is based on my personal experience. Feedback Welcome. Would really appreciate your time.
In my earlier blog post Linux Vs Windows – Why Linux Is Better for Programming & Web Dev, I quickly compared some key features of both the Linux and Windows OS based on my experience. I found Linux Operating System quite developer friendly and the community is huge. So, I switched to Linux (uninstalled Windows OS) to learn coding and web development. Being a Windows user and a newbie in tech, difficulties appeared immediately I switched to Linux. I have documented my experience in this post Hello Newbies in Tech! Switching From Windows to Linux? Read This First.
In this blog post, I will cover how to get started with a Linux distro on Windows machine without actually installing any Virtual Machine software. For this, we will install a tool called Windows Subsystem for Linux (WSL). Installing this will be almost similar to installing a software on Windows (with a little extra effort). There are several ways to use Linux but why did I install this tool in the first place? I wrote about this in my blog post – Hello Newbies in Tech! Switching From Windows to Linux? Read This First
This is part 2 of our 4 part series. You can find the full series here:
Part 1 – Linux Vs Windows (Why Linux For Programming & Web Dev)
Part 2 – Windows Subsystem For Linux – Run Linux on Windows (It’s FAST)
Part 3 – A Beginner’s Guide to Use Windows Subsystem For Linux
Part 4 – How to Install and Use Visual Studio Code Editor with WSL
Windows Subsystem for Linux (WSL)
In my previous article, based on a quick comparison between Windows and Linux, I talked about why aspiring programmers or web developers should choose a Linux distro. The last section of my article was especially focused on the downside of using Linux, especially if you are new to the Linux world and coding. Please read my post why did I say that. With the arrival of WSL, there is absolutely no downside for anyone.
Windows Subsystem for Linux (WSL) is a handy and fast tool announced by Microsoft to get a full GNU/Linux system inside of Windows.
Running and using a Linux distro on Windows will be as easy as using software on Windows.
WSL 1 Vs WSL 2 – Two Versions of WSL
There are two versions of WSL – WSL 1 and WSL 2. The former was the first version and WSL 2 is the newer version of the architecture. In the newer version, the Linux distros interact with Windows in a different way. To enable new features in WSL 2, the architecture uses virtualization technology and a Linux kernel. To enable virtualization, it uses Hyper-V architecture.
Advantages of WSL 2
WSL 2 is faster than its previous version. The two main advantages of using WSL 2 are:
- Faster system file performance speed
- Full System Call Compatibility
According to Microsoft:
Please note that the speed will also depend on which app a user is running, but operations such as git clone, npm install, apt update, and apt upgrade will be faster in WSL 2. Overall, the newest version is said to be 2-5x faster.
It is worth mentioning that WSL 1 won’t be abandoned.
Important Changes in WSL 2
There are two important changes in the current version.
- In WSL 1, you need to put the files, you wish to access with Linux applications, in the C Drive of Windows. But in WSL 2, the files must be kept inside of Linux root system to reap the benefit of file performance.
- As WSL 2 runs on a virtual machine ( no additional software need though), to access Linux network applications from Windows machine you need to use the IP address of that virtual machine. Similarly, to access Windows networking applications from Linux distro, you need to use the IP address of that Windows host. More here.
Here is the list of other changes in WSL 2. You can also go through this article to read more about WSL 2.
How to install the Windows Subsystem for Linux
Let’s get into the real thing now. Here is a step by step guide to install WSL.
- Prerequisites
- Enable WSL 1
- Install a Linux distribution
- Complete initialization of your distro
- Enable WSL 2
- Set WSL 2 as your default architecture
- WSL Version Check
Let’s get started.
Step 1: Prerequisites
The Windows OS build 18917 or higher is required. To check Windows build number, open Windows PowerShell as Administrator. To open it, press CNTRL + X buttons simultaneously using your keyboard and then click on “Windows PowerShell (Admin)”. Click Yes when prompted by User Account Control. Run the following command in PowerShell. This will give you the OS build number.
# systeminfo | Select-String "^OS Name","^OS Version"
If the build is not 18917 or higher, you need to join the Windows Insider Program and select the ‘Slow’ ring update type.
Caution: Before moving ahead, I strongly encourage beginners to read about Microsoft Insider Program. This talks about different types of Windows updates. Focus on the update type Slow. Once you have understood the risk, please proceed.
Open Settings > Update and Security. Look for Windows Insider Program and register your account. Once registered and enabled, you will see 3 options for Insider Settings. Select Slow (Recommended).
Now, update your Windows. Open Settings > Update and Security > Windows Update. Restart computer if asked. Check the build number again using the PowerShell command. This will fulfill the requirement to go ahead.
Step 2: Enable WSL 1
Open PowerShell as Administrator. Run the following commands one by one.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Running the above commands will install Virtual Machine Platform and Windows Subsystem for Linux. Restart your computer. To confirm if WSL is enabled, run this command in PowerShell as Administrator.
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
If you get a similar output as shown in the image below, you are good to go.
Step 3: Install a Linux distribution
Now you can install your preferred distro(s), I recommend installing a distro from the Microsoft Store. In this article, I will install Ubuntu. Open Store and Search for Ubuntu. Always choose the LTS version. LTS means Long Term Support which receives five years of support without any charge. Click on the LTS version.
A new page will open. Select "Get" and the distro will start downloading, and then it will be installed. If you want to install a different distro, please use this official Microsoft guide.
Step 4: Complete initialization of your distro
Once you have downloaded and installed a Linux distro, you need to complete initialization of this new distro. This is similar to what we do (such as set your username, password, etc.) during installing a Windows OS. Click on "Launch" button as shown in the image below.
A pop screen will appear. You will set username and password there. You can also follow this step by step guide to initialize a newly installed distro.
Moving ahead. Run wsl -l in PowerShell to check your installed distro. You will see the name of your distro.
Here I have installed Ubuntu-18.04 and this is the default distro. To check which version of WSL is installed, run wsl -l -v . It will display a '1' under the 'version' column.
Step 5: Enable WSL 2
In this step, we will set our distro Ubuntu to be backed by the newer version of WSL architecture - WSL 2. For this, you need to find your distro name. You can find this by running wsl -l command. It is Ubuntu-18.04 in my case. To enable WSL2, enter the command:
$ wsl --set-version Ubuntu-18.04 2
You will see "Conversion in progress, this may take a few minutes....". Wait for sometime. In the above command, you can always replace the word Ubuntu-18.04 with the name of your installed distro. The 2 in the command means we are going to change architecture to WSL 2.
You can always go back to WSL 1 by running the above command and replace the '2' with a '1'.
Note: In my case, during conversion from WSL 1 to 2, I got a error, which says, "Exporting the distribution failed. bsdtar: Write error". Here is the screenshot.
Many people have found the same issue while converting version 1 to 2. To fix this issue, "Turn Off Windows Real Time Protection". Run the conversion command again. This worked for me.
Check the GitHub thread to find the other fixes if the above did not work for you. Please note that you may need to run the conversion command twice if the conversion stucks somewhere.
Do not forget to "Turn On Windows Real Time Protection" once the conversion is done.
Step 6: How to set WSL 2 as your default architecture
If you wise to always use WSL 2, make sure you make this architecture as the default. Meaning whenever you will install any distro from Microsoft Store, the newer version WSL 2 will be installed. Run this command in PowerShell as Admin.
$ wsl --set-default-version 2
Step 7: Check which version of WSL is Installed
To check which version of WSL is installed, run wsl -l -v . It will now display a '2' under the 'version' column.
Troubleshooting
Microsoft has a dedicated page for the WSL related errors and fixes. For WSL 2 related issues, click here.
Starting up WSL
Now that we have installed Windows Subsystem For Linux, you need to update Ubuntu. From the Windows Start Menu, find Ubuntu 18.04 LTS (that is what we have installed). Run the program.
You will see a pop up, known as bash terminal, running on the WSL distro of your choice (here Ubuntu). Using this bash terminal, you can run any Linux commands of your choice.
First we will update our distro. To update the local database of available packages and upgrade the installed packages of Ubuntu, run this command:
$ sudo apt update && sudo apt upgrade
More about the above command is explained here. You would need to enter your password that you set during the initialization of your distro. During this process, it will ask your permission to download and install updates. Type Y.
Once the update finishes, the set up is complete. Note that the above terminal is also known as WSL terminal.