VS Code and WSL¶
35 min Foundations Needs: Windows 10 or 11, admin rights
Notebooks are for experiments. Once your code grows past a few cells you want a real editor. This tutorial sets up VS Code on Windows with Linux running underneath it.
Why this matters¶
Almost every machine learning system in production runs on Linux. If you learn on Windows paths and Windows commands, you relearn everything the first time you deploy something.
WSL solves this. It gives you a real Ubuntu inside Windows. You keep the Windows you are used to, your code runs on Linux, and the two share files.
On a Mac or Linux already?
You do not need WSL. Install VS Code, skip to Step 4, and use your normal terminal wherever this page says WSL terminal.
How the pieces fit¶
One thing to hold on to: the window is Windows, the code is Linux.
Step 1: Install VS Code¶
Download it from code.visualstudio.com and run the installer. Tick these three boxes when offered:
- Add "Open with Code" to the file context menu
- Add "Open with Code" to the directory context menu
- Add to PATH
The last one lets you type code . in any terminal to open the current folder.
You will use it constantly.
Step 2: Install WSL¶
Right click the Start button and choose Terminal (Admin), then run:
This turns on the Windows features WSL needs, downloads the Linux kernel, and installs Ubuntu. Restart when it asks.
After the restart, Ubuntu opens and asks for a username and password. They do
not have to match your Windows ones. Write the password down somewhere, because
you need it every time you run sudo and Linux does not show any characters
while you type it.
Check it worked:
You want to see Ubuntu with VERSION 2:
wsl --install fails or WSL version says 1
Virtualisation is probably turned off in your BIOS. Restart, enter BIOS
setup, and enable Intel VT-x or AMD-V. If WSL installed but shows version
1, run wsl --set-version Ubuntu 2 and wait. It can take a few minutes.
Step 3: Connect VS Code to Ubuntu¶
- Open VS Code and press Ctrl+Shift+X for Extensions.
- Search for WSL by Microsoft and install it.
- Press Ctrl+Shift+P and run WSL: Connect to WSL.
A new window opens. Look at the bottom left corner. It should say WSL: Ubuntu. That label is how you know which side you are on, and you should check it whenever something behaves oddly.
Open the terminal inside VS Code with Ctrl+J, or with Ctrl and the backtick key. The prompt now looks like this:
That is Linux. Everything you type there runs on Ubuntu.
Step 4: Set up Python on the Linux side¶
Update the system first:
Then install the basics:
Now make a virtual environment. A virtual environment is a private folder of packages for one project. Without it, two projects that need different versions of the same library fight each other, and the loser is whichever one you open second.
python3 -m venv ~/ml-env
source ~/ml-env/bin/activate
pip install numpy pandas matplotlib scikit-learn jupyter
Your prompt now starts with (ml-env). That tells you the environment is
active. It deactivates when you close the terminal, so run the source line
again each session.
You installed Anaconda already. Is this a duplicate?
No. Anaconda is on the Windows side, WSL is a separate machine, and neither can see the other's packages. Use Anaconda and its notebooks for exploration on Windows, and this environment for the code you run in Linux. If you prefer one place for everything, install Anaconda inside WSL instead and use it for both.
Step 5: Work in the right folder¶
This is the one WSL rule worth memorising.
Keep your projects in the Linux home folder. Files there live on the Linux
filesystem and are fast. Files under /mnt/c live on the Windows filesystem
and every read crosses a bridge, which makes Git and Python noticeably slow on
a large project.
That last command opens the folder in VS Code, still connected to Ubuntu. When
you do need something from Windows, it is under /mnt/c/Users/YourName/.
Finally, install two extensions in this WSL window: Python and Jupyter, both from Microsoft. Extensions install per side, so having them on Windows is not enough.
Shortcuts worth knowing¶
| Keys | What it does |
|---|---|
| Ctrl+Shift+P | Command palette. Everything VS Code can do is in here |
| Ctrl+J | Show or hide the terminal |
| Ctrl+Shift+E | File explorer |
| Ctrl+B | Show or hide the sidebar |
| Ctrl+P | Jump to a file by typing part of its name |
Check yourself¶
- Open VS Code and confirm the bottom left corner says WSL: Ubuntu.
- In the terminal, run
pwd. You should see/home/yourname/ml-projects, not aC:path. - Create
hello.pywithprint("Linux is running my code")and run it withpython3 hello.py. - Activate
ml-envand runpython3 -c "import pandas; print(pandas.__version__)". - Run
git --versionto confirm Git is available on this side too.
If step 2 shows a /mnt/c path, you opened a Windows folder. Close the window
and start again from ~/ml-projects.
Next¶
You have an editor, a Linux environment, and somewhere to run code. Time to write some.
Official guide if you want more: VS Code with WSL.