DEV Community

Cover image for Complete Python Development Environment With Docker And Linux GUI Apps in WSL2 for Windows 10
Wael Ramadan
Wael Ramadan

Posted on

Complete Python Development Environment With Docker And Linux GUI Apps in WSL2 for Windows 10

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

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

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

wsl-step-2

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
Enter fullscreen mode Exit fullscreen mode

wsl-step-3


Install WSL2 Kernel

Step 1. Download the WSL2 Kernal update from the following link: WSL2 Kernal

Step 2. Set WSL version 2 as your default WSL version by entering the following command in PowerShell

wsl --set-default-version 2
Enter fullscreen mode Exit fullscreen mode

wsl-step-4


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

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

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

  • 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


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
Enter fullscreen mode Exit fullscreen mode

Step 2. Set the display server by entering the following command in the Ubuntu terminal

cat /etc/resolv.conf
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode
  • Run PyCharm from within the Downloads folder
cd pycharm-community/bin
./pycharm.sh
Enter fullscreen mode Exit fullscreen mode

Step 5. Installing pip and miniconda

  • We can install pip for Python3 using the following command
sudo apt install python3-pip
Enter fullscreen mode Exit fullscreen mode
  • Install miniconda by downloading the installation script from the following link 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
Enter fullscreen mode Exit fullscreen mode

Step 6. Install Docker Desktop for Windows

  • Download Docker Desktop for Windows from the following link
  • 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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
revoltez profile image
revoltez

a one sec google search gave me this : github.com/Tookmund/Swapspace/
and about battery life, it depends from Os + laptop to another due to drivers, so its not like its objectively true that windows has better battery life
the only thing i can think of when it comes to commercial app support is Adobe apps, i record videos a lot and i use OBS + Kdenlive and it works great.

and no matter what Microsoft offers, the tradeoff is too huge for me to use their shitty Os that will make a slave for them https://www.youtube.com/watch?v=Lv5xHfZnk4s&t

Collapse
 
wmramadan profile image
Wael Ramadan

As for the Swapspace repository, this is not an official built-in feature in the Linux Kernel hence it is probably not as vetted towards security attacks and probably not very stable for production use. Also if you noticed in the repository it states N.B. If you want suspend and resume to work you will still need a static swapfile! so it is also not feature complete. The Linux community of course offers workarounds/patches/alternatives to everything, yet this is not part of the core system.

As for battery life, if you look at most battery tests/benchmarks yes Windows does come out on top most of the time. This, as you have stated, depends on Vendor but the majority of tests will show that Windows will outlast Linux in battery life optimization.

In spite of all of this, you are free to use whatever OS you are comfortable with. Linux is definitely better than Windows in certain tasks and vice versa. I myself use Linux on a daily basis.