Python/使用 anaconda 管理多个版本的 CUDA

Python/使用 anaconda 管理多个版本的 CUDA

虽然现在 Tensorflow 2 已经发布一段时间了,但仍然有很多历史代码是基于 Tensorflow 1 的。

由于经常需要复现过去的论文的代码,需要使用 Tensorflow 1 的 GPU 版本 ,而我的服务器已经安装了 Tensorflow 2 的 GPU 版本。

Tensorflow 2Tensorflow 1 的 GPU 版本需要的 CUDA 和 cudnn 版本都不一样。

下面来看看如何在一个同一个服务器上,安装不同版本的 Tensorflow GPU 版本。

我们先来看下 Tensorflow 官网 对于不同的 Tensorflow GPU 需要的 CUDA 版本要求。


目前的情况是:我的服务器已经安装了 CUDA 10.1tensorflow-2.1.0,现在我想安装 tensorflow_gpu-1.15.0,而这需要 CUDA 10.0

也就是说需要两个不同版本的 CUDA。

通常网上搜索到的办法是 安装多个版本的 CUDA,但是这种方法的缺点是:当我们需要切换不同的 tensorflow 环境时,也需要手动切换对应版本的 CUDA,才能使用 GPU。

我发现了一种更加简便的方法,即是用 anaconda 来管理多个版本的 tensorflowCUDA

网上的大多数 tensorflow GPU 安装教程,都是手动安装 CUDA、cudnn、tensorflow,需要我们自己精确他们之间的版本对应关系,极其容易在版本上出错,而且费时费力。

其实使用 anaconda 也可以安装 CUDA、cudnn,而且可以创建多个不同的环境,来使用不同版本的 CUDA、cudnn。

比如我们需要安装 tensorflow_gpu-1.15.0,首先创建一个环境 tensorflow_gpu-1.15

1
conda create -n tensorflow_gpu-1.15 python=3.6

切换到这个新建的环境:

1
source activate tensorflow_gpu-1.15

然后使用 conda 安装 tensorflow_gpu-1.15.0

1
conda install -c tensorflow_gpu==1.15.0

就是这么简单,conda 会帮我们下载安装 tensorflow_gpu-1.15.0 对应版本的 CUDA 和 cudnn 等库。

注意,这里不要使用 pip 来安装。

因为使用 conda 和 pip 安装是有区别的。

  • conda 会检查 tensorflow_gpu-1.15.0 对应版本的 CUDA、cudnn 等,并自动安装这些依赖库。
  • pip 不会安装对应版本 CUDA、cudnn。

最后一点,如果你使用 conda 安装特定版本的 tensorflow 失败,比如下面的 PackagesNotFoundError 错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

- tensorflow_gpu=1.15.0

Current channels:

- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/linux-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

https://anaconda.org

and use the search bar at the top of the page.

你可以打开 anaconda 的官网:https://anaconda.org/,在搜索框里搜索 tensorflow


然后在搜索结果页中,找到你想要安装的版本,比如我这里要找 tensorflow_gpu-1.15.0,点击进去。


就可以看到对应的安装命令 conda install -c databricks tensorflow-gpu


使用这个命令安装即可。

参考

评论