文章目录
- Commonly Used Conda Commands
- Check for GPU Driver Information
- Create a Virtual Environment for Python Project
- View and Edit Virtual Environment Directory
- Install Additional Packages to Your Virtual Environment
- Clone a Virtual Environment for Python Project
- Remove a Specific Package and Delete the Virtual Environment
Commonly Used Conda Commands
View existing virtual environments:
conda env list
Check which packages are installed:
conda list
conda info -e
Check for updates to the current Conda:
conda update conda
Check for GPU Driver Information
nvidia-smi
Sample output:
'CUDA Version' in nvidia-smi shows you the highest CUDA version that your driver supports.
Create a Virtual Environment for Python Project
Create a virtual environment using the conda command. Replace [env name] with your desired environment name, x.x with your Python version, and specify CUDA and cuDNN versions as necessary. It’s important to ensure that the CUDA version aligns with the PyTorch version you intend to use.
If cuDNN version is not specified, it will automatically match with the given CUDA version.
conda create -n [env name] python=x.x cudatoolkit=x.x cudnn=x.x
View and Edit Virtual Environment Directory
View the directory of your Conda environments
This following command will display all the environments you have, and next to each environment’s name, you’ll see its directory path. The currently activated environment will be marked with an asterisk (*).
conda env list
Check and edit the default location of virtual environments
To determine the default location of your virtual environments, execute the following command
conda info
Sample Output:
envs directories : /home/[username]/.conda/envs
/home/[username]/anaconda3/envs
In the output, scroll down to the section labeled ‘envs_dirs’. Here, you’ll see several paths listed. The first one is the default installation path for virtual environments.
If you want to set a specific directory as the default for your Conda environments, you can use the ‘conda config’ command.
Execute the following command, replacing ‘[path]’ with the full path of your desired directory:
conda config --add envs_dirs [path]
By executing this command, the specified directory will be added to the list of directories where Conda looks for and creates environments. If the directory is already in the list, it will be set as the default location for new environments.
Install Additional Packages to Your Virtual Environment
When installing packages using pip or conda in China, the download speed can be very slow because the servers are located overseas. Therefore, it is recommanded to use domestic mirror sources to improve the speed of installing third-party libraries.
Creat a domestic mirror for ‘Pip’
Create a ‘.pip’ Directory
mkdir ~/.pip
Navigate to the Directory
cd ~/.pip
Open the ‘pip.conf’ configuration file in ‘vim’ or your preferred text editor:
vim pip.conf
Inside ‘vim’, press i to switch to insert mode and add the following content:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
Press Esc, then type :wq and press Enter to save and exit.
Creat a domestic mirror for ‘Conda’
Add Tsinghua Conda mirror channels for faster Conda installations:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
To enable the display of channel URLs when you search:
conda config --set show_channel_urls yes
Install Packages to Your Environment Using ‘pip install’
First, activate your environment.
conda activate [env name]
Next, use ‘pip install’ to add the packages you wish to include.
pip install [package]
Deactivate your enviroument
conda deactivate [env name]
Install Packages to Your Environment Using ‘conda’
conda install -n your_env_name [package]
Clone a Virtual Environment for Python Project
Clone an existing environment using the conda command. This allows you to replicate all the packages and configurations from one environment into another, which can be especially useful when you want to experiment without altering the original settings.
Replace [source_env] with the name of the environment you wish to clone and [clone_env] with your desired name for the cloned environment:
conda create --name [clone_env] --clone [source_env]
Remove a Specific Package and Delete the Virtual Environment
Important Note on conda and pip Package Management
It’s crucial to understand that packages installed via conda should also be uninstalled using conda. Similarly, for packages installed with pip, you should use pip uninstall to remove them.
Remove a package using ‘pip’
First, activate the environment:
conda activate [env name]
Then, use pip to uninstall the package:
pip uninstall [package]
conda deactivate [env name]
conda deactivate [env name]
Remove a package using ‘Conda’
To remove a package from a specific environment using conda:
conda remove -n [env name] [package name]
Delete speacific environment
conda remove -n [env name] --all