|
| 1 | +--- |
| 2 | +title: "How do I create a conda virtual environment?" |
| 3 | +type: docs |
| 4 | +tags: |
| 5 | +- CUDA |
| 6 | +- PyTorch |
| 7 | +--- |
| 8 | + |
| 9 | +To create a conda virtual environment: |
| 10 | + |
| 11 | +1. Download the latest version of |
| 12 | + [Miniconda3](https://docs.conda.io/en/latest/miniconda.html) by running: |
| 13 | + |
| 14 | + ```bash |
| 15 | + curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh |
| 16 | + ``` |
| 17 | + |
| 18 | + Then, install Miniconda3 by running the command: |
| 19 | + |
| 20 | + ```bash |
| 21 | + sh Miniconda3-latest-Linux-x86_64.sh |
| 22 | + ``` |
| 23 | + |
| 24 | + Follow the installer prompts. Install Miniconda3 in the default location. |
| 25 | + Allow the installer to initialize Miniconda3. |
| 26 | + |
| 27 | +1. If you want to create a conda virtual environment immediately after |
| 28 | + installing Miniconda3, you need to load the changes made to your `.bashrc`. |
| 29 | + |
| 30 | + You can either: |
| 31 | + |
| 32 | + - Exit and reopen your shell (terminal). |
| 33 | + - Run `source ~/.bashrc`. |
| 34 | + |
| 35 | + {{% alert title="Tip" color="success" %}} |
| 36 | + For compatibility with the |
| 37 | + [Python venv module]({{< relref "create-python-virtual-environment" >}}), |
| 38 | + it's recommended that you disable automatic activation of the conda base |
| 39 | + environment by running: |
| 40 | + |
| 41 | + ```bash |
| 42 | + conda config --set auto_activate_base false |
| 43 | + ``` |
| 44 | + {{% /alert %}} |
| 45 | + |
| 46 | +1. Create a conda virtual environment using Miniconda3 by running: |
| 47 | + |
| 48 | + ```bash |
| 49 | + conda create OPTIONS -n NAME PACKAGES |
| 50 | + ``` |
| 51 | + |
| 52 | + Replace **NAME** with the name you want to give your virtual environment. |
| 53 | + |
| 54 | + Replace **PACKAGES** with the list of packages you want to install in your |
| 55 | + virtual environment. |
| 56 | + |
| 57 | + (Optional) Replace **OPTIONS** with options for the `conda create` command. |
| 58 | + [See the `conda create` documentation](https://docs.conda.io/projects/conda/en/latest/commands/create.html) |
| 59 | + to learn more about available options. |
| 60 | + |
| 61 | + For example, to create a conda virtual environment for PyTorch® with CUDA |
| 62 | + 11.8, run the below command and follow the prompts: |
| 63 | + |
| 64 | + ```bash |
| 65 | + conda create -c pytorch -c nvidia -n pytorch+cuda_11-8 pytorch torchvision torchaudio pytorch-cuda=11.8 |
| 66 | + ``` |
| 67 | +1. Activate the conda virtual environment by running: |
| 68 | + |
| 69 | + ```bash |
| 70 | + conda activate NAME |
| 71 | + ``` |
| 72 | + |
| 73 | + Replace **NAME** with the name of the virtual environment created in the |
| 74 | + previous step. |
| 75 | + |
| 76 | + For instance, to activate the example PyTorch with CUDA 11.8 virtual |
| 77 | + environment mentioned in the previous step, run: |
| 78 | + |
| 79 | + ```bash |
| 80 | + conda activate pytorch+cuda_11-8 |
| 81 | + ``` |
| 82 | + |
| 83 | + Once activated, you can test the example virtual environment is working by |
| 84 | + running: |
| 85 | + |
| 86 | + ```bash |
| 87 | + python -c 'import torch ; print("\nIs available: ", torch.cuda.is_available()) ; print("Pytorch CUDA Compiled version: ", torch._C._cuda_getCompiledVersion()) ; print("Pytorch version: ", torch.__version__) ; print("pytorch file: ", torch.__file__) ; num_of_gpus = torch.cuda.device_count(); print("Number of GPUs: ",num_of_gpus)' |
| 88 | + ``` |
| 89 | + |
| 90 | + You should see output similar to: |
| 91 | + |
| 92 | + ``` |
| 93 | + Is available: True |
| 94 | + Pytorch CUDA Compiled version: 11080 |
| 95 | + Pytorch version: 2.0.1 |
| 96 | + pytorch file: /home/ubuntu/miniconda3/envs/pytorch+cuda_11-8/lib/python3.11/site-packages/torch/__init__.py |
| 97 | + Number of GPUs: 1 |
| 98 | + ``` |
| 99 | + |
| 100 | +{{% alert title="Note" color="info" %}} |
| 101 | +Locally installed packages can conflict with packages installed in virtual |
| 102 | +environments. For this reason, it's recommended to uninstall locally installed |
| 103 | +packages by running: |
| 104 | + |
| 105 | +To uninstall packages installed locally for your user only, run: |
| 106 | + |
| 107 | +```bash |
| 108 | +pip uninstall -y $(pip -v list | grep ${HOME}/.local | awk '{printf "%s ", $1}') |
| 109 | +``` |
| 110 | + |
| 111 | +To uninstall packages installed locally, system-wide (for all users), run: |
| 112 | + |
| 113 | +```bash |
| 114 | +sudo pip uninstall -y $(pip -v list | grep /usr/local | awk '{printf "%s ", $1}') |
| 115 | +``` |
| 116 | +{{% /alert %}} |
| 117 | + |
| 118 | +{{% alert title="Warning" color="warning" %}} |
| 119 | +**Don't run the above uninstall commands on Lambda GPU Cloud on-demand |
| 120 | +instances!** |
| 121 | + |
| 122 | +The above uninstall commands remove all locally installed packages and, on |
| 123 | +on-demand instances, break programs including pip and JupyterLab. |
| 124 | +{{% /alert %}} |
| 125 | + |
| 126 | +{{% alert title="Tip" color="success" %}} |
| 127 | +[See the Conda documentation](https://docs.conda.io/projects/conda/en/stable/commands.html) |
| 128 | +to learn more about how to manage conda virtual environments. |
| 129 | +{{% /alert %}} |
0 commit comments