Getting Started with Conda environment [Part-1]

Mostafa Farrag
Hydroinformatics
Published in
9 min readDec 21, 2022

In this article, we will go through the first steps to use conda to create your python environment, so this is the first step for anyone who is looking to start working with python.

In the beginning, we have to give some context to what is anaconda and the main difference between anaconda, miniconda and conda, then we will install miniconda in Linux, once we have the installation work, we will create a new environment and install/update packages and add some conda channels.

The heading in this article are listed below

  • What is Anaconda?
  • What is Conda?
  • What is Miniconda?
  • What is Anaconda Navigator?
  • Installation (Windows/Linux)
  • Create new environment
  • Activate an environment
  • Install packages (Conda Channels)
  • Uninstall packages
  • Update packages

so let’s start with

What is Anaconda?

  • Anaconda is a free and open-source distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment.
  • Anaconda includes a collection of pre-built and pre-configured libraries, tools, and other software packages specifically designed for data science and machine learning.
  • Anaconda has a package manager called conda that allows you to easily install, update packages, and create and manage virtual environments for your projects.
  • Anaconda comes with a variety of popular data science libraries such as NumPy, Pandas, SciPy, scikit-learn, and others pre-installed, so you don’t have to worry about installing and configuring these libraries separately.
  • It also includes tools like Jupyter notebooks, which allow you to write and execute code, as well as visualize and document your results in an interactive and collaborative way.

What is Conda?

  • Conda is a standalone package manager and environment management system that is included with Anaconda but can also be used independently of it.
  • Conda allows you to manage and install packages from a variety of different sources (such as the Anaconda repository, PyPI, etc.), as well as create and manage virtual environments for your projects.

What is Miniconda?

  • Miniconda is a smaller version of Anaconda that includes only the conda package manager and Python.
  • It does not include any of the pre-built and pre-configured libraries, tools, and other software packages that come with Anaconda, and instead allows you to install only the packages you need for your projects.
  • Miniconda is useful if you want a lightweight and more customizable alternative to Anaconda, or if you want to install only the packages you need and avoid installing the larger collection of packages that come with Anaconda.

What is Anaconda Navigator?

  • Anaconda Navigator is a desktop graphical user interface (GUI) included in Anaconda Distribution, that allows you to launch applications and manage conda packages, environments, and channels without using command line interface (CLI) commands.

In a few words, Anaconda is an open-source distribution for python that has a lot of pre-installed tools which includes conda the package manager, while miniconda is a smaller version of Anaconda that includes the conda package manager and allows you to install the other tools.

Installation

  • As explained above anaconda comes with a lot of pre-installed tools that most of the time you won’t use, and the downside of this is the big size of the whole software, therefore it is good to start with Miniconda and step-by-step install the tool that you need.
  • For the installation of the Anaconda distribution check this Link.

Windows

  • to install miniconda in windows, you just need to download the installer from [here] and press double-click on the installer once it is downloaded and follow the instructions, mostly it will only ask you for the installation location.

Linux

  • Download the installer from here, from your command line type
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
  • Then leave it till it finishes the download
Download miniconda from your terminal
  • The downloaded executable won’t have the execute permission by default, to make it executable.
chmod +x Miniconda3-latest-Linux-x86_64.sh
  • Now you can execute the installer, to do so type the following and then press enter till the licence agreement finishes.
./Miniconda3-latest-Linux-x86_64.sh
  • once you reach the end of the license agreement type yes and confirm the installation location (the default location is HOME/miniconda3) then press enter, then the installation will start
  • The base environment will be activated by default in any terminal you open
  • Once you open any terminal you will notice the activated environment name at the left of the terminal (base)
  • by default the ~/miniconda3/bin directory will be added to the environment variable PATH, to make sure, check the $PATH environment variable
echo $PATH | grep miniconda3
  • if the ~/miniconda3/bin directory does not show up, you can add it manually.
export PATH="~/miniconda3/bin:$PATH"
  • Then reload your .bashrc file to apply the changes
source ~/.bashrc
  • To verify that the installation is successful check the installed version.
conda --version
  • Now you can execute any of the commands available in the conda command line interface, to list all the commands, use the — help flag.
conda --help

Now the installation is finished, and in the next steps we will create new environment and install some packages

Create new environment

  • to create a new environment from scratch you can use the create command followed by the environment name after the -n flag (name), then list all the packages you want to install, it is a good practice to start with python
