<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/feeds/atom-style.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>https://ydysd.top/en/</id>
    <title>CodeRiff</title>
    <updated>2026-04-17T02:16:16.200Z</updated>
    <generator>Astro-Theme-Retypeset with Feed for Node.js</generator>
    <author>
        <name>David Ye</name>
        <uri>https://ydysd.top/</uri>
    </author>
    <link rel="alternate" href="https://ydysd.top/en/"/>
    <link rel="self" href="https://ydysd.top/en/atom.xml"/>
    <subtitle>David Ye's personal tech blog, which may also include some life essays. Powered by Astro theme Retypeset and GitHub Pages.</subtitle>
    <rights>Copyright © 2026 David Ye</rights>
    <entry>
        <title type="html"><![CDATA[Best Practices for Configuring a Python and C/C++ Development Environment with WSL 2 and VS Code]]></title>
        <id>https://ydysd.top/en/posts/wsl2-vscode-python-cpp-dev-best-practices/</id>
        <link href="https://ydysd.top/en/posts/wsl2-vscode-python-cpp-dev-best-practices/"/>
        <updated>2025-07-04T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[This article outlines the steps for configuring a basic Python and C/C++ development environment with VS Code and WSL 2.]]></summary>
        <content type="html"><![CDATA[<p>:::note[Translated by AI]
This article was originally written manually in <strong>Simplified Chinese</strong> and translated into <strong>English</strong> by the GPT-5 model.
:::</p>
<blockquote>
<p>[!NOTE]
The setup in this post was done on Windows 11 Home 24H2 with an Ubuntu 24.04 LTS distro running under WSL 2. Your mileage may vary with different versions or distros.</p>
</blockquote>
<h2>Enable WSL 2 and install a Linux distribution</h2>
<p>If you haven't enabled or installed WSL at all, open PowerShell or CMD as Administrator and run <code>wsl --install</code>. Then restart your PC. This single command enables WSL, downloads and installs the latest WSL Linux kernel (WSL 2 by default — you can change it with <code>wsl --set-default-version 2</code>, though you likely won't need to), and installs the default <code>Ubuntu</code> distro (typically the latest Ubuntu LTS). The only thing you'll do manually is create a Unix user and set a password when prompted.</p>
<p>For example:</p>
<pre><code>Windows PowerShell
版权所有（C） Microsoft Corporation。保留所有权利。

安装最新的 PowerShell，了解新功能和改进！https://aka.ms/PSWindows

PS C:\WINDOWS\system32&gt; wsl --install
正在下载: Ubuntu
正在安装: Ubuntu
已成功安装分发。可以通过 “wsl.exe -d Ubuntu” 启动它
正在启动 Ubuntu...
wsl: 检测到 localhost 代理配置，但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: david
New password:
Retype new password:
passwd: password updated successfully
To run a command as administrator (user "root"), use "sudo &lt;command&gt;".
See "man sudo_root" for details.

david@ThinkBook:/mnt/c/WINDOWS/system32$ exit
exit
PS C:\WINDOWS\system32&gt; wsl -l -v
  NAME              STATE           VERSION
* Ubuntu            Stopped         2
</code></pre>
<p>Of course, you can also perform the steps that <code>wsl --install</code> automates: open Settings, go to Apps -&gt; Optional features -&gt; More Windows features, check Windows Subsystem for Linux and Virtual Machine Platform, click OK, and reboot.</p>
<p>Next, run <code>wsl -l -o</code> (<code>wsl --list --online</code>) to see all available distros and versions. Then install a specific distro with <code>wsl --install -d &lt;Name&gt;</code> (in newer WSL builds you can omit <code>-d</code> and just run <code>wsl --install &lt;Name&gt;</code>). For example: <code>wsl --install -d Ubuntu-24.04</code>.</p>
<pre><code>PS C:\WINDOWS\system32&gt; wsl --list --online
以下是可安装的有效分发的列表。
使用“wsl.exe --install &lt;Distro&gt;”安装。

NAME                            FRIENDLY NAME
AlmaLinux-8                     AlmaLinux OS 8
AlmaLinux-9                     AlmaLinux OS 9
AlmaLinux-Kitten-10             AlmaLinux OS Kitten 10
AlmaLinux-10                    AlmaLinux OS 10
Debian                          Debian GNU/Linux
FedoraLinux-42                  Fedora Linux 42
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6
SUSE-Linux-Enterprise-15-SP7    SUSE Linux Enterprise 15 SP7
Ubuntu                          Ubuntu
Ubuntu-24.04                    Ubuntu 24.04 LTS
archlinux                       Arch Linux
kali-linux                      Kali Linux Rolling
openSUSE-Tumbleweed             openSUSE Tumbleweed
openSUSE-Leap-15.6              openSUSE Leap 15.6
Ubuntu-18.04                    Ubuntu 18.04 LTS
Ubuntu-20.04                    Ubuntu 20.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_7                 Oracle Linux 8.7
OracleLinux_9_1                 Oracle Linux 9.1
</code></pre>
<h2>Manage installed WSL distros</h2>
<p>Use <code>wsl -l -v</code> (<code>wsl --list --verbose</code>) to view all installed WSL distros and their status. Start a specific distro with <code>wsl -d &lt;Name&gt;</code>.</p>
<p>Here are some common commands.</p>
<h3>List all distros and status</h3>
<pre><code>PS C:\WINDOWS\system32&gt; wsl -l -v
  NAME              STATE           VERSION
* Ubuntu-24.04      Running         2
  docker-desktop    Stopped         2
</code></pre>
<p>The item with an asterisk <code>*</code> is the default distro. You can change it with <code>wsl --set-default &lt;Name&gt;</code>. If you don't pass <code>-d</code> (<code>--distribution</code>), WSL starts the default distro.</p>
<h3>Launch a distro</h3>
<pre><code>PS C:\WINDOWS\system32&gt; wsl ~ -d Ubuntu-24.04
&lt;YourUsername&gt;@&lt;YourMachineName&gt;:~$
</code></pre>
<p>If you don't specify the initial directory <code>~</code>, you'll land in the Windows terminal's current directory, e.g., <code>/mnt/c/Users/david</code>.</p>
<h3>Update WSL</h3>
<pre><code>PS C:\WINDOWS\system32&gt; wsl --update
正在检查更新。
已安装最新版本的适用于 Linux 的 Windows 子系统。
</code></pre>
<h3>Stop or exit a distro</h3>
<p>To stop a specific distro, use <code>wsl --terminate &lt;Name&gt;</code>. From inside the distro, you can exit with <code>logout</code>, <code>exit</code>, or <code>Ctrl + D</code>. To shut down all WSL instances, use <code>wsl --shutdown</code>.</p>
<h2>Some other miscellaneous settings</h2>
<h3>Move a distro off the system drive</h3>
<p>The <code>wsl --install</code> command doesn't let you choose an install path; it installs the distro under your profile on drive C. If C: space is tight, you can migrate the distro to another drive. Here's how to move <code>Ubuntu-24.04</code> to D: as an example:</p>
<ol>
<li>Create target folders on D:: <code>mkdir D:\WSL</code>, <code>mkdir D:\WSL\Ubuntu-24.04</code>.</li>
<li>Export the installed distro: <code>wsl --export Ubuntu-24.04 D:\WSL\ubuntu2404.tar</code>.</li>
<li>Unregister (remove) the old distro on C:: <code>wsl --unregister Ubuntu-24.04</code>. This deletes the original distro and all its data on C:, so make sure the export in the previous step completed successfully.</li>
<li>Import the distro to D:: <code>wsl --import Ubuntu-24.04 D:\WSL\Ubuntu-24.04 D:\WSL\ubuntu2404.tar --version 2</code>.</li>
<li>After a successful import, delete the backup file: <code>del D:\WSL\ubuntu2404.tar</code>.</li>
</ol>
<h3>Configure open-source mirror sites</h3>
<p>If you want to use a proxy like Clash in WSL 2, see <a href="https://blog.east.monster/2022/10/05/clash-config-in-wsl/">this post</a>.</p>
<p>For package sources, it's recommended to use mirror sites hosted in mainland China for better speed (if you're there), for example the <a href="https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/">Tsinghua University Open Source Mirror</a>.</p>
<h2>Develop on WSL 2 with VS Code</h2>
<p>I won't cover installing and configuring VS Code itself here. Just install the WSL extension. Press <code>Ctrl + Alt + O</code> or <code>F1</code> and type "WSL", then choose <code>New WSL Window using Distro...</code> to connect to a specific distro (<code>New WSL Window</code> launches the default distro).</p>
<h3>Set up the Python environment</h3>
<ol>
<li>In the VS Code window connected to WSL, install the Python extension in both the local and the remote (WSL) environments.</li>
<li>Install Miniconda inside your WSL 2 Ubuntu: <code>wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh</code>, then run the script <code>bash Miniconda3-latest-Linux-x86_64.sh</code>. After installation, restart the terminal, and you should see <code>(base)</code> in your prompt.</li>
</ol>
<p>During the installer, you'll likely be asked: <code>Do you wish the installer to initialize Miniconda3 by running 'conda init'?</code> I recommend <code>yes</code>. The script will update your shell rc file (usually <code>~/.bashrc</code>) so the Conda base environment activates automatically in new terminals.</p>
<p>You can also keep using the system Python 3.12 from Ubuntu 24.04 for OS/system tools while using Miniconda for your own development to avoid conflicts:</p>
<p>Ubuntu 24.04 ships Python 3.12. You can install or verify it with the commands below. This version is tested by Canonical and works well with other system components.</p>
<ol>
<li>Update the package list: <code>sudo apt update</code>.</li>
<li>Install Python 3.12: <code>sudo apt install python3.12</code>.</li>
<li>Verify the version: <code>python3.12 --version</code> — you should see Python 3.12.3.</li>
</ol>
<p>Note the version here is 3.12.3. On an LTS system, you don't need the absolute latest — the Ubuntu-maintained packages are fine.</p>
<pre><code>ysd1123@YSD-ThinkBook:~$ python3.12 -V
Python 3.12.3
ysd1123@YSD-ThinkBook:~$ conda activate
(base) ysd1123@YSD-ThinkBook:~$ python -V
Python 3.13.5
</code></pre>
<h3>Set up a deep learning environment</h3>
<p>Use <code>nvidia-smi</code> or <code>nvcc</code> to check whether your NVIDIA GPU is visible inside WSL. You may need the CUDA Toolkit for the latter (<code>sudo apt install nvidia-cuda-toolkit</code>).</p>
<pre><code>(base) ysd1123@YSD-ThinkBook:~$ nvidia-smi
Wed Jul  2 23:09:16 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 575.51.02              Driver Version: 576.02         CUDA Version: 12.9     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 4060 ...    On  |   00000000:01:00.0  On |                  N/A |
| N/A   34C    P8              2W /  115W |     265MiB /   8188MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+
(base) ysd1123@YSD-ThinkBook:~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Fri_Jan__6_16:45:21_PST_2023
Cuda compilation tools, release 12.0, V12.0.140
Build cuda_12.0.r12.0/compiler.32267302_0
</code></pre>
<h3>Set up the C/C++ environment</h3>
<p>I mostly use C/C++ for algorithm exercises at the moment, so this section doesn't go into large-scale project setup.</p>
<h4>Install the C/C++ toolchain on Ubuntu</h4>
<p>First, install the core build tools inside your WSL 2 Ubuntu. <code>build-essential</code> is a meta package that includes gcc/g++, make, etc. <code>gdb</code> is the debugger.</p>
<p>In the VS Code integrated terminal, run <code>sudo apt update &amp;&amp; sudo apt install build-essential gdb -y</code> to update packages and install the GNU toolchain and GDB.</p>
<p>After installation, verify:</p>
<pre><code>(base) &lt;YourUsername&gt;@&lt;YourMachineName&gt;:~$ g++ --version
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

