Can't install GPU-enabled Pytorch in Conda environment from environment.yml
I'm on Ubuntu 20.04 LTS with CUDA 11.1 installed (and working, with PATH and LD_LIBRARY_PATH configured correctly), and I'm trying to define a reusable conda environment (i.e., in an environment.yml file) that successfully installs PyTorch with CUDA support.
However, when I use the environment file, I get a message that Torch wasn't compiled with CUDA support:
Python 3.8.10 | packaged by conda-forge | (default, May 11 2021, 07:01:05)
[GCC 9.3.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; device = torch.device(&quot;cuda:0&quot;)
&gt;&gt;&gt; t = torch.tensor(device=device, data=[0,1,2,3])
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/home/jdr2160/anaconda3/envs/foo/lib/python3.8/site-packages/torch/cuda/__init__.py&quot;, line 166, in _lazy_init
    raise AssertionError(&quot;Torch not compiled with CUDA enabled&quot;)
AssertionError: Torch not compiled with CUDA enabled

My environment.yml is pretty bare-bones:
name: foo
channels:
  - conda-forge
  - nvidia
  - pytorch
dependencies:
  - cudatoolkit=11.1
  - python=3.8
  - pytorch

When I create an 'empty' python 3.8 environment and install the Conda packages from the command line instead of from an environment file, everything works fine:
$ conda env create --name bar python=3.8
...
$ conda activate bar
$ conda install pytorch cudatoolkit=11.1 -c pytorch -c nvidia
...
$ python
Python 3.8.10 | packaged by conda-forge | (default, May 11 2021, 07:01:05)
[GCC 9.3.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; device = torch.device(&quot;cuda:0&quot;)
&gt;&gt;&gt; t = torch.tensor(device=device, data=[0,1,2,3])
&gt;&gt;&gt;

Can anyone tell what's going on here? It seems that Conda doesn't see the cudatoolkit=11.1 dependency while installing PyTorch from the environment file, but I have no idea how to fix it.

Just a few minutes after posting this question, I was able to figure out the solution. It turns out that it has to do with prioritizing Conda channels. The solution (which isn't well-documented by Anaconda) is to specify the correct channel for cudatoolkit and pytorch in environment.yml:
name: foo
channels:
  - conda-forge
  - nvidia
  - pytorch
dependencies:
  - nvidia::cudatoolkit=11.1
  - python=3.8
  - pytorch::pytorch

