WSL+Systemd+Gnome+VcXsrv+CUDAToolkit 安装

2023-05-16

我的版本信息

wsl:

PS C:\Users\kirk> wsl --update
正在检查更新...
无更新使用。
内核版本: 5.10.102.1

Ubuntu:

kirk@KirkComputer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

安装Ubuntu

WSL安装建议参照参照官方文档,我就不介绍了,安装的是默认的Ubuntu,我看了下目前版本是Ubuntu-20.04和选择安装Ubuntu-20.04是一样的
安装 WSL | Microsoft Docshttps://docs.microsoft.com/zh-cn/windows/wsl/install

安装Systemd

网上的DamionGans/ubuntu-wsl2-systemd-script我这个版本好像已经不能用了,所以我参考的是github上的GitHub - nullpo-head/wsl-distrod: Distrod is a meta-distro for WSL 2 which installs Ubuntu, Arch, Debian, Gentoo, etc. with systemd in a minute for you. Distrod also has built-in auto-start feature on Windows startup and port forwarding ability.Distrod is a meta-distro for WSL 2 which installs Ubuntu, Arch, Debian, Gentoo, etc. with systemd in a minute for you. Distrod also has built-in auto-start feature on Windows startup and port forwarding ability. - GitHub - nullpo-head/wsl-distrod: Distrod is a meta-distro for WSL 2 which installs Ubuntu, Arch, Debian, Gentoo, etc. with systemd in a minute for you. Distrod also has built-in auto-start feature on Windows startup and port forwarding ability.https://github.com/nullpo-head/wsl-distrod

 下面是WSL 安装systemd的脚本,实测好像不用加 --start-on-windows-boot 跟随系统启动也不影响

#这个install.sh  网络问题,可以开vpn 下载来再拷过去 
#内容第五行:LATEST_RELEASE_URL="https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz"
#默认下载最新的包 进行安装
curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh"
chmod +x install.sh
sudo ./install.sh install
#安装成功 执行
sudo /opt/distrod/bin/distrod enable --start-on-windows-boot
#--------------------------显示内容-------------------------------------
# [Distrod] Distrod has been enabled. Now your shell will start under systemd.
# [Distrod] Enabling atuomatic startup of Distrod. UAC dialog will appear because scheduling
# a task requires the admin privilege. Please hit enter to proceed.
#--------------------------显示内容-------------------------------------
#这里提示你要回车,回车之后,会弹出管理员授权点同意,然后会让你输入密码(注:windows的用户密码)要创建任务计划程序,密码输错了,就继续重试 内容如下:
#--------------------------显示内容-------------------------------------
# Error
# It seems the task has not been scheduled successfully. You may have typed a wrong password, or you may not have the
# necessary administrative privileges. Do you want to retry?
# [Y] Yes  [N] No  [?] 帮助 (默认值为“Y”):
#--------------------------显示内容-------------------------------------
#如果密码输入正确,并且成功创建任务计划程序会显示下面内容:
#--------------------------显示内容-------------------------------------
# Enabling autostart has succeeded.
# [Distrod] Distrod will now start automatically on Windows startup.
#--------------------------显示内容-------------------------------------
#补充: 如果加  --start-on-windows-boot 执行失败的话 报错信息是 Caused by: C drive not found.
# 在容器外部为 Systemd 打开一个 Shell 会话 ,打开powershell 输入
wsl -d Ubuntu -e /bin/bash
# 再重复 sudo /opt/distrod/bin/distrod enable --start-on-windows-boot 操作
#到此就结束了安装成功
#查看版本
distrod --version
# distrod 0.1.4
#测试
systemctl status
# ● KirkComputer
#     State: running
#      Jobs: 0 queued
#    Failed: 0 units
#     Since: Mon 2022-05-09 23:15:56 +08; 22min ago
#    CGroup: /

 上述 install.sh 我就贴在这里省得翻墙,要最新的话还是参照上面地址去获取,内容如下:

#!/bin/sh

set -e
#这里默认获取的是最新的安装包,可以自己修改想要的版本
LATEST_RELEASE_URL="https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz"

HELP_STR="Usage: $0 <command>

command:
    - install
    - update
    - uninstall"

help () {
    echo "$HELP_STR"
}

error_help () {
    error "$HELP_STR"
}

install () {
    mkdir /opt/distrod || error "Failed to create /opt/distrod."
    cd /opt/distrod || error "Could not change directory to /opt/distrod"
    get_release_file
    tar xvf opt_distrod.tar.gz
    rm opt_distrod.tar.gz
    echo "Installation is complete!"
}

uninstall () {
    if grep -o systemd /proc/1/status > /dev/null; then
        error "This uninstall command cannot run inside a running Distrod distro."
        error "To uninstall it, do the following first."
        error "1. /opt/distrod/bin/distrod disable  # Stop systemd from starting as init"
        error "2. wsl.exe --shutdown  # Terminate WSL2"
        error "After that, Systemd will not run as the init and you can run uninstall."
        exit 1
    fi
    rm -rf /opt/distrod
    echo "Distrod has been uninstalled!"
}