(base) &lt;YourUsername&gt;@&lt;YourMachineName&gt;:~$ gdb --version
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
</code></pre>
<p>Then install the C/C++ extension in VS Code (in the WSL environment).</p>
<h4>Create a workspace and configure build &amp; debug</h4>
<p>Create a directory under your home, e.g., <code>/projects/acm_practise</code>. Then <code>cd ~/projects/helloworld</code> and run <code>code .</code> to open a new VS Code window in that folder.</p>
<p>Create a new C++ file <code>main.cpp</code>, paste the following and save:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
   vector&lt;string&gt; msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

   for (const string&amp; word : msg)
   {
      cout &lt;&lt; word &lt;&lt; " ";
   }
   cout &lt;&lt; endl;
}
</code></pre>
<p>Click the Run button in the top right of VS Code, choose <code>Run C/C++ File</code>, then select <code>g++ build and debug active file</code> from the detected compilers. The first time you run a C++ file, VS Code will prompt you to select a compiler (it will be saved to <code>tasks.json</code>).</p>
<p>Alternatively, press <code>Ctrl + Shift + D</code> and create a <code>launch.json</code> under Run and Debug. If your workspace already has one, VS Code will use it to run/debug your code when you hit the Run button; otherwise it creates a temporary config. (Details: https://code.visualstudio.com/docs/cpp/config-wsl)</p>
<p>I also recommend the <a href="https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner">Code Runner</a> extension. Configure it in your workspace settings (avoid changing global settings) via <code>.vscode/settings.json</code> like this:</p>
<pre><code>{
  "code-runner.runInTerminal": true,
  "code-runner.clearPreviousOutput": true,
  "code-runner.fileDirectoryAsCwd": true,
  "code-runner.executorMap": {
    "c": "gcc $fileName -o $fileNameWithoutExt &amp;&amp; $dir$fileNameWithoutExt",
    "cpp": "g++ $fileName -o build/$fileNameWithoutExt &amp;&amp; $dir/build/$fileNameWithoutExt"
  },
  "code-runner.preserveFocus": false,
  "code-runner.saveFileBeforeRun": true
}
</code></pre>
<p>I won't expand on what each setting does here.</p>
<p>Finally, with <code>Ctrl + Alt + N</code> you can quickly build and run the file open in the editor. If you need to debug, use the Run button in the top right.</p>
<hr />
<h2>References</h2>
<ol>
<li><a href="https://code.visualstudio.com/docs/remote/wsl">code.visualstudio.com, Developing in WSL.</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl">marketplace.visualstudio.com, Visual Studio Code WSL</a></li>
<li><a href="https://code.visualstudio.com/blogs/2019/09/03/wsl2">code.visualstudio.com, WSL 2 with Visual Studio Code</a></li>
<li><a href="https://learn.microsoft.com/en-us/windows/wsl/basic-commands">learn.microsoft.com, Basic commands for WSL</a></li>
<li><a href="https://code.visualstudio.com/docs/cpp/config-wsl">code.visualstudio.com, Using C++ and WSL in VS Code</a></li>
<li><a href="https://prinsss.github.io/vscode-c-cpp-configuration-for-acm-oj/">prinsss.github.io, 使用 VS Code 搭建适用于 ACM 练习的 C/C++ 开发环境</a></li>
</ol>
]]></content>
        <author>
            <name>David Ye</name>
            <uri>https://ydysd.top/</uri>
        </author>
        <published>2025-07-04T00:00:00.000Z</published>
    </entry>
</feed>