Creating a Python Virtual Environment Using Conda on Ubantu(CUDA and cuDNN)

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:
nvidia-smi
'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 
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值