Long gone are the days when one had to choose which OS to use – Windows or any Linux distribution. Some time ago Microsoft introduced a tool that allowed to run Linux programs within Windows OS. Currently, there is a 2nd version of this tool called Windows Subsystem for Linux.
In this article, I’ll describe what a WSL is and how to enable and start working with it.
What is a WSL?
Windows Subsystem for Linux is a feature of both – Windows 10 and Windows 11 that allows
to run Linux command-line tools (also some graphical programs), utilities and applications directly from Windows.
In order to do so, at least one of the most popular Linux distributions, such as Ubuntu, Debian, Fedora, Kali Linux etc. needs to be installed within the WSL. This provides a full Linux environment while still being able to use Windows applications and tools at the same time.
What does the WSL change?
So far, if one wanted to work both with Windows and Linux, there were two main options:
- Dual boot – install Windows and Linux as separate operating systems – unfortunately it wasn’t the most convenient solution as it was possible to work with one OS at the same time,
- VMs – use virtual machines, for example, Windows as host and Linux as VM, or the other way round – those were still 2 separate operating systems, that could be run at the same time, but as one was a full VM, it was consuming lots of hardware resources (RAM, CPU, etc.).
With WSL, there’s no need to use a dual boot or set up a fully featured VM. The overall experience is more compliant, easier for the end user, and consumes fewer resources than a typical VM.
Difference between WSL 1 and WSL 2
WSL 1 was first introduced in July 2016, while WSL 2 was first introduced in May 2019. Of course, the age of those two features is not the only difference. Their approach to running Linux applications is totally different.
How does the WSL 1 work?
WSL 1 uses a compatibility layer in order to translate Linux system calls into Windows system calls, allowing Linux binaries to run natively on Windows. This introduces some limitations – slower file system performance, limited access to system resources and lack of support for running Docker containers.
How does the WSL 2 work?
WSL 2 handles the same problems quite differently. First of all, there’s no compatibility layer anymore, but a lightweight virtual machine to run Linux kernel. This results in improved performance and better compatibility with Linux applications and tools. What is more, WSL 2 now also supports running Docker containers and provides access to the same system resources as Windows, such as GPUs (especially interesting for Machine Learning), serial ports or USB devices.
As WSL 2 is the newest, recommended-to-use solution, I’ll focus on this version in the article.
Setting up WSL
Enabling WSL is usually easy and straightforward, but can be also a bit tricky at the same time. First of all, you need to remember about few details.
Prerequisites
In order to enable WSL, you need to fulfil the following prerequisites:
- Windows 11 or Windows 10 version 2004 and higher needs to be installed,
- As WSL 2 utilizes VM, virtualization must be enabled in BIOS/UEFI,
- You need to have admin privileges in the system.
Recommended tools
I’ll use the following tools:
- Windows Terminal – modern terminal application for Windows that provides a CLI environment (for details, please see the documentation),
- Visual Studio Code (see the next article),
- IntelliJ IDEA (see the next article).
Before installation
Before you start, I would really recommend you install the Windows Terminal, as well as… create a recovery point for the Windows. WSL is quite a mature and proven tool, but it’s still under constant development, and sometimes, after the installation process it causes the OS malfunctions resulting in OS start failure. Therefore, it’s better to be prepared in advance for that scenario, and when the problem occurs – just to recover the OS to the point before any changes were made.
Installation process
The first step would be to decide, which Linux distribution you would like to use within the WSL. Open Windows Terminal and type in:
wsl -l -o
To list all available install Linux distributions. The one marked with an asterisk “*” is the default one that would be installed by command:
wsl --install
Installation for users with admin privileges
In order to install specific Linux distribution, open Windows Terminal with admin privileges and type in, for example for Ubuntu 22.04 LTS:
wsl --install -d Ubuntu-22.04
The admin user to run Power Shell has to be the same, one that is currently logged in. When you try to run the Power Shell terminal as an admin user, but are different than is currently logged in, you may encounter the following failure:
Installing: Virtual Machine Platform Virtual Machine Platform has been installed. Installing: Windows Subsystem for Linux Windows Subsystem for Linux has been installed. Installing: Windows Subsystem for Linux Catastrophic failure.
Linux configuration
When WSL is installed, you’ll need to reboot your PC. Then, after you log in again to the OS, a terminal with Linux configuration appears. You need to define a new user – you can choose whichever name you want, as this is a totally separate Linux OS user – it’s not connected in any way with a Windows user.
Then you’ll be asked for a password. Again, you can type whichever password you want. Note that no single asterisk nor dot sign appears in the terminal when you type in. This is totally normal behaviour. Just type in the new password and approve it with enter. Then retype it again and everything is ready. You can find best practices for setting up usernames and passwords here.
Installation for users with no admin privileges
Installation of WSL 2 becomes quite tricky and counterintuitive in this scenario. First of all, even if you know the admin user login and password and open Power Shell with that user, you will probably encounter the installation failure mentioned above. You could log in to the OS as an admin user and install WSL 2 from that account, but then the Linux distro would be available only from that admin user.
If you were logged in as a non-admin user, you would have to run Power Shell as administrator every time when you’d like to use WSL – this is not the best solution.
The first step I would recommend in this scenario is to set the default WSL version to 2 by running the following command in Power Shell (open Windows Terminal):
wsl --set-default-version 2
Then, install the desired Linux distro, for example, Ubuntu-22.04 LTS:
wsl --install -d Ubuntu-22.04
Linux configuration
Linux terminal may appear without rebooting your PC. Follow the same instructions as mentioned above to define a new user and password. Then you can close the Linux terminal.
Check the WSL version
As I mentioned earlier, WSL installation for non-privileged users can be quite tricky. You need to verify which version of WSL was installed by running the command in Power Shell:
wsl -l -v
If the output is like below:
NAME STATE VERSION * Ubuntu-22.04 Running 2
Then you have the correct version of WSL installed. However, if you see 1 under the version, follow these steps to migrate to version 2.
Migration from WSL 1 to WSL 2
Open Power Shell (for example by running Windows Terminal) as administrator and run the following command to enable the virtual machine feature:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
In the next step download and open the migration tool described here.
After installation, open Power Shell and run the following command again:
wsl --set-default-version 2
And finally, migrate the existing WSL Linux distro to version 2:
wsl --set-version Ubuntu-22.04 2
When the above step completes, you can again verify the version of WSL Linux distro installed:
wsl -l -v
Steps to do after installation
Linux distribution within WSL was successfully installed, but how can you get access to it, and what needs to be done now?
How to access Linux distribution
In order to use Linux, simply run Windows Terminal. Probably (if you haven’t changed it in the settings), the default tab would be a Windows Power Shell. You can then choose the down arrow or use the shortcut to open the Linux terminal. One of the advantages of Windows Terminal is the fact, that when Linux distribution is installed within the WSL, then it’s automatically registered in the program. See the screen below:
Other option would be to choose Ubuntu from Windows Start menu, but I recommend using Windows Terminal.
Eventual internet connection issues
It may appear, that there’s no Internet connection from the installed Linux distribution. This can be checked easily by running:
ping siiportal.sii.pl
If the output is as follows:
ping: siiportal.sii.pl: Temporary failure in name resolution
It means that you have no internet connection from your Linux OS.
Solving the Internet connection problem
The problem is the file /etc/resolv.conf generated by Windows by default, always when Linux OS is started. To have Internet access automatically, every time, when Linux OS is started, the following steps must be executed:
- Add entry in /etc/wsl.conf in order to stop generating /etc/resolv.conf file every time, the Linux is started,
- Remove /etc/resolv.conf file as it is a symlink,
- Create /etc/resolv.conf file again with our own nameserver entry,
- Change the attribute of /etc/resolv.conf guaranteeing its immutability – that means this file can’t be edited or removed.
Stop automatically generating /etc/resolv.conf file
Open /etc/wsl.conf file with admin privileges, for example using nano:
sudo nano /etc/wsl.conf
And add the following entry to the file:
[network] generateResolvConf = false
Save changes.
Remove /etc/resolv.conf
In order to remove the mentioned file, execute command:
sudo rm /etc/resolv.conf
Create /etc/resolv.conf again
Create and open the /etc/resolv.conf file, for example with nano:
sudo nano /etc/resolv.conf
And add your own DNS server address, for example the Google’s one:
nameserver 8.8.8.8
Make /etc/resolv.conf file immutable
Execute the following command (it would fail if /etc/resolv.conf was a symlink):
sudo chattr +i /etc/resolv.conf
Verify solution
Now you can close the tab with Linux. You can wait ~ 30 seconds until Linux OS will be stopped automatically by WSL, or make it stop manually by executing:
wsl --shutdown
Now check if WSL is stopped:
wsl -l -v
The output should be similar to the one below:
NAME STATE VERSION * Ubuntu-22.04 Stopped 2
The most important is the Stopped state.
Now you can open Linux terminal tab again and check with the ping command if you have a connection to the Internet. If the output is similar to the one below, everything works fine:
PING siiportal.sii.pl (91.227.21.84) 56(84) bytes of data. 64 bytes from static-91-227-21-84.devs.futuro.pl (91.227.21.84): icmp_seq=1 ttl=55 time=6.12 ms 64 bytes from static-91-227-21-84.devs.futuro.pl (91.227.21.84): icmp_seq=2 ttl=55 time=6.17 ms (…)
Update Linux distribution
The final step is to update the Linux distribution. You need to remember, that you are responsible for updates and installed software in particular Linux distributions. WSL doesn’t update any Linux distro, nor does it automatically Windows updates.
Execute the following commands in order to update package information and update the OS:
sudo apt update && sudo apt dist-upgrade -y
Conclusion
In this article I’ve described briefly what a WSL is, what are the potential advantages of using this tool and how to install it.
I strongly recommend getting familiar with the official documentation available here.
In next article I’ll describe how to configure developer tools to work with installed Linux distribution (Visual Studio Code, IntelliJ IDEA), as well as how to install and use Docker from WSL.
Sources
- What is Windows Terminal?
- Set up your Linux username and password
- Enable Virtual Machine feature
- Download the Linux kernel update package
- Windows Subsystem for Linux Documentation
***
Other articles in the series can be found here:
Leave a comment