Contents
Check to see that conda is in your PATH
- In a terminal enter
conda -V
- If conda is in your PATH you should see something similar to the following
netid@g2-login-01:~$ conda -V conda 4.10.3
- If the conda command is not found, you will need to add it to your PATH by issuing the following command (which is only good for the current shell) or append your .bashrc file with the following:
netid@g2-login-01:~$ export PATH=/share/apps/anaconda3/2021.05/bin:$PATH
- or issue the conda init command below
netid@g2-login-01:~$ /share/apps/anaconda3/2021.05/bin/conda init
- This conda init command places code in your . bashrc file that modifies, among other things, the PATH environment variable by prepending it to the path of the base conda environment
- You must log out and log back in to cause the new code in .bashrc to be referenced
- Note – the command prompt will change to indicate which conda environment you are currently in by prepending (base) – this is the default environment that includes a Python installation and some core system libraries and dependencies of Conda
(base) netid@g2-login-01:~$
Create a virtual environment
- In a terminal enter the following where virt_env_name is what you want to name your virtual environment and replace x.x.x with the version of Python you want to use
netid@g2-login-01:~$ conda create -n virt_env_name python=x.x.x
- To see a list of available Python versions, enter the following
netid@g2-login-01:~$ conda search "^python$"
- Press y or hit enter to proceed
- This will install the Python version and all the associated anaconda packaged libraries at
- /home/netid/.conda/envs/virt_env_name
List your virtual environments
- To see a list of all of your virtual environments, enter the following in a terminal
netid@g2-login-01:~$ conda info --envs or conda info -e
Activate a virtual environment
- To activate or switch into a virtual environment type the following where virt_env_name is the name you gave to your virtual environment when it was created
netid@g2-login-01:~$ conda activate virt_env_name
- Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created
- Note – the command prompt will change to indicate which conda environment you are currently in by prepending (virt_env_name)
(virt_env_name) netid@g2-login-01:~$
Install additional Python packages into a virtual environment
- To install additional packages to your virtual environment, enter the following command where virt_env_name is the name of the environment you want to modify and [package] is the name of the package you want to install
netid@g2-login-01:~$ conda install -n virt_env_name [package]
- Failure to specify “-n virt_env_name” will cause conda to attempt to install the package to the root Python installation located at /share/apps/anaconda3/2021.05 but users do not have write permissions to this directory so it will fail with
EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
List packages installed in a virtual environment
- To list all packages in a virtual environment, enter the following command where virt_env_name is the name of the environment you want to check on
netid@g2-login-01:~$ conda list -n virt_env_name
Deactivate a virtual environment
- To end a session in the current virtual environment, enter the following
netid@g2-login-01:~$ source deactivate
- There is no need to specify the virtual environment name – the virtual environment currently active will simply be deactivated and the PATH and shell variables will be returned to normal
Delete a virtual environment
- To delete a virtual environment, enter the following, where virt_env_name is the name of the virtual environment you want to delete
netid@g2-login-01:~$ conda remove -n virt_env_name --all
- Press y or hit enter to proceed
Example of creating and moving around in a virtual environment
- Create a virtual environment named pytorch using python v3.7.2 and add the pytorch and tk packages (and all dependencies) to the environment
netid@g2-login-01:~$ conda create -n pytorch python=3.7.2 pytorch tk netid@g2-login-01:~$ conda activate pytorch (pytorch) netid@g2-login-01:~$ which python /home/netid/.conda/envs/pytorch/bin/python (pytorch) netid@g2-login-01:~$ python -V Python 3.7.2 (pytorch) netid@g2-login-01:~$ conda deactivate netid@g2-login-01:~$
Additional information
- The official documentation for Conda can be found here