To create a conda virtual environment:
-
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.
-
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
.
Tip
For compatibility with the Python venv module, it’s recommended that you disable automatic activation of the conda base environment by running:
conda config --set auto_activate_base false
-
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 theconda 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
-
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
Note
Locally installed packages can conflict with packages installed in virtual environments. For this reason, it’s recommended to uninstall locally installed packages by running:
To uninstall packages installed locally for your user only, run:
pip uninstall -y $(pip -v list | grep ${HOME}/.local | awk '{printf "%s ", $1}')
To uninstall packages installed locally, system-wide (for all users), run:
sudo pip uninstall -y $(pip -v list | grep /usr/local | awk '{printf "%s ", $1}')
Warning
Don’t run the above uninstall commands on Lambda GPU Cloud on-demand instances!
The above uninstall commands remove all locally installed packages and, on on-demand instances, break programs including pip and JupyterLab.