December 22, 2024

Create Virtual Conda/Anaconda Python Environments in Linux and Install/Manage Packages

In this tutorial, we explain how to create, manipulate, and delete Anaconda or Conda Python virtual environments in Linux Ubuntu and how to install and manage packages. The YouTube tutorial accompanying this webpage is given below.

How to install Conda and Anaconda

The first step is to install Anaconda such that we can run conda commands in the Linux terminal. We created a separate video tutorial explaining how to install Anaconda. The tutorial is given here.

After the installation process is completed, let us make sure that conda is installed and that it is in the system path. To do that, open a terminal and type

conda list

As the output you should see a list of installed packages and their versions in the base anaconda environment ~/anaconda3

How to create and activate/deactivate Conda environments in Linux Ubuntu

First, let us learn how to create Conda environments in Linux Ubuntu. First, let us create our folder. Open a Linux terminal and type:

mkdir codes
cd codes
mkdir testConda
cd testConda

To create an environment we need to type

conda create -n <env-name>

Let us create an environment called “env1”:

conda create -n env1

Then, to activate that environment, we need to type

conda activate env1

Now, you will see that the newly created environment is active

(env1) aleksandar@aleksandar:~$

To deactivate the environment (to exit the environment), you need to type this:

conda deactivate

And you will be returned to the base environment. To list all currently created environments, you need to type this:

conda info --envs

The output will look like this:

# conda environments:
#
base                     /home/aleksandar/anaconda3
env1                     /home/aleksandar/anaconda3/envs/env1

You will see the list of all currently available environments. The “base” environment is the environment that comes with Anaconda.

How to install Packages in Conda or Anaconda environment

In the standard Python virtual environments, we use “pip install” to install a package. Although, we can use “pip install” in Anaconda, this is the third option for installing the packages, and it should be used only if the first two options do not work or the package is not available through the first two options.

The first option is to use conda install <name of the package>.

First, let us activate the environment we created:

conda activate env1

Then, let us check the packages that are currently installed in the environment:

conda list

The list should be empty. To see if a particular package is available and can be installed in Anaconda or Conda, type this

conda search <name of the package>

To search for scipy, type this:

conda search scipy

Let us install a numpy and scipy packages

conda install numpy
conda install scipy

Then, let us double-check that the packages are actually installed:

conda list

Now we can see the list of installed packages:

# packages in environment at /home/aleksandar/anaconda3/envs/env1:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
_openmp_mutex             5.1                       1_gnu  
blas                      1.0                         mkl  
bzip2                     1.0.8                h5eee18b_6  
ca-certificates           2024.7.2             h06a4308_0  
expat                     2.6.3                h6a678d5_0  
intel-openmp              2023.1.0         hdb19cb5_46306  
ld_impl_linux-64          2.38                 h1181459_1  
libffi                    3.4.4                h6a678d5_1  
libgcc-ng                 11.2.0               h1234567_1  
libgfortran-ng            11.2.0               h00389a5_1  
libgfortran5              11.2.0               h1234567_1  
libgomp                   11.2.0               h1234567_1  
libstdcxx-ng              11.2.0               h1234567_1  
libuuid                   1.41.5               h5eee18b_0  
mkl                       2023.1.0         h213fc3f_46344  
mkl-service               2.4.0           py312h5eee18b_1  
mkl_fft                   1.3.10          py312h5eee18b_0  
mkl_random                1.2.7           py312h526ad5a_0  
ncurses                   6.4                  h6a678d5_0  
numpy                     1.26.4          py312hc5e2394_0  
numpy-base                1.26.4          py312h0da6c21_0  
openssl                   3.0.15               h5eee18b_0  
pip                       24.2            py312h06a4308_0  
pybind11-abi              5                    hd3eb1b0_0  
python                    3.12.5               h5148396_1  
readline                  8.2                  h5eee18b_0  
scipy                     1.13.1          py312hc5e2394_0  
setuptools                72.1.0          py312h06a4308_0  
sqlite                    3.45.3               h5eee18b_0  
tbb                       2021.8.0             hdb19cb5_0  
tk                        8.6.14               h39e8969_0  
tzdata                    2024a                h04d1e81_0  
wheel                     0.44.0          py312h06a4308_0  
xz                        5.4.6                h5eee18b_1  
zlib                      1.2.13               h5eee18b_1  

We see not only NumPy and SciPy but also their prerequisites. We can install a particular version of the package by typing

conda install scipy=0.15.0

We can install several packages at the same time by typing

conda install scipy numpy pygame

You can also use “pip install”. However, sometimes there might be some compatibility issues, and use “pip install” only if conda packages are not available:

pip install matplotlib

How to run a Python program in Conda or Anaconda environment

Make sure that the environment is created, and activated, and make sure that the packages are installed.

Let us test that these packages are installed by creating a simple program. Open your favorite python editor and type this program:

import numpy as np
import scipy

matrix1=np.array([[1,2],[3,4]])
matrix2=np.array([[1,3],[7,8]])

matrix3=np.matmul(matrix1,matrix2)

matrix4=scipy.linalg.inv(matrix2)

matrix5=np.matmul(matrix3,matrix4)

print(matrix1)
print(matrix5)

Save this file as “test1.py”. To run this file from our virtual environment, first make sure that the path of the python executable is actually in our virtual environment. To do that, in the terminal type:

which python3

and you should see the path to the Python executable file:

/home/aleksandar/anaconda3/envs/env1/bin/python3

It is important that this path is inside of our python virtual environment (which is the case). We can also check the Python version by typing

python3 --version

To run the file, simply type

python3 test1.py

or you can run the file from the VS Code (make sure that the proper virtual environment is selected).

How to Create Anaconda or Conda environment for a particular Python version

Sometimes it is necessary to create Anaconda or Conda environment for a particular Python version. First deactivate the current environment (if it is active)

conda deactivate

To create anaconda environment for a particular Python version, simply type the conda create command with a specified Python version. For example, if need Python 3.10, we need to type this

conda create -n env2 python=3.10

To activate that environment, simply type this

conda info --envs

conda activate env2

python3 --version

To deactivate, simply type

conda deactivate

How to remove Anaconda or Conda environment

To remove (erase) Anaconda or Conda environment, simply type this

conda remove --name <name of environment> --all

To remove our environments, we need to type

conda remove --name env1 --all 
conda remove --name env2 --all

After that, double-check that the environment is removed:

conda info --envs

and you should only see the base environment.