Python

Delete Conda Environment

Conda is a free and open-source package management system for Python. It is an excellent alternative to pip as it offers package and environment management in a single utility.

Using conda, you can create, activate, deactivate, switch, and delete virtual environments with simple and intuitive commands.

This tutorial will guide you through the basics of working with conda environments in simple and easy-to-follow steps.

Let’s get started.

Conda Create Environment

To create a virtual environment with conda, all you need is access to a terminal on a system with conda installed.

1. To create a virtual env:

$ conda create --name environment_name

Replace ‘environment_name’ with the name of your virtual environment.

Once you run the conda create command, you will be prompted whether you wish to proceed with the location of the virtual environment.

Type the letter ‘Y’ to proceed.

Conda Activate Environment

Once you have created your anaconda environment, you will need to activate it before use.

Luckily, you can use a simple command as:

$ conda activate environment_name

The command above will initialize and activate the environment with the specified name.

In most cases, your terminal prompt will update with the prefix of your environment name when active.

 

Conda Show Environments

In some instances, you may have more than one virtual environment. Instead of manually browsing for their names, you can use the command:

$ conda env list

To show the available environments and the path where they are located.

Conda Deactivate Environment

It is recommended to deactivate an environment when performing Python operations globally.

You can use the deactivate command as;

$ conda deactivate

You must have at least one environment active before running the deactivate command:

Conda Delete Environment

After you are done with a specific environment, you can delete it from your system using the commands provided below:

Note that before running the delete commands, ensure the target environment is deactivated.

$ conda env remove --name environment_name

The command will delete the environment with the specified name.

You can also use the -n instead of the –name flag.

$ conda env remove -n environment_name

Conda also allows you to specify the environment path instead of the name. The command is as shown:

$ conda env remove ---prefix /path/to/environment

Or the short form:

$ conda env remove -p /path/to/environment

If you want to remove the environment manually, you can delete the directory where the environment is stored.

Start by getting the path of the environment:

$ conda env list

Once you have the path to the target environment, use the rm command:

$ rm -rvf /path/to/environment

Although removing the environment directory works, it is not recommended. It may lead to anaconda referencing an environment that does not exist.

Conclusion

This tutorial covered the basics of working with virtual environments with conda. We started with creating new environments, how to activate and deactivate them, and finally, how to delete inactive environments.

Removing conda virtual environments allows you to remove corrupted environments and save some space on your system.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list