---
# System prepended metadata

title: Cuda建置環境

---


## Cuda on WSL2
### [WSL2](https://learn.microsoft.com/zh-tw/windows/wsl/install)
1. :::spoiler Windows啟用WSL 
    控制台-->程式和功能-->開啟或關閉Windows功能-->點選"Hyper-V"、"適用於 Linux 的 Windows 子系統"、"虛擬機平台"
    ![](https://hackmd.io/_uploads/BkcVO8YMT.png)
    ![](https://hackmd.io/_uploads/HJUwdUFM6.png)
    ![](https://hackmd.io/_uploads/SygT_Utfa.png)
    ![](https://hackmd.io/_uploads/Sy9bc8tfT.png)
    * 重新啟動電腦
    :::
2. Open cmd: `wsl --install`, add `-d` option to choose OS version, ex: ubuntu-22.04
3. `wsl --update`
4. `wsl` to enter ubuntu environment.
5. `sudo apt update -y; sudo apt upgrade -y`
    * If it is not successful:
        1. Ign, `sudo vim /etc/resolv.conf`, comment all, and add `nameserver 8.8.8.8`
        2. close the firewall or 防毒軟體.
6. If you want to change wsl path, please check this [link](https://viml.nchc.org.tw/archive_blog_836/), [link2](https://superuser.com/questions/1506304/setting-default-user-in-linux-wsl-in-sideloaded-distro).
### Install Cuda
1. Only use this [driver](https://www.nvidia.com/en-us/drivers/), do not install any other driver in wsl2.
    * you can use `nvidia-smi` on cmd.
    * Only wsl2 has the path `/usr/lib/wsl/lib/nvidia-smi`
        * `sudo cp /usr/lib/wsl/lib/nvidia-smi /usr/bin/nvidia-smi`
        * `sudo chmod ogu+x /usr/bin/nvidia-smi`
        * test: `cd`, `nvidia-smi`
2. `sudo apt-key del 7fa2af80`
3. Follow these [commands](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0) to install cuda 12.6 toolkit.
### Environment-modules
1. `sudo apt install environment-modules` and restart ubuntu environment.
2. module (每次登入要重新load，可以`vim .bashrc`寫入指令)
    * `module list`：現在使用的module
    * `module avail`：可以使用的module
    * `module load <mymodule>`：載入module
    * `module unload <mymodule>`：取消載入module
3. create cuda modulefile
```
cd /usr/share/modules/modulefiles
mkdir cuda
cd cuda
sudo vim 12.6 #the cuda version you installed
```
4. Add in File 12.6, use `:wq` to quit.
```
#%Module1.0

proc ModulesHelp { } {
    global version
    puts stderr "\tSets up environment for CUDA $version\n"
}

module-whatis "sets up environment for CUDA 12.6"

set version 12.6
set root /usr/local/cuda-12.6
setenv CUDA_HOME	$root

prepend-path PATH $root/bin
prepend-path LD_LIBRARY_PATH $root/lib64
```
5. test using module: `module load cuda/12.6`, `nvcc -h`, `nvcc -V`.
6. If we need different version of cuda in the competition, check [this](https://gist.github.com/garg-aayush/156ec6ddda3d62e2c0ddad00b7e66956).
### deviceQuery.cpp
```
git clone https://github.com/NVIDIA/cuda-samples.git
cd cuda-samples/Samples/1_Utilities/deviceQuery
make
./deviceQuery
```
## Cuda on Windows
1. Install Microsoft Visual Studio 2019(或者偏好的其他版本：2017、2019、2022)(紫色的)
2. Install [Cuda toolkit 12.6](https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=11&target_type=exe_local)(或者偏好的其他版本), 按照指示安裝，會跟著安裝cuda在VS2019的套件。
3. 安裝好後打開VS，介面被改變了，從介面建立cuda檔案。
## Cuda on colab
1. register and login [colab](https://colab.research.google.com/?hl=zh-tw).
2. 參考 [How To Run CUDA C/C++ on Jupyter notebook in Google Colaboratory](https://www.geeksforgeeks.org/how-to-run-cuda-c-c-on-jupyter-notebook-in-google-colaboratory/)
    * 記得改版本，Step4指令不要照抄，看上面的 [commands](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0)

##  References
* [wsl2 installation guide](https://learn.microsoft.com/zh-tw/windows/wsl/install)
* [CUDA on WSL 2](https://docs.nvidia.com/cuda/wsl-user-guide/index.html#getting-started-with-cuda-on-wsl-2)
* [wsl2 Ubuntu22.04 sudo apt update 一直无法连接 出现 ign](https://link.csdn.net/?target=https%3A%2F%2Fgitcode.com%3Futm_source%3Dcsdn_toolbar)
* [Cuda-installation-on-WSL2-Ubuntu-20.04-and-Windows11](https://github.com/ashishpatel26/Cuda-installation-on-WSL2-Ubuntu-20.04-and-Windows11)
* [how-to-remove-cuda-completely-from-ubuntu](https://stackoverflow.com/questions/56431461/how-to-remove-cuda-completely-from-ubuntu)
* [wsl 1 to 2](https://learn.microsoft.com/zh-tw/windows/wsl/basic-commands#set-wsl-version-to-1-or-2)
* [Install *Module* for Different CUDA Environment (On Linux)](https://hackmd.io/@kkennethwu/BkQSHF_42#III-Module-instructions)