How to Conda update Python in virtual environment?


There are multiple ways to update Python using Anaconda. You can create a virtual environment with desired version of Python or you can also install target Python version in main environment.

What is Anaconda?

Conda is the open source package & environment management system. It was created for Python but could be used for any language. In simple words, it helps you find and install packages.

Before doing anything, you should know the current installed version on Python in your system.

python --version

# Output 👉 Python 3.9.13
checking python version in anaconda terminal

Next we need to see the available versions of Python in conda package manager –

conda search python
getting list of available python versions in conda package manager using conda search python

With the help of this, we now know the available versions and the version installed on our system.

1. Installing Python in Virtual Environment

Follow these steps to create a virtual environment of desired Python version.

Step 1: Create Virtual Environment using the below command –

conda create -n virtual_env_name python=3.6 anaconda

Here, change virtual_env_name with whatever you want to call this environment. And we are using python v3.6 but you can use any available version.

creating virtual environment with specific python version using anaconda

Step 2: Activate the environment –

conda activate virtual_env_name

Again change virtual_env_name with your environment name.

Why environment activation is required?

Activation helps in two purposes –

  1. Running activation scripts which are necessary for setting environment variables for packages.
  2. Adding entries to the PATH for environment
activate virtual environment in conda using conda activate virtual_env_name

Step 3: Check if you are in the correct environment. In the terminal you should see your environment name in () or [] like (virtual_env_name) $.

If you could not see that, then list all the environments using below command and check if your desired environment is selected by (*).

conda info --envs
getting list of virtual environments in conda using conda info --envs

Step 4: Check the python version in the new environment.

python --version
checking python version in virtual environment

2. Upgrading minor version of Python

If you just want to upgrade the minor version of Python then you can use the below command –

conda update python

By minor version, I mean that the python will update within same branch. So, if you have 3.5.4 installed then it will upgrade to 3.5.6. But it won’t change to 3.9.15. Only the minor version 3.5.x will upgrade.

3. Upgrading major version of Python

If you wish to upgrade major version then you need to install that. Anaconda will replace the necessary files for upgradation. This is not recommended as files will be replaced by conda and could lead to breaking of installation. Better to create a virtual environment for different versions.

conda install python=3.8