Sunday, September 3, 2023

How to setup Conda, Create an environment with Python and Activate the environment

Agenda: How to setup Conda, Create an environment with Python and Activate the environment

Purpose: I needed different versions of python for different application, so I thought I use conda to manage that.

In this blog, we will see how to install conda and then how to create an environment with python 3.11& activate it as well.


Reference url: https://docs.conda.io/projects/miniconda/en/latest/

1) Setup Conda

Goto the VM
Navigate to the relevant directory

Steps from reference url:

mkdir -p ./miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ./miniconda3/miniconda.sh
bash ./miniconda3/miniconda.sh -b -u -p ./miniconda3
rm -rf ./miniconda3/miniconda.sh


./miniconda3/bin/conda init bash

#skip#~/miniconda3/bin/conda init zsh

Actual output:

[vagrant@localhost app]$ df -h .
Filesystem                           Size  Used Avail Use% Mounted on
/dev/mapper/appdata--vg-appdata--lv  3.9G   60M  3.8G   2% /u01
[vagrant@localhost app]$ pwd
/u01/app
[vagrant@localhost app]$ mkdir -p ./miniconda3
[vagrant@localhost app]$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ./miniconda3/miniconda.sh
--2023-09-03 12:50:54--  https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
Resolving repo.anaconda.com (repo.anaconda.com)... 104.16.131.3, 104.16.130.3, 2606:4700::6810:8303, ...
Connecting to repo.anaconda.com (repo.anaconda.com)|104.16.131.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 103219356 (98M) [application/x-sh]
Saving to: ‘./miniconda3/miniconda.sh’
./miniconda3/miniconda.sh      100%[==================================================>]  98.44M  8.61MB/s    in 11s
2023-09-03 12:51:06 (8.66 MB/s) - ‘./miniconda3/miniconda.sh’ saved [103219356/103219356]

[vagrant@localhost app]$ ls -altr
total 0
drwxr-xr-x. 3 vagrant vagrant 17 Sep  3 12:35 ..
drwxrwxr-x. 3 vagrant vagrant 24 Sep  3 12:50 .
drwxrwxr-x. 2 vagrant vagrant 26 Sep  3 12:50 miniconda3

[vagrant@localhost app]$ ls -altr miniconda3/
total 100804
-rw-rw-r--. 1 vagrant vagrant 103219356 Jul 13 19:01 miniconda.sh
drwxrwxr-x. 3 vagrant vagrant        24 Sep  3 12:50 ..
drwxrwxr-x. 2 vagrant vagrant        26 Sep  3 12:50 .

[vagrant@localhost app]$ bash ./miniconda3/miniconda.sh -b -u -p ./miniconda3
PREFIX=/u01/app/miniconda3
Unpacking payload ...
Installing base environment...

Downloading and Extracting Packages

Downloading and Extracting Packages
Preparing transaction: done
Executing transaction: done
installation finished.

[vagrant@localhost app]$ ls -altr
total 4
drwxr-xr-x.  3 vagrant vagrant   17 Sep  3 12:35 ..
drwxrwxr-x.  3 vagrant vagrant   24 Sep  3 12:50 .
drwxrwxr-x. 18 vagrant vagrant 4096 Sep  3 12:52 miniconda3

[vagrant@localhost app]$ ls -altr miniconda3
total 100876
-rw-r--r--.  1 vagrant vagrant     10732 Jul 13 18:43 LICENSE.txt
-rw-rw-r--.  1 vagrant vagrant 103219356 Jul 13 19:01 miniconda.sh
drwxrwxr-x.  3 vagrant vagrant        24 Sep  3 12:50 ..
drwxrwxr-x.  3 vagrant vagrant        17 Sep  3 12:51 x86_64-conda-linux-gnu
drwxrwxr-x.  3 vagrant vagrant        17 Sep  3 12:51 x86_64-conda_cos7-linux-gnu
drwxrwxr-x.  3 vagrant vagrant       146 Sep  3 12:52 ssl
drwxrwxr-x.  4 vagrant vagrant        30 Sep  3 12:52 man
drwxrwxr-x. 16 vagrant vagrant      4096 Sep  3 12:52 share
drwxrwxr-x.  2 vagrant vagrant      4096 Sep  3 12:52 sbin
drwxrwxr-x.  2 vagrant vagrant        30 Sep  3 12:52 compiler_compat
drwxrwxr-x. 26 vagrant vagrant      8192 Sep  3 12:52 include
drwxrwxr-x. 19 vagrant vagrant      8192 Sep  3 12:52 lib
drwxrwxr-x.  4 vagrant vagrant        35 Sep  3 12:52 etc
drwxrwxr-x.  3 vagrant vagrant        22 Sep  3 12:52 shell
drwxrwxr-x.  2 vagrant vagrant        19 Sep  3 12:52 condabin
drwxrwxr-x.  2 vagrant vagrant      4096 Sep  3 12:52 bin
drwxrwxr-x.  2 vagrant vagrant      4096 Sep  3 12:52 conda-meta
drwxrwxr-x. 73 vagrant vagrant      8192 Sep  3 12:52 pkgs
drwxrwxr-x.  2 vagrant vagrant         6 Sep  3 12:52 envs
drwxrwxr-x. 18 vagrant vagrant      4096 Sep  3 12:52 .

