
文档:设置 WSL 开发环境 | Microsoft Learn

wsl.exe --update安装WSL相关组件文件位置(24.04)/etc/apt/sources.list.d/ubuntu.sources
注意不是
/etc/apt/sources.list,这是22.04
参考ubuntu | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
22.04
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
24.04
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
# Types: deb-src
# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# Suites: noble noble-updates noble-backports
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# Types: deb-src
# URIs: http://security.ubuntu.com/ubuntu/
# Suites: noble-security
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 预发布软件源,不建议启用
# Types: deb
# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# Suites: noble-proposed
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# # Types: deb-src
# # URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# # Suites: noble-proposed
# # Components: main restricted universe multiverse
# # Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
更新源
sudo apt-get update
sudo apt-get upgrade
对不同的显卡可以到官网下载最新驱动,必须选择GeForce game ready版本的,而不是studio!否则不支持wsl
只需要在windows安装驱动即可,不要在wsl的linux中安装任何驱动!
如果在wsl已经配置好环境后,在Windows更新驱动,会出现nvidia-smicommand not found的情况,只需要将/usr/lib/wsl/lib/nvidia-smi加入到环境变量PATH中。
wget从清华源下载sh安装脚本wget --user-agent=“Mozilla” https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2024.06-1-Linux-x86_64.sh
不加
--user-agent=“Mozilla”会报错403forbidden
参考链接Index of /anaconda/archive/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
根据提示安装即可:bash Anaconda3-2024.06-1-Linux-x86_64.sh

初始化anaconda选择yes
使用conda报错not found:设置环境变量
vim ~/.bashrc
在其中加入export PATH=$PATH:/home/aii/software/anaconda3/bin
更新环境变量:source .bashrc
验证:conda -V
换源:在用户目录下创建touch ~/.condarc加入以下内容
anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
使用nvidia-smi查看当前显卡驱动可以安装的最高cuda版本,比如这里最高可以安装的cuda-toolkit的版本是12.4

参考CUDA Toolkit Archive | NVIDIA Developer
选择对应版本,使用提供的命令安装即可,安装runfile(local)版本,其他两个会自动更新到最新版本的cuda,即使你的显卡驱动并不适配,我也不知道为啥
这里我安装的是11.8(环境需要)
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run

设置环境变量
export PATH=/usr/local/cuda-11.4/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.4/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda-11.4
vim ~/.bashrc
检查安装成功:nvcc -V
cuDNN Archive | NVIDIA Developer
下载对应的tar.xz文件,
解压tar -xf cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz
将cudnn中的文件放入cuda中:
sudo cp cudnn-linux-x86_64-8.9.7.29_cuda11-archive/include/* /usr/local/cuda-11.8/include
sudo cp cudnn-linux-x86_64-8.9.7.29_cuda11-archive/lib/libcudnn* /usr/local/cuda-11.8/lib64
分配权限
sudo chmod a+r /usr/local/cuda-11.8/include/cudnn.h
sudo chmod a+r /usr/local/cuda-11.8/lib64/libcudnn*
检查是否成功:cat /usr/local/cuda-11.8/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

先在conda中创建环境:conda create -n [name] python=[version]
激活环境:conda acticate [name]
选择对应版本,使用他提供的命令就行,但是最好是能科学上网,否则大概率失败
如:conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
在Ubuntu24.04安装低版本的GCC时,需要先添加旧版本的源
sudo cat <<EOF | sudo tee /etc/apt/sources.list.d/gcc-8.list
deb http://old-releases.ubuntu.com/ubuntu/ impish main restricted universe multiverse
EOF
sudo apt update
apt-cache policy g++-8
apt-cache show g++-8
sudo apt install g++-8 gcc-8
查看系统中存在的gcc版本:ls /usr/bin/gcc*
查看g++存在的版本:ls /usr/bin/g++*
使用 update-alternatives 工具创建多个 GCC 和 G++ 编译器替代方案的列表
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13
更换GCC:sudo update-alternatives --config gcc

更换g++:sudo update-alternatives --config g++

/etc/apt/sources.list添加以下内容:
# 10
deb http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main
deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main
添加key
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
更新源:sudo apt-get update
下载:sudo apt-get install clang-10
# for clang-10.0
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 1 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-10
# for clang-14.0
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 2 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-14
# for clang-18.0
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-18 3 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-18
切换:sudo update-alternatives --config clang

在~/.bashrc中添加
host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
export http_proxy="http://$host_ip:[端口]"
export https_proxy="http://$host_ip:[端口]"
代理管理:
创建一个脚本proxy_on_off.sh,参数:on、off
#!/bin/bash
# 获取当前的代理状态
proxy_status=$(grep -c "export http_proxy" ~/.bashrc)
# 检查是否有传递的参数
if [[ "$1" == "on" ]]; then
if [[ $proxy_status -eq 0 ]]; then
host_ip=$(cat /etc/resolv.conf | grep "nameserver" | cut -f 2 -d " ")
echo "export http_proxy=\"http://$host_ip:7890\"" >> ~/.bashrc
echo "export https_proxy=\"http://$host_ip:7890\"" >> ~/.bashrc
export http_proxy="http://$host_ip:7890"
export https_proxy="http://$host_ip:7890"
echo "代理已启用"
else
echo "代理已经启用"
fi
elif [[ "$1" == "off" ]]; then
if [[ $proxy_status -eq 1 ]]; then
sed -i '/export http_proxy/d' ~/.bashrc
sed -i '/export https_proxy/d' ~/.bashrc
unset http_proxy
unset https_proxy
echo "代理已禁用"
else
echo "代理已经禁用"
fi
# 彻底删除所有代理配置
unset ALL_PROXY HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY
unset all_proxy http_proxy https_proxy ftp_proxy no_proxy
else
echo "用法: $0 {on|off}"
fi
# 强制刷新当前终端
exec bash
WSL 2.2.1 版本以后默认启用了 DNS 隧道, 会导致该方法失效。在 C:\Users\<UserName>\.wslconfig 文件中 (如果不存在就手动创建一个) 加入以下内容以关闭 DNS 隧道:
[wsl2]
dnsTunneling=false
设置root用户也能使用代理:
sudo visudo
在其中把对应内容的注释取消

export http_proxy=“http://192.168.182.2:7890”
export https_proxy=“http://192.168.182.2:7890”
wsl中配置git代理:
git config --global http.proxy http://windowsip:7890
git config --global https.proxy http://windowsip:7890
验证代理配置:
git config --global --get http.proxy
git config --global --get https.proxy
取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
在.wslcong中开启 systemd 后,code .命令能打开 vscode,但是不能正常打开任何文件。
本以为是开启 systemd 后的 wsl 架构发生变化,没曾想,在我安装 ssh-server 服务后,又重新恢复正常了
问题解决步骤:
code .发现不正常sudo apt install openssh-servercode .命令竟然恢复了正常喜出望外、意外之喜啊。
原因分析:应该是原先的 sshd 守护进程服务并不在 systemd 下,是打开 systemd 后,系统架构发生变化导致 ssh 服务失效。重新下载 ssh-server 服务后,该服务由 systemd 接管。
打包输出到F盘wsl --export Debian F:\Debian.tar
在WSL中取消挂载Debian wsl --unregister Debian
重新挂载Debian wsl --import Debian F:\wsl2\Debian F:\Debian.tar