Working with Conda – Basic Commands

I have been using python venv module for all my virtual environments needs. It is basic and works very well. However since I got my M1 chip MacBook, things changed. A lot of python libraries are not supported yet. Miniforge supports the Rosetta 2 layer – as a result quite a few of the libraries can work again.

To get over the issues, I decided to use community version of miniforge. Miniforge is a community driven version for miniconda and all libraries are downloaded from conda-forge. Since all of these are new for me, I decided to put the basic commands in one place so that I can refer back to them whenever needed.

Installation

I will be using the community driven version of conda. You can download miniforge installer from https://github.com/conda-forge/miniforge. Following list shows all builds for miniforge.

Miniforge builds

For MacBook with M1 chip, you will only need to run Miniforge3-MacOSX-arm64.sh and follow prompts to install Miniforge.

For this task I will install PyTorch in a virtual environment. This should give an example for how to create and use a fresh virtual conda environment.

Create a Virtual Environment

First let’s do some housekeeping stuff.

$ conda update conda -y
Collecting package metadata (current_repodata.json): done
 Solving environment: done
 All requested packages already installed.

$ conda clean --packages
Cache location:
 There are no unused packages to remove

$ conda -V
conda 4.10.2

$ conda env list
conda environments:
 #
 base                  *  /opt/homebrew/Caskroom/miniforge/base

We updated all outdated packages in conda install to latest version. Since conda does not cleanup old packages by default, we have also made sure that we clean them in the next command. The commands following this are informational and prints the version and all installed virtual environments.

Now let’s create a new virtual environment and install pytorch in it. We will use the default version of python installed. Conda allows multiple versions of python to be maintained. We can use following command to list all available versions of python.

$ conda search "^python$"
Loading channels: done
 Name                       Version           Build  Channel
 python                         3.8.5 h05baefb_8_cpython  conda-forge
 python                         3.8.6 h12cc5a1_1_cpython  conda-forge
 python                         3.8.6 h12cc5a1_2_cpython  conda-forge
 python                         3.8.6 h12cc5a1_3_cpython  conda-forge
 ::::: and many more :::::

Use the following command to create an empty virtual environment with the default version of python installed.

$ conda create -n pytorch
Collecting package metadata (current_repodata.json): done
 Solving environment: done
 Package Plan
 environment location: /opt/homebrew/Caskroom/miniforge/base/envs/pytorch
 Proceed ([y]/n)? y
 Preparing transaction: done
 Verifying transaction: done
 Executing transaction: done
 #
 To activate this environment, use
 #
 $ conda activate pytorch
 #
 To deactivate an active environment, use
 #
 $ conda deactivate

$ conda activate pytorch

The last command activates pytorch virtual environment. Next step is to install pytorch.

$ conda install pytorch -y
 :::: lots of output :::
 Preparing transaction: done
 Verifying transaction: done
 Executing transaction: done

$ conda install notebook -y

We installed pytorch and jupyter notebook above. Let’s run some basic test now.

Running a Basic Test

First we start jupyter notebook

$ conda list
packages in environment at /opt/homebrew/Caskroom/miniforge/base/envs/pytorch:
 #

$ jupyter notebook

The first command list all installed packages in pytorch environment. Before starting notebook, we make sure that pytorch and jupyter notebook are installed. Try some pytorch commands and see if it works.

Jupyter view

Everything seems to be working. As the next step, we will remove this virtual environment. This is not necessary as we would probably continue to work on this environment.

$ conda deactivate
$ conda env remove -n pytorch

Pip vs Conda

FeaturePIP commandConda command
Versionpip -Vconda -V
Create Environmentpython3 -m venv NAMEconda create -n NAME
Activatesource NAME/bin/activateconda activate NAME
Deactivatedeactivateconda deactivate
Install packagepip install PCKGconda install PCKG
Uninstall packagepip uninstall PCKGconda remove PCKG
List packages installedpip listconda list
Remove venvrm -rf ./NAMEconda env remove -n NAME
Exportpip freeze > rqmt.txtconda list –export > rqmt.txt
Importpip install -r rqmt.txtconda create -n NAME –file rqmt.txt
PIP vs Conda

Conclusion

I started my journey with conda with the list of commands above. Hopefully it helps someone. Ciao for now!