Complete Python Development Environment With Docker And Linux GUI Apps in WSL2 for Windows 10
Tags: linux, wsl2, docker, python
Published: 2022-09-04 14:12:17

So lets create a development environment based on Linux inside of Microsoft Windows 10. We will be using WSL2 (Windows Subsystem for Linux) version 2.

As Linus Torvalds once said "It is best to develop on the platform you are deploying to." Since our applications will be deployed on a Linux server it would be the best option to develop on a GNU/Linux operating system.

Yet why would we want to have Linux running inside of a Windows operating system. Well unfortunately, Windows has a number of benefits as a Desktop operating system over Linux. Although I use a native setup of Linux on a daily basis, Windows has the following advantages:

1. Dynamically expanding swap
2. Better battery life on laptops
3. Commercial Software support

So let's break this down to understand what all of this means and how it is beneficial.

## Dynamically expanding swap
Swap is a file or partition on your SSD/HDD that is used to swap memory from RAM to disk when your RAM is approaching full capacity. This allows for applications to keep running even if you use more memory than you have in physical RAM.
Microsoft Windows and macOS both offer dynamically expanding swap. Which has no fixed size so it can grow or shrink as needed.
Linux and Unix based operating systems use a fixed swap allocation. This space cannot be dynamically resized, it is a fixed space and cannot grow or shrink according to your needs. The problem with this is that if you run out of swap space your OS will become extremely sluggish.

## Better battery life on laptops
It has been proven that battery life is just better on Windows due to the optimization made by Microsoft and hardware vendors on Windows. Although this is being improved and is getting better on Linux, Windows still excels in this area.

## Commercial Software support
As we all know most commercial software is more available to Windows and macOS operating systems. Specifically with media based software such as video editing, audio production and gaming. Yet this is begin improved on Linux, it still does not offer some industry leading software that are used by professionals.

---

## Install WSL2 on Windows 10

**Step 1.** Run PowerShell as administrator

![wsl-step-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilwkveknso6609qv1mwd.png)

**Step 2.** Enable WSL by entering the following command in PowerShell

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

![wsl-step-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz8x1xw0qc0tbz57977e.png)

**Step 3.** Enable Virtual Machine Platform by entering the following command in PowerShell _(You will be prompted to restart Windows, if not please do restart Windows after this step)_

```
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
```

![wsl-step-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdz48ja91aom5273szc7.png)

---

## Install WSL2 Kernel
**Step 1.** Download the WSL2 Kernal update from the following link: [WSL2 Kernal](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)

**Step 2.** Set WSL version 2 as your default WSL version by entering the following command in PowerShell
```
wsl --set-default-version 2
```

![wsl-step-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uwjw4d576p4pngl7k1n.png)

---

## Install a Linux Distro
**Step 1.** Open the Microsoft Store and search for `Linux`

**Step 2.** Pick a Linux distro of your choice to install for example `Ubuntu` and click `Get`


![Install Linux Distro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82ausfgx509mdt2yx4u0.png)

After the distro is installed it will open a window to configure your username and password.

---

## Install and Configure VcXsrv Windows X Server
This will be used to display all of our Linux GUI apps inside of Windows.

**Step 1.** Download and install VcXsrv. [VcXsrv Download Link](https://sourceforge.net/projects/vcxsrv/)

**Step 2.** Configure VcXsrv
- Run `XLaunch`
- Use all default settings and click next until you reach the `Extra settings` section where you will select all options

![vcxsrv default settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9unpa3dq8z3rw90773gc.png)

- Choose `Save configuration` and save it to the desktop so you can use it to start VcXsrv again which you will need to do every time you restart or logout of Windows


![vcxsrv save config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf27wtt9l2nv4pbwtmbl.png)

---

## Install GUI Apps
**Step 1.** Update your Ubuntu Linux distro by entering the following command in the Ubuntu terminal
```
sudo apt update
sudo apt upgrade
```
**Step 2.** Set the display server by entering the following command in the Ubuntu terminal
```
cat /etc/resolv.conf
```
This will show you your nameserver IP, replace the `xxx.xx.xx.x` with the IP retrieved from the above command and enter the following in your Ubuntu terminal
```
export DISPLAY=xxx.xx.xx.x:0.0
```
**Step 3.** Install and run Firefox
Lets see our gui apps in action by installing and running Firefox entering the following in your Ubuntu terminal
```
sudo apt install firefox
firefox
```
Congratulations, Firefox should be running now! This browser will come in handy for downloading anything we want to run inside of WSl2.

**Step 4.** Install and run PyCharm CE
- Download PyCharm by opening the following [link](- https://www.jetbrains.com/pycharm/download/) inside of the Firefox browser you have just opened from within Ubuntu (WSL2).
- Extract the archive which you should find in your home downloads folder inside your Ubuntu WSL terminal under `~/Downloads/` by entering the following command and replace `file.tar.gz` with the filename you have downloaded
```
tar xvzf file.tar.gz
```
- Run PyCharm from within the `Downloads` folder
```
cd pycharm-community/bin
./pycharm.sh
```

**Step 5.** Installing pip and miniconda
- We can install pip for Python3 using the following command
```
sudo apt install python3-pip
```
- Install miniconda by downloading the installation script from the following [link](https://docs.conda.io/en/latest/miniconda.html) and make sure you open this link from within the firefox browser that is running inside of WSL2
- Once downloaded you can start the installation by going to your Download directory in WSL2 and executing the script using the following command
```
bash ~/Downloads/Miniconda3-latest-Linux-x86_64.sh
```

**Step 6.** Install Docker Desktop for Windows
- Download Docker Desktop for Windows from the following [link](https://docs.docker.com/desktop/install/windows-install/)
- After installation you will be prompted to logout of Windows
- Once you login to Windows again and docker desktop for Windows is running, you can open Ubuntu WSL2 terminal and run the following commands to make sure that docker is running inside of WSL2:

```
docker -v
docker-compose -v
```
Congratulations you now have docker running in your WSL2 instance.

You can install more tools inside of WSL2 as you would with any Linux distro. This setup should get you going for Python development.