windows的wsl子系统安装Ubuntu20,并配置ros以及cuda

一. wsl与Ubuntu安装

在cmd界面输入

wsl --install

为了快一点可以输入

wsl --install --web-download

安装完后重启电脑,再直接去微软商店下载自己需要的发行版,我为了和同事的匹配,使用的是ubuntu20.04,直接点击免费获取-->打开即可;然后就是设置用户名和密码。

之后迁移到D盘或其他位置:

wsl --export Ubuntu-20.04 d:\wsl-ubuntu20.04.tar  #导出分发版为tar文件到d盘
wsl --unregister Ubuntu-20.04    #注销当前分发版
wsl --import Ubuntu-20.04 d:\wsl-ubuntu20.04 d:\wsl-ubuntu20.04.tar --version 2    #重新导入并安装WSL在d:\wsl-ubuntu20.04
ubuntu2004 config --default-user Username   # (Username为之前已经设置的名称)
del d:\wsl-ubuntu20.04.tar   #删除tar文件

主要参考从零开始安装Windows子系统wsl2-Ubuntu{超详细}

二. vscode安装wsl插件

再vscode的插件当中搜索wsl安装,可能要搭配Remote-SSH插件一起,就可以如同远程编译普通Ubuntu系统一样去修改编译子系统上的文件了;
且支持再子系统命令行中直接敲code .打开文件夹。

三. 安装ROS1

1.安装ROS Noetic

添加ROS安装源和密钥: 
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
安装ROS完整桌面版: 
sudo apt install ros-noetic-desktop-full -y
  1. 初始化rosdep

    安装并配置rosdep:

sudo apt install python3-rosdep python3-pip -y
sudo rosdep init
rosdep update
  1. 配置环境变量

    添加ROS到环境变量中:

echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
  1. 测试ROS安装

    启动核心服务:

roscore
启动Turtlesim仿真器: 
rosrun turtlesim turtlesim_node
打开新终端,运行控制程序: 
rosrun turtlesim turtle_teleop_key

使用方向键控制乌龟移动,验证图形界面是否正常显示。

四. 安装cuda相关

wsl安装的Ubuntu直接就默认有了nvidia驱动,可以直接执行nvidia-smi

Tue Nov 18 14:46:52 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.82.10              Driver Version: 581.29         CUDA Version: 13.0     |
+-----------------------------------------+------------------------+----------------------+
| 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 5060        On  |   00000000:01:00.0  On |                  N/A |
| 42%   55C    P1             70W /  145W |    4313MiB /   8151MiB |     62%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A              41      G   /Xwayland                             N/A      |
+-----------------------------------------------------------------------------------------+

cuda官网:CUDA Toolkit 13.0 Update 2 Downloads
参考网页WSL2安装CUDA,nvidia环境小白教程和踩坑记录

对应版本选择

官网提供的命令行:

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/13.0.2/local_installers/cuda-repo-wsl-ubuntu-13-0-local_13.0.2-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-13-0-local_13.0.2-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-13-0-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-13-0

似乎和微软提倡的使用docker容器略有差异;
安装完之后还要配置路径,防止nvcc命令找不到:

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-13/lib64" >> ~/.bashrc
echo "export PATH=$PATH:/usr/local/cuda-13/bin" >> ~/.bashrc
echo "export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-13" >> ~/.bashrc
echo "export PATH=/usr/local/cuda/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

然后执行nccc -V查看版本

nccc -V

得到输出

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Wed_Aug_20_01:58:59_PM_PDT_2025
Cuda compilation tools, release 13.0, V13.0.88
Build cuda_13.0.r13.0/compiler.36424714_0

cudnn

官网Index of /compute/cudnn/redist/cudnn/linux-x86_64
据说tensorrt10以后就不用依赖cudnn了,暂时搁置;

tensorrt

去官网下载对应的版本,如果前面是13.0就下13.0的,我这儿为了和同事兼容,把版本降下来了
https://developer.nvidia.com/tensorrt/download
https://developer.nvidia.com/tensorrt/download/10x
然后安装下载下来的包:

sudo dpkg -i nv-tensorrt-local-repo-ubuntu2004-10.11.0-cuda-12.9_1.0-1_amd64.deb
sudo cp /var/nv-tensorrt-local-repo-ubuntu2004-10.11.0-cuda-12.9/nv-tensorrt-local-99237616-keyring.gpg /usr/share/keyrings/

更新软件源并安装 TensorRT

sudo apt-get update
sudo apt-get install -y tensorrt

安装 Python 绑定(必需)

sudo apt-get install -y python3-libnvinfer

验证安装

# 检查 TensorRT 版本
dpkg -l | grep TensorRT  # 应显示 10.11.0

# 验证 Python 导入
python3 -c "import tensorrt; print(tensorrt.__version__)"  # 输出:一堆 10.11.0

配置路径,在~/.bashrc文件中添加以下行:

export PATH=/usr/src/tensorrt/bin:$PATH

然后在命令行激活更新

source ~/.bashrc

查看trtexec版本:

# 检查命令可用性
which trtexec

# 查看版本
trtexec --version
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容