How do I create a conda virtual environment?

To create a conda virtual environment:

  1. Download the latest version of Miniconda3 by running:

    curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    

    Then, install Miniconda3 by running the command:

    sh Miniconda3-latest-Linux-x86_64.sh
    

    Follow the installer prompts. Install Miniconda3 in the default location. Allow the installer to initialize Miniconda3.

  2. If you want to create a conda virtual environment immediately after installing Miniconda3, you need to load the changes made to your .bashrc.

    You can either:

    • Exit and reopen your shell (terminal).
    • Run source ~/.bashrc.
  3. Create a conda virtual environment using Miniconda3 by running:

    conda create OPTIONS -n NAME PACKAGES
    

    Replace NAME with the name you want to give your virtual environment.

    Replace PACKAGES with the list of packages you want to install in your virtual environment.

    (Optional) Replace OPTIONS with options for the conda create command. See the conda create documentation to learn more about available options.

    For example, to create a conda virtual environment for PyTorch® with CUDA 11.8, run the below command and follow the prompts:

    conda create -c pytorch -c nvidia -n pytorch+cuda_11-8 pytorch torchvision torchaudio pytorch-cuda=11.8
    
  4. Activate the conda virtual environment by running:

    conda activate NAME
    

    Replace NAME with the name of the virtual environment created in the previous step.

    For instance, to activate the example PyTorch with CUDA 11.8 virtual environment mentioned in the previous step, run:

    conda activate pytorch+cuda_11-8
    

    Once activated, you can test the example virtual environment is working by running:

    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)'
    

    You should see output similar to:

    Is available:  True
    Pytorch CUDA Compiled version:  11080
    Pytorch version:  2.0.1
    pytorch file:  /home/ubuntu/miniconda3/envs/pytorch+cuda_11-8/lib/python3.11/site-packages/torch/__init__.py
    Number of GPUs:  1
    

Last modified January 31, 2024: Delete script that's no longer useable (af5a731)