wsl安装tensorflow(with cuda12.4)
下载并打开wsl
参考windows下使用wsl设置启动wsl并安装ubuntu版本分发
打开wsl
wsl -d ubuntu
更新软件包
sudo apt-get update
sudo apt-get upgrade -y
安装配置cuda 12.4
- 下载安装cuda 12.4
根据cuda toolkit 12.4nvidia官方安装参考
按顺序输入以下命令即可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/12.4.1/local_installers/cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-4
rm cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
- 配置cuda环境变量
- 打开用户配置文件
vim ~/.bashrc
- 点击键盘
i
键进入编辑模式 - 将光标移动到最下面添加如下内容
export cuda_home=/usr/local/cuda-12.4 export path=$cuda_home/bin:$path export ld_library_path=$cuda_home/lib64:$ld_library_path
- 刷新用户配置
source ~/.bashrc
- 打开用户配置文件
安装cudnn
cudnn的安装即把必要的文件移动到cuda的对应目录下即可
- 下载cudnn8.9.7for cuda 12.x
文件为cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz
- 将下载到的文件移动到wsl内
- 打开文件资源管理器的linux,找到下载的发行版并进入用户目录<version>/home/<username>/<folders>/
- 在wsl中进入对应文件夹
- 输入以下命令解压
tar -xvf cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz
- 进入cudnn文件夹
cd cudnn-linux-x86_64-8.9.7.29_cuda12-archive
- 移动必要文件
sudo mv include/* /usr/local/cuda-12.4/include/
sudo mv lib/* /usr/local/cuda-12.4/lib64
- 刷新用户配置
source ~/.bashrc
- 删除不必要文件
cd .. && rm -rf cudnn-linux-x86_64-8.9.7.29_cuda12-archive
下载并安装anaconda3
-
下载anaconda3
在控制台输入即可将anacodna3安装包下载到本地
wget https://repo.anaconda.com/archive/anaconda3-2024.02-1-linux-x86_64.sh
-
安装anaconda3
-
赋予anaconda3安装文件执行权限
chmod +x anaconda3-2024.02-1-linux-x86_64.sh
-
执行安装文件
./anaconda3-2024.02-1-linux-x86_64.sh
-
开始安装
welcome to anaconda3 2024.02-1 in order to continue the installation process, please review the license agreement. please, press enter to continue >>>
显示以上内容,按回车键继续
-
阅读并同意协议
按住空格可快速过掉用户协议,待到需要输入内容时
do you accept the license terms? [yes|no] >>>
输入
yes
表示同意协议并继续安装 -
选择安装位置
anaconda3 will now be installed into this location: /home/<yourname>/anaconda3 - press enter to confirm the location - press ctrl-c to abort the installation - or specify a different location below [/home/<yourname>/anaconda3] >>>
在后面输入安装位置全路径可更改安装位置,直接回车下一步表示使用默认位置(当前用户的主目录)
-
设置环境变量
installation finished. do you wish to update your shell profile to automatically initialize conda? this will activate conda on startup and change the command prompt when activated. if you'd prefer that conda's base environment not be activated on startup, run the following command when conda is activated: conda config --set auto_activate_base false you can undo this by running `conda init --reverse $shell`? [yes|no] [no] >>>
当看到此提示即表示anaconda3已经安装完成,输入
yes
并回车可自动修改用户配置文件,将anaconda3自动添加到环境变量如果没有在此输入yes,则需要修改配置文件
.bashrc
- 打开用户配置文件
vim ~/.bashrc
- 点击键盘
i
键进入编辑模式 - 将光标移动到最下面添加以下内容(将
/home/ubuntu/anaconda3
替换为自己的实际安装路径)# >>> conda initialize >>> # !! contents within this block are managed by 'conda init' !! __conda_setup="$('/home/ubuntu/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then . "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" else export path="/home/ubuntu/anaconda3/bin:$path" fi fi unset __conda_setup # <<< conda initialize <<<
- 打开用户配置文件
-
刷新用户配置
source ~/.bashrc
-
删除anacodna安装文件
rm anaconda3-2024.02-1-linux-x86_64.sh
-
下载tensorflow
- 创建虚拟环境并进入
conda create -n tensorflow python -y && conda activate tensorflow
- 下载tensorflow(如果速度很不理想可以在后面加上
-i https://pypi.tuna.tsinghua.edu.cn/simple
使用清华大学镜像源)pip install tensorflow[and-cuda]
验证安装
cuda 安装
执行完安装后执行nvcc -v
即可查看cuda 版本,正常显示版本信息则安装成功
nvcc -v
nvcc: nvidia (r) cuda compiler driver
copyright (c) 2005-2024 nvidia corporation
built on thu_mar_28_02:18:24_pdt_2024
cuda compilation tools, release 12.4, v12.4.131
build cuda_12.4.r12.4/compiler.34097967_0
anaconda3安装
执行完成安装后输入conda --version
查看conda版本信息,正常显示conda版本信息则安装成功
conda --version
conda 24.1.2
tensorflow安装
执行完成安装后输入python
进入python控制台
python
在其中输入import tensorflow
并显示tensorflow版本tensorflow.__version__
import tensorflow
tensorflow.__version__
如果可以正常显示版本信息则安装成功
'2.16.1'
tensorrflow是支持cuda
如果tensorflow安装成功,则继续执行
tensorflow.config.list_physical_devices("gpu")
如果能有如下输出则安装成功
[physicaldevice(name='/physical_device:gpu:0', device_type='gpu')]
发表评论