安装conda
以miniconda为例,Linux下sh Miniconda3-latest-Linux-x86_64.sh -b
,然后~/miniconda3/bin/conda init
即可。windows下可以搜索下载安装。conda也是一模一样的操作。
conda换源
各系统都可以通过修改用户目录下的 .condarc 文件来使用 镜像源。Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes
生成该文件之后再修改。
显示有哪些源(全局)
conda config --show-sources
删除所有源
conda config --remove-key channels
删除某条源
conda config --remove channels http://
添加源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --set show_channel_urls yes
pip换源
1.临时换
pip install package -i https://pypi.tuna.tsinghua.edu.cn/simple
2.永久换
修改 ~/.pip/pip.conf (ubuntu)或者C:\Users\xxx\pip\pip.ini(windows), 内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
3.设置timeout超时时间
pip --default-timeout=1000 install package
4.本地安装库python setup.py install
时修改set up.py中的pip源
直接在setup.py的同目录放置一个setup.cfg:
[easy_install]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
5.pip国内的一些镜像
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
创建虚拟环境、添加package
# 取一个你喜欢的名字,以tf1为例
conda env list #查看当前已有的虚拟环境
conda create -n test python=3.8 #创建一个对应python版本的test虚拟环境
conda activate test #进入环境
conda list # 查看当前环境下已有的package
conda install # conda install用一个等号,pip install 用两个等号
conda deactivate # 退出环境
conda remove --name <env_name> <package_name># 卸载指定环境中的包
conda remove <package_name> # 卸载当前环境中的包
conda remove -n test --al # 删除环境
conda create --name B --clone A # 克隆环境
conda install python=3.9 # 当前conda虚拟环境python版本修改
让环境能在jupyternotebook中使用
使用下述方法或者直接在base环境下安装nb_conda插件
conda install nb_conda
conda activate your_env_name # [激活your_env_name这个虚拟环境,your_env_name 是之前创建的python环境名字]
conda install ipykernel # [安装ipykernel] 为了可以在jupyter notebook中使用
# jupyter kernelspec list # 查看kernel list
# python -m ipykernel install --name your_env_name # [将 your_env_name 添加至kernel list]
# python -m ipykernel install --user --name your_env_name --display-name your_env_name # 远程登录ubuntu用户使用
# jupyter kernelspec remove your_env_name # 在kernel list里删除你这个kernel
更多说明
cuda和torch对应版本查询:https://pytorch.org/get-started/previous-versions/
python -m ipykernel install --name test [不加–user代表修改系统的设置]
python -m ipykernel install --user --name=test [添加–user意思是修改自己用户目录下的设置]
ipython kernelspec install-self [被DEPRECATED,建议使用ipykernel install]
# 安装gpu版本的pytorch时,去官网查看对应自己电脑cuda版本的torch,可以conda安装也可以pip安装
conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=10.1
conda install pytorch==1.1.0 torchvision==0.3.0 cudatoolkit=9.0
# 用以下程序检测成功使用gpu-pytorch
import torch
a=torch.Tensor([1,2])
a=a.cuda()
a
cuda和cudnn安装
-
在命令行输入下面命令,查看version.txt的内容 注意cat后面有空格
cat /usr/local/cuda/version.txt
-
还有一种可以查看GPU占用情况,这种同时也可以查看当前GPU的版本情况
nvidia-smi
-
如果安装好cudatoolkit的话也可以用下面指令查看当前cuda版本
nvcc -V
-
查看cudnn版本
# 先进入 /usr 目录下 cd /usr # 在该目录下搜索 cudnn.h 或者 cudnn_version.h 文件 sudo find -name cudnn.h # 搜索结果如下 # ./include/cudnn.h # 然后执行如下命令 cat ./include/cudnn.h | grep CUDNN_MAJOR -A 2
-
linux下安装cuda和cudnn
cuda下载:https://developer.nvidia.com/cuda-downloads
cudnn下载:https://developer.nvidia.com/rdp/cudnn-download下载好之后,解压cudnn(tar -cvf)到随便路径,安装cuda(sudo sh .run,注意按空格取消显驱driver的安装,a symlink already exists at /usr/local/cuda我选了NO)。
安装好之后,vim ~/.bashrc
,添加以下几行后source ~/.bashrc即可
:export CUDA_HOME=/usr/local/cuda-11.7 export PATH=/usr/local/cuda-11.7/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64:$LD_LIBRARY_PATH export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/cudnn-linux-x86_64-8.9.7.29_cuda11-archive/lib
安装和卸载nvidia显卡驱动
卸载显卡驱动
sudo /usr/bin/nvidia-uninstall
在线安装
查看推荐的显卡驱动版本
ubuntu-drivers devices
进行推荐版本的安装
sudo apt-get install nvidia-XX你需要的版本号XX
重启
sudo shutdown -r now
查看已装的显卡驱动版本和在这个驱动下支持的最大CUDA版本()
nvidia-smi
本地安装
去官网下载需要的显卡驱动,以NVIDIA-Linux-x86_64-440.31.run为例。
下载后的.run文件拷贝至home目录下。
1.禁用 nouveau驱动
查看有没有输出,如果有信息输出,则需要禁掉
lsmod | grep nouveau
sudo gedit /etc/modprobe.d/blacklist.conf
在blacklist.conf的最后添加下面几行:
blacklist nouveau
options nouveau modeset=0
更新
sudo update-initramfs -u
lsmod | grep nouveau
再次查看有没有输出,如果没有任何信息输出,则说明ok
2.进入tty模式进行安装
Ctrl+Alt+F1进入文本模式
关闭图形界面
sudo service lightdm stop
给驱动run文件赋予执行权限
sudo chmod a+x NVIDIA-Linux-x86_64-440.31.run
安装
sudo sh NVIDIA-Linux-x86_64-440.31.run --no-opengl-files --no-x-check --no-nouveau-check
--no-opengl-files 只安装驱动文件,不安装OpenGL文件
。这个参数最重要,否则会出现登陆界面循环登陆
的现象。
–no-x-check 安装驱动时不检查X服务 --no-nouveau-check 安装驱动时不检查nouveau后面两个参数可不加
。
如果在装的过程中出现以下信息,请选择: The distribution-provided pre-install script
failed! Are you sure you want to continue? 选择 yes 继续。 Would you like
to register the kernel module souces with DKMS? This will allow DKMS
to automatically build a new module, if you install a different kernel
later? 选择 No 继续。 Nvidia’s 32-bit compatibility libraries? 选择 No 继续。
Would you like to run the nvidia-xconfigutility to automatically
update your x configuration so that the NVIDIA x driver will be used
when you restart x? Any pre-existing x confile will be backed up. 选择
no 继续
最后
sudo service lightdm restart
重新开启图形界面
nvidia-smi
查看是否安装成功
问题1
若是出现
/dev/sda5:clean, files, blocks
[ok] Started udev Kernel Device Manager等词,都是显卡版本不对所导致的。
问题2
进系统之后只有壁纸没有侧边栏和上边栏
rm -rf .gconf/apps/compiz*
rm -rf .cache/compizconfig-1/
rm -rf .config/compiz-1/
rm -rf .compiz/
sudo shutdown -r now
如果你是在修同一局域网下距离很远的服务器,可以使用SSH终端工具完成上述操作,这里我很推荐Mobaxterm这个客户端控制软件,加上vscode可以实现实时修改服务器上的项目代码。
cuda和cudnn安装
https://pytorch.org/get-started/previous-versions/
https://pypi.org/project/torchvision/
https://download.pytorch.org/whl/torch_stable.html
安装nvidia显卡驱动