3 minute read

Anaconda has the advantage of providing numerous packages necessary for data science, but it also has another advantage, above all, that packages and environments can be managed independently using Conda. This increases the efficiency of work in a Python environment where numerous package compatibility considerations are required.

How you can install the Anaconda on Ubuntu

Background 1 - Tips for Linux installation

I will mention the installation environment on the Ubuntu. Installing the Ubuntu is covered in many other blogs.

  • It is worth noting that there is a lot of discussion about how to partition partitions when using dual booting of the operating system.
  • Otherwise, it is also good to additionally allocate the swap area.
  • For dual booting, the boot loader installation location must be set when ubuntu is newly installed. If the window boot loader is already installed, install it there. However if the boot-loader installation location is not clear, select the ‘Ubuntu installation disk’ and set the ‘Ubuntu disk’ as the first boot priority to enable selective dual booting.

Background 2 - What is a shell? bash?

  • A ‘shell’ is a program that provides an interface between user and operating system. And it could be considered the command space.
  • A ‘shellscript’ is to arrange the ‘shell’ in text format and make us possible to execute it sequentially.
  • A ‘bash(Bourne-agin shell)’ is a kind of the shell
  • A ‘bashrc’ is a file that defines things to be referred when bash is operated.
  • Bash syntax
    • The code below is essentially included at the top of the script.
       #!/bin/bash
      
    • If the .sh file does not work, you need to change the permission with chmod
    • $ - syntax example Reference1

    Example 1

      #!/bin/bash
      for (( c=1; c<=5; c++ ))
      do  
         echo "Welcome $c times"
      done
    
      Welcome 1 times
      Welcome 2 times
      Welcome 3 times
      Welcome 4 times
      Welcome 5 times
    

    Example 2

      for s in server1 server2 server3
      do
          echo "Server ${s}: $(ssh vivek@${s} uptime)"
      done
    
      Server server1:  09:34:46 up 12 days, 21:57,  0 users,  load average: 0.08, 0.09, 0.09
      Server server2:  09:34:50 up 17 days,  2:30,  0 users,  load average: 0.03, 0.03, 0.00
      Server server3:  09:34:53 up 17 days,  2:31,  0 users,  load average: 0.04, 0.04, 0.00
    

Anaconda installation

$ wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
$ bash Anaconda3-2021.05-Linux-x86_64.sh

# After installation, check the installed version
$ conda --version

Anaconda environment setting ( Loading a virtual environment )

As mentioned above, the strength of Anaconda is that it can create a virtual environment on my computer and run a project which has specific settings. If you look at many Github repositories, you can see that envioronment.yml(or yaml) and setup_env.sh (for example) files are provided.

  • From the user’s point of view, in the bashscript for the setup, in general, we should reset to select the location where the env settings would be installed. (opening through any an editor)

  • And then run the below code.

    $ bash setup_env.sh
    

    One of the example code of setup_env.sh is shown below. (Reference)

      # Default RECIPE 'environment' can be overridden by 'RECIPE=foo setup.sh'
    RECIPE=${RECIPE:-environment.yml}
    # Default ENV_NAME 'hab' can be overridden by 'ENV_NAME=foo setup.sh'
    ENV_NAME="${ENV_NAME:-netd}"
    echo "Creating conda environment ${ENV_NAME}"
    
    if [[ ! $(type -P conda) ]]
    then
        echo "conda not in PATH"
        echo "read: https://conda.io/docs/user-guide/install/index.html"
        exit 1
    fi
    
    # If within the torralba lab NFS environment, set up dotconda directory.
    if [[ ! -e ${HOME}/.conda &&
          -e /data/vision/torralba/scratch2 &&
          ! -e /data/vision/torralba/scratch2/${USER}/dotconda ]]
    then
        mkdir -p /data/vision/torralba/scratch2/${USER}/dotconda
        ln -s /data/vision/torralba/scratch2/${USER}/dotconda -T ~/.conda
    fi
    
    if df "${HOME}/.conda" --type=afs > /dev/null 2>&1
    then
        echo "Not installing: your ~/.conda directory is on AFS."
        echo "Run this:"
        echo "mkdir /data/vision/torralba/scratch2/${USER}"
        echo "mv ~/.conda /data/vision/torralba/scratch2/${USER}/dotconda"
        echo "ln -s /data/vision/torralba/scratch2/${USER}/dotconda ~/.conda"
        echo "This will avoid using up your AFS quota."
        exit 1
    fi
    
    # Uninstall existing environment
    if [[ -e "${HOME}/.conda/envs/${ENV_NAME}" ]]
    then
     if [[ "$1" == "rebuild" ]]
     then
      echo "Deleting and rebuilding existing environment ${ENV_NAME}."
     else
      echo "Conda environment ${ENV_NAME} already exists."
      echo "Use '$0 rebuild' if you want to delete it and rebuild."
      exit 1
     fi
    fi
    

    Therefore, if you run this bashscript, ‘netd’ environment will be installed on the below location.

    ${HOME}/.conda/envs/${ENV_NAME}
    
  • You can check that new virtual-environment(LOADED-VIRENV) is created as below. Yellow-shaded area represents a name of the new envrionment.
    conda env list
    

    Screenshot from 2021-09-13 10-07-07

  • And then activate the ‘loaded environment’ as below.

    $ conda activate LOADED-VIRENV
    #LOADED-VIRENV's name can be checked on the 'setup_env.sh'
    

    if you want to deactivate the virtual environment, run the below code.

    $ conda deactivate
    
  • If you want to check more command related to Anaconda, i recommend this Site