Creating a Virtual Environment with WSL2 and Linux on Windows 11
In this article we’re going to install Windows Subsystem for Linux (WSL) and Linux Ubuntu on Windows 11, then we’re going to create a Virtual Environment.
Windows Subsystem for Linux (WSL) allows Windows users to create a complete Linux Terminal environment without the necessity of a traditional virtual machine or a dual-boot setup, in this way you can develop cross-platform applications without leaving Windows.
Table of contents
- Installing WSL2
- Download Ubuntu
- Configure Ubuntu
- Creating a Virtual Environment
1. Installing WSL2
The first thing we have to do is installing WSL2.
There are two ways to do this: using the command line or from the Microsoft Store
Using the command line
Open a command prompt as an Administrator and run the following line of code:
wsl --install
From the Microsoft Store
Open the Microsoft Store and search for Windows Subsystem for Linux
2. Download Ubuntu
The next thing to do is to download Ubuntu. As in the previous step, we can do this either by using the Microsoft Store or by using the command line.
Using the command line
Run in a command prompt this code:
wsl --list --online
In this way you’ll see all the distros available:
You can then install the distro you want to use. In our case we’ll install Ubuntu 20.04 so you have to type:
wsl --install -d Ubuntu-20.04
And it will start the download and installation of Linux Ubuntu on the WSL.
If you want to check which distros are installed in the WSL and what version of WSL they are running on you can check by typing:
wsl -l -v
From the Microsoft Store
You can do the same process by installing Ubuntu 20.04 directly from the Microsoft Store.
Search for “Ubuntu”:
You can see there are different versions of Ubuntu.
- Ubuntu without a version number will install you the latest LTS version
- Ubuntu with a version number will install that specific version of Ubuntu
- Ubuntu (Preview) will install you a daily built of the latest development version of Ubuntu.
Choose the one you prefer and install it.
3. Configure Ubuntu
You can now open the Ubuntu app, the first time it will ask to set a username and a password
Then, it’s good practice to install the latest updates by typing in the terminal:
sudo apt update
and then:
sudo apt full-upgrade
You have now installed and updated Ubuntu on your WSL.
4. Creating a Virtual Environment
The last step is to create a Virtual Environment on Ubuntu to start our data science project.
There are many options (Anaconda, etc.) but we’re using the default module of Python: venv.
To create the new python environment type this code in the Ubuntu terminal:
python3 -m venv /path/to/new/virtual/environment
substitute /path/to/new/virtual/environment with the path where you want to create the environment and the name of the environment.
To activate the environment type:
source <venv>/bin/activate
You can now create your project!