In our earlier article, we had shown how to install multiple versions of python without breaking System tools and Create an Isolated environment using Virtualenv?. Here’s an another method to create virtual environment for different versions of Python – using Conda.
Conda is an open-source package management system and environment management for any language such as Python, Ruby, R, Lua, Scala, Java, C/C++, Fortran. Conda easily creates, saves, loads and switches between environments. So if you need to execute an application or a program that requires different version of Python, then Conda helps to do that.
How to install Conda Package Management System
There are two ways to install Conda.
- The fastest way is to install Miniconda, a mini-version of Anaconda that includes only Conda and its dependencies.
- Need Conda and 720+ open source packages, then install Anaconda.
We recommend you to install Anaconda. An advantage of Anaconda is, it does not require administrator privileges.
How to install Anaconda – Package Management system
Step 1: Install Prerequisites
#apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
Step 2: Download the Anaconda Installer for Linux
You can download Anaconda installer for Linux depending on your Python version. Of late Anaconda is supporting 64-bit version of Linux. Make sure you are running 64-bit OS and not 32-bit.
Step 3: Run Anaconda Installer
Once you have downloaded the Anaconda installer, run it on your terminal using the below command:
Follow the prompts on the installer screens.
The installer prompts “Do you wish the installer to initialize Anaconda3 by running Conda init?” We recommend “yes”.
Upon successful installation, close and open the terminal to take effect.
Verify the Conda
Create a Virtual Environment for your project
To use a particular Python version for your Virtual Environment, first search the list of available python versions using the below command:
$conda search "^python$"
This will list all python versions. You can select the particular version for your virtual environment by replacing the x.x with the version you require in the below command:
$conda create -n name_of_the_env python=x.x anaconda
Press Y to proceed. This will install the Python version and all the associated anaconda package libraries at the path you specify during the environment creation.
Activate your Virtual Environment
Once you create the virtual environment, you need to activate it by simply executing the below command with your environment name to switch to your virtual environment.
$conda activate name_of_the_env
How to install additional Python packages to your Virtual Environment
You can install additional Python packages to your virtual environment using the below command:
$conda install -n name_of_the_env [package]
De-activate your Virtual Environment
You can de-activate your virtual environment to end the session.
List all virtual environments
You can get a list of all virtual environments using the below command:
$conda env list
Delete a non-required Virtual Environment
You can delete a virtual environment which is no longer needed using the below command:
$conda remove -n name_of_the_env -all
Refer this cheat sheet for more conda commands.