update () {
    cd /opt/distrod || error "Could not change directory to /opt/distrod"
    get_release_file
    EXCLUDE=""
    for FILE in /opt/distrod/conf/*; do
        FILE=${FILE#/opt/distrod/}
        if printf "%s" "$FILE" | grep -E ' '; then
            error "Found a file with a name containing spaces. Please remove it. Aborting update command."
            exit 1
        fi
        EXCLUDE="$EXCLUDE --exclude $FILE"
    done
    # shellcheck disable=SC2086
    tar xvf opt_distrod.tar.gz $EXCLUDE
    echo "Ruuning post-update actions..."
    POST_UPDATE="/opt/distrod/misc/distrod-post-update"
    if [ -e "${POST_UPDATE}" ]; then
        "${POST_UPDATE}"
    fi
    echo "Distrod has been updated!"
}

get_release_file() {
    if [ -n "$RELEASE_FILE" ]; then
        cp "$RELEASE_FILE" opt_distrod.tar.gz
    else
        curl -L -O "${LATEST_RELEASE_URL}"
    fi
}

error () {
    echo "$@" >&2
}


if [ -z "$1" ]; then
    error_help
    exit 1
fi

if [ "$(whoami)" != "root" ]; then
    error "You must be root to run this script, please use sudo ./install.sh"
    exit 1
fi

COMMAND=
while [ -n "$1" ]; do
    case "$1" in
    -h|--help)
        echo "$HELP_STR"
        exit 0
        ;;
    install)
        COMMAND=install
        shift
        ;;
    uninstall)
        COMMAND=uninstall
        shift
        ;;
    update)
        COMMAND=update
        shift
        ;;
    -r|--release-file)
        RELEASE_FILE="$(realpath "$2")"
        shift 2
        ;;
    -*)
        error "Error: Unknown flag $1"
        exit 1
        ;;
    *) # preserve positional arguments
        error "Error: Unknown command $1"
        exit 1
        ;;
    esac
done

"$COMMAND"

安装Gnome

这个我参考了底部链接那个老哥的链接,目前只有root能用,普通用户没法使用,我后面再研究研究

1:windows 安装VcXsrv 这个自行安装,下载地址:

VcXsrv Windows X Server download | SourceForge.nethttps://sourceforge.net/projects/vcxsrv/2:WSL 安装 ubuntu-desktop gnome 修改 ~/.bashrc文件,我这里用的是root用户

#安装 gnome
sudo apt install ubuntu-desktop gnome
#gnome 图形界面配置
vi ~/.bashrc
export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
#刷新
source ~/.bashrc

3:windows 打开 VcXsrv
#参数基本默认,只需要改动三处:进入之后选择one large window;display number设为0;下一步,下一步,勾上disable access control,可以保存配置,也可以不保存
4:WSL 执行 sudo gnome-session

sudo gnome-session
#此时 VcXsrv中已经显示了ubuntu界面

到这里图形界面就安装成功了,中文包还有Gnome其他组件看个人需求自己加。 

安装CUDAToolkit 

 这个可安装可不安装,主要用来GPU加速,没有显卡就不管了,用不到GPU运算也不用管了

windows要安装CPU驱动和GPU驱动这个对应自己的笔记本版本号可以参考官方文档

WSL 中的 GPU 加速 ML 训练 | Microsoft Docshttps://docs.microsoft.com/zh-cn/windows/wsl/tutorials/gpu-compute
安装CUDAToolkit 可以参考NVIDIA官方文档 

CUDA on WSL :: CUDA Toolkit Documentation (nvidia.com)https://docs.nvidia.com/cuda/wsl-user-guide/index.html我看的是Option 2: Installation of Linux x86 CUDA Toolkit using Meta Package部分

#注意:windows上安装了驱动程序,所以ubuntu上只需要安装CUDA Toolkit 工具包就可以,不要在去安装CUDA(会在linux上去安装驱动)
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-ubuntu2004-11-7-local_11.7.0-515.43.04-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-7-local_11.7.0-515.43.04-1_amd64.deb
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-11-7
#安装torch
pip3 install pytorch-directml
#测试是否有GPU加速 打开python3
import torch
torch.cuda.is_available()
# Python 3.8.10 (default, Mar 15 2022, 12:22:08)
# [GCC 9.4.0] on linux
# Type "help", "copyright", "credits" or "license" for more information.
# >>> import torch
# >>> torch.cuda.is_available()
# True
# >>>
#到这里就显示安装成功了

参考链接有

(66条消息) Windows中WSL2 配置运行GNOME桌面版 Ubuntu_Hack电子的博客-CSDN博客

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WSL+Systemd+Gnome+VcXsrv+CUDAToolkit 安装 的相关文章

随机推荐