conda create -n pyramids python=3.9
  • To create a new environment
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.12.0
latest version: 22.11.1
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /mnt/c/MyComputer/miniconda3/envs/pyramids
added / updated specs:
- python=3.9
The following packages will be downloaded:
package | build
- - - - - - - - - - - - - -| - - - - - - - - -
_openmp_mutex-5.1 | 1_gnu 21 KB
ca-certificates-2022.10.11 | h06a4308_0 124 KB
certifi-2022.12.7 | py39h06a4308_0 150 KB
ld_impl_linux-64–2.38 | h1181459_1 654 KB
libffi-3.4.2 | h6a678d5_6 136 KB
libgcc-ng-11.2.0 | h1234567_1 5.3 MB
libgomp-11.2.0 | h1234567_1 474 KB
libstdcxx-ng-11.2.0 | h1234567_1 4.7 MB
ncurses-6.3 | h5eee18b_3 781 KB
openssl-1.1.1s | h7f8727e_0 3.6 MB
pip-22.3.1 | py39h06a4308_0 2.7 MB
python-3.9.15 | h7a1cb2a_2 25.0 MB
readline-8.2 | h5eee18b_0 357 KB
setuptools-65.5.0 | py39h06a4308_0 1.1 MB
sqlite-3.40.0 | h5082296_0 1.2 MB
tk-8.6.12 | h1ccaba5_0 3.0 MB
tzdata-2022g | h04d1e81_0 114 KB
xz-5.2.8 | h5eee18b_0 429 KB
zlib-1.2.13 | h5eee18b_0 103 KB
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Total: 50.0 MB
  • Press yes then the default packages will be installed automatically, leave the installation to finish, at the end it will show the following

Activate an environment

  • now that we have created a new environment, to start developing code using this specific environment, you need to activate it in your terminal, so do so, use the activate command
conda activate pyramids
  • when you activate any environment you should see the environment name before the path of the current directory in your terminal.

Install packages

  • while writing your code, you found a new package that you need to use, you have to install it in your environment, to do so you need to make sure that you activated your environment, then use the install command
  • let's say we want to install the pyramids package for GIS functionalities and raster manipulation
conda install -c conda-forge pyramids
  • The pyramids package and all its dependencies will be listed to be downloaded and then installed
  • the package will be installed peacefully as it is the first package we install in the environment, incase there is conflict with other installed packages, conda will take some time to resolve the conflict.
  • Once the installation finishes you will see the following
  • To make sure the package is installed correctly, open python in your terminal and import the package
(pyramids) ubuntu@Mufasa:~$ python
Python 3.9.15 (main, Nov 24 2022, 14:31:59)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyramids
>>> pyramids.__dict__
{'__name__': 'pyramids', '__doc__': '\npyramids - GIS utility package\n', '__package__': 'pyramids'}
  • Now that I have used the -c flag I have to explain what are conda channels.

Conda Channels

  • Channels are locations where packages are stored and made available for installation. Conda allows you to add and use multiple channels, so you can install packages from a variety of sources.
  • There are several default channels that are included with conda, such as the Anaconda repository (which contains a large collection of pre-built and pre-configured packages for data science and machine learning), and the default channel for the Python Package Index (PyPI), which is the main repository for Python packages.
Conda-forge
  • You can also add additional channels, such as custom channels provided by your organization, or third-party channels that contain specialized or experimental packages.
  • To show the channels in your environment use the config command.
conda config -- show channels
  • To add a specific channel (for example conda-forge)
conda config -- add channels conda-forge
  • Now when you try to install any package without specifying a channel, conda will search for the package in the default channels, but if you specified a channel, it will search for the package only in this channel.
  • To remove a channel from your environment
conda config - remove channels conda-forge

Uninstall packages

  • To remove any package we can use the uninstall command, as follows
conda uninstall numpy
  • but using the previous command will remove numpy and all the packages that have numpy as a dependency.
  • Sometimes you want to uninstall a certain package from conda and reinstall it from pip, one of the ways to do so is to use the — force flag (double dashes) in order to remove numpy only without the dependent packages
conda uninstall numpy --force
  • make sure that the listed packages to be removed has only the package you want to remove (in our case here that is numpy), then type yes.

Update packages

  • To update a specific package, use the update command followed by the package name
conda update pyramids

So far we have installed miniconda, added conda to the PATH environment variable, created an environment and activated it in the terminal, installed packed and updated it, in the next article we will make some improvements to the environment to reduce the size, remove unwanted downloaded packages and learn how to create an entire environment from the environment.yaml file

--

--