[vagrant@localhost app]$ rm -rf ./miniconda3/miniconda.sh
[vagrant@localhost app]$ pwd
/u01/app
[vagrant@localhost app]$ ./miniconda3/bin/conda init bash
no change     /u01/app/miniconda3/condabin/conda
no change     /u01/app/miniconda3/bin/conda
no change     /u01/app/miniconda3/bin/conda-env
no change     /u01/app/miniconda3/bin/activate
no change     /u01/app/miniconda3/bin/deactivate
no change     /u01/app/miniconda3/etc/profile.d/conda.sh
no change     /u01/app/miniconda3/etc/fish/conf.d/conda.fish
no change     /u01/app/miniconda3/shell/condabin/Conda.psm1
no change     /u01/app/miniconda3/shell/condabin/conda-hook.ps1
no change     /u01/app/miniconda3/lib/python3.11/site-packages/xontrib/conda.xsh
no change     /u01/app/miniconda3/etc/profile.d/conda.csh
modified      /home/vagrant/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
[vagrant@localhost app]$ ls -altr /home/vagrant/.bashrc
-rw-r--r--. 1 vagrant vagrant 843 Sep  3 12:52 /home/vagrant/.bashrc
[vagrant@localhost app]$ cat /home/vagrant/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/u01/app/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/u01/app/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/u01/app/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/u01/app/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
[vagrant@localhost app]$


2) To create an environment:

reference url: https://docs.conda.io/projects/conda/en/stable/commands/create.html

>>> open a new putty terminal

conda create -n azcli python=3.11


Actual output:

(base) [vagrant@localhost ~]$ conda create -n azcli python=3.11
Collecting package metadata (current_repodata.json): done
Solving environment: done

==> WARNING: A newer version of conda exists. <==
  current version: 23.5.2
  latest version: 23.7.3
Please update conda by running
    $ conda update -n base -c defaults conda
Or to minimize the number of packages updated during conda update use
     conda install conda=23.7.3


## Package Plan ##
  environment location: /u01/app/miniconda3/envs/azcli
  added / updated specs:
    - python=3.11

The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    openssl-3.0.10             |       h7f8727e_2         5.2 MB
    pip-23.2.1                 |  py311h06a4308_0         3.3 MB
    setuptools-68.0.0          |  py311h06a4308_0         1.2 MB
    ------------------------------------------------------------
                                           Total:         9.7 MB
The following NEW packages will be INSTALLED:
  _libgcc_mutex      pkgs/main/linux-64::_libgcc_mutex-0.1-main
  _openmp_mutex      pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
  bzip2              pkgs/main/linux-64::bzip2-1.0.8-h7b6447c_0
  ca-certificates    pkgs/main/linux-64::ca-certificates-2023.05.30-h06a4308_0
  ld_impl_linux-64   pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
  libffi             pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0
  libgcc-ng          pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
  libgomp            pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
  libstdcxx-ng       pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
  libuuid            pkgs/main/linux-64::libuuid-1.41.5-h5eee18b_0
  ncurses            pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
  openssl            pkgs/main/linux-64::openssl-3.0.10-h7f8727e_2
  pip                pkgs/main/linux-64::pip-23.2.1-py311h06a4308_0
  python             pkgs/main/linux-64::python-3.11.4-h955ad1f_0
  readline           pkgs/main/linux-64::readline-8.2-h5eee18b_0
  setuptools         pkgs/main/linux-64::setuptools-68.0.0-py311h06a4308_0
  sqlite             pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
  tk                 pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
  tzdata             pkgs/main/noarch::tzdata-2023c-h04d1e81_0
  wheel              pkgs/main/linux-64::wheel-0.38.4-py311h06a4308_0
  xz                 pkgs/main/linux-64::xz-5.4.2-h5eee18b_0
  zlib               pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0

Proceed ([y]/n)? y

Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate azcli
#
# To deactivate an active environment, use
#
#     $ conda deactivate
(base) [vagrant@localhost ~]$


3. To activate an environment:

conda activate azcli

Actual output:

(base) [vagrant@localhost ~]$ conda activate azcli
(azcli) [vagrant@localhost ~]$

(azcli) [vagrant@localhost ~]$ python --version
Python 3.11.4
(azcli) [vagrant@localhost ~]$


YouTube video: 



Thanks

No comments:

Post a Comment

Flashback data archive steps

 Objective: Ways to track DML changes in a table Detailed objective: We should be able to track the changes in the table at any point in tim...