DEV Community

Ernesto Lopez
Ernesto Lopez

Posted on

Do you want to work with env? let's talk about Conda - the basics

On Python, most of the time, it is a good practice to work with environments, simply because there are different packages or versions to work with between projects, and maintain all that dependencies can be a nightmare on a single environment. There are different options like venv but today we are going to talk a little aboud CONDA

Conda is an open source environment management systems that can be used to install and manage packages too. Ti can be installed on Mac, Linux or Windows, and also provide an Enterprise version

Think of it like having several minicomputers inside your computer.

Basically, you can install any package inside the Anaconda repo

It works with any language including Python, R, Java, JavaScript, etc. But today we are going to work some examples in Python.

Another aspect to consider, you will need to install miniconda or Anaconda to use Conda and the installation will get you also a version of python.

For this example i am going to use:

  • Anaconda
  • Ubuntu 20.04

Note make sure you haver installed wget on your ubuntu

Frist of all we are starting by downloading the anaconda script:

wget -O anaconda.sh https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh
Enter fullscreen mode Exit fullscreen mode

HERE you will find the list of all the available installers

Then, move to the directory where your anaconda.sh file was stored and execute:

bash anaconda.sh
 #After installantion successfully ends 
conda init
Enter fullscreen mode Exit fullscreen mode

If conda init executed correctly you will see something like this on your prompt:

(base) ernestolopez@mypc:~$
Enter fullscreen mode Exit fullscreen mode

(base) will let you know that conda was successfully installed but if you want to make sure, just run conda info
That will provide you with information such as:

  • active environment
  • env location
  • conda version
  • python version
  • user config file

And now we are going to see some useful commands to work with conda.

BASIC COMMANDS TO WORK WITH CONDA

We will start by listing our environments

(base) ernestolopez@mypc:~$ conda env list
# conda environments:
#
base                  *  /home/ernestolopez/anaconda3
Enter fullscreen mode Exit fullscreen mode

As we can see, we only have one environment, so how about if we create another one:

(base) ernestolopez@mypc:~$ conda create --name env2
Collecting package metadata (current_repodata.json): done
Solving environment: done

 ## Package Plan ##

  environment location: /home/ernestolopez/anaconda3/envs/env2



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
 #
 # To activate this environment, use
 #
 #     $ conda activate env2
 #
 # To deactivate an active environment, use
 #
 #     $ conda deactivate

Enter fullscreen mode Exit fullscreen mode

If we analize this output, we obser two new commands:

  • conda activate env2 will activate the new environment and automatically we are going to be inside of it
  • conda deactivate will deactivate the current environemnt, befor you can delete any environment you will need to deactivate it first.

OK, Ok

So we created an environment, but we didn't tell the environment that we wanted new packages or any different version right?
Let's create a new environment with an specific python version (3.6) and a two new packages.

(base) ernestolopez@mypc:~$ conda create --name=env-demo-36 python=3.6 astor

 #you can add new packages just by adding a space and the name of the package in anaconda repo
Enter fullscreen mode Exit fullscreen mode

When prompt, answer yes

AN now we will get into this new environment and see the packages listed

(base) ernestolopez@mypc:~$ conda activate env-demo-36
(env-demo-36) ernestolopez@mypc:~$ 

(env-demo-36) ernestolopez@mypc:~$ conda list
# packages in environment at /home/ernestolopez/anaconda3/envs/env-demo-36:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
_openmp_mutex             4.5                       1_gnu  
astor                     0.8.1            py36h06a4308_0  
ca-certificates           2021.10.26                         h5101ec6_17  
openssl                   1.1.1m               h7f8727e_0  
pip                       21.2.2           py36h06a4308_0  
python                    3.6.13               h12debd9_1  
readline                  8.1.2                h7f8727e_1  
setuptools                58.0.4           py36h06a4308_0  
sqlite                    3.37.0               hc218d9a_0  
tk                        8.6.11               h1ccaba5_0  
wheel                     0.37.1             pyhd3eb1b0_0  
xz                        5.2.5                h7b6447c_0  
zlib                      1.2.11               h7f8727e_4  


 #THis give you a complete list if we want to see a specific package we can use 

(env-demo-36) ernestolopez@mypc:~$ conda list python
# packages in environment at /home/ernestolopez/anaconda3/envs/env-demo-36:
#
# Name                    Version                   Build  Channel
python                    3.6.13               h12debd9_1 
Enter fullscreen mode Exit fullscreen mode

NOTE > We can start developing inside this environment by moving to the folfer of the env /home/ernestolopez/anaconda3/envs/env-demo-36 and writing code .

We can update packages inside our environment:

(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda update python
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/ernestolopez/anaconda3/envs/env-demo-36

  added / updated specs:
    - python


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    astor-0.8.1                |   py39h06a4308_0          47 KB
    ------------------------------------------------------------
                                           Total:          47 KB

The following NEW packages will be INSTALLED:

  tzdata             pkgs/main/noarch::tzdata-2021e-hda174b7_0

The following packages will be UPDATED


Proceed ([y]/n)? y

Downloading and Extracting Packages
astor-0.8.1          | 47 KB     | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda list python
# packages in environment at /home/ernestolopez/anaconda3/envs/env-demo-36:
#
# Name                    Version                   Build  Channel
python                    3.9.7                h12debd9_1  
(env-demo-36) 

Enter fullscreen mode Exit fullscreen mode

WE have just update Python, and now our beautiful name env-demo-36 is not valid anymore because we are not in 3.6 version

In Conda, you cannot rename an environment, so what we want to do es clone our environemnt with a new name, and delete the old one:

(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda create --name env-demo-39 --copy --clone env-demo-36
Source:      /home/ernestolopez/anaconda3/envs/env-demo-36
Destination: /home/ernestolopez/anaconda3/envs/env-demo-39
Packages: 22
Files: 0
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate env-demo-39
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(env-demo-36) 
Enter fullscreen mode Exit fullscreen mode

If you list both environments, you will see that both are similar.

It is time to close this basic tutorial, and now we are going to delete our environment:

(env-demo-36) elopez@winterfell:~/anaconda3/envs/env-demo-36$ conda env list
# conda environments:
#
base                     /home/ernestolopez/anaconda3
env-demo-36           *  /home/ernestolopez/anaconda3/envs/env-demo-36
env-demo-39              /home/ernestolopez/anaconda3/envs/env-demo-39
env2                     /home/ernestolopez/anaconda3/envs/env2

(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda env remove --name env2

Remove all packages in environment /home/ernestolopez/anaconda3/envs/env2:

(env-demo-36)  
Enter fullscreen mode Exit fullscreen mode

If you list your environments again, it will not appear, but what happens if i try to delete my current environment?? *(env-demo-36) *

It will provoke an error, remember i just told you, you need to deactivate your env previous to delete it.

(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda env remove --name env-demo-36

CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again

(env-demo-36) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda deactivate
(base) ernestolopez@mypc:~/anaconda3/envs/env-demo-36$ conda env remove --name env-demo-36

Remove all packages in environment /home/ernestolopez/anaconda3/envs/env-demo-36:


Enter fullscreen mode Exit fullscreen mode

AND THAT's IT!!

Hope this information was useful!!!

Top comments (0)