Anaconda is an open-source package manager, environment manager, and Python and R language distribution. It is bundled with over 1500 open-source packages for use on any platform. It is used for machine learning, data science, data processing, scientific computing and predictive analysis. Anaconda is available in both free and paid versions.

This tutorial will teach you how to install the Anaconda Python Distribution on Ubuntu 20.04 system. You will also learn how to set up Anaconda environments and how to configure them.

Prerequisites

  1. A server running Ubuntu 20.04.

  2. A non-root user with sudo privileges.

  3. Make sure everything is updated.

    $ sudo apt update
    $ sudo apt upgrade
    

Download Anaconda

Anaconda is not available in the official Ubuntu repository. Therefore, we will use the Anaconda bash script available from its official site for the installation.

Grab the latest version of the Anaconda for Python 3 from the official download page. At the time of writing, 2021.05 is the latest version available.

Download the installer using the following command.

$ curl https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh --output anaconda.sh

Run the following command to find the SHA-256 checksum of the installer.

$ sha256sum anaconda.sh
2751ab3d678ff0277ae80f9e8a74f218cfc70fe9a9cdc7bb1c137d7e47e33d53  anaconda.sh

We can use this hash to verify the integrity of the installer. Check the above value against the hashes available at the Anaconda with Python 3 on 64-bit Linux page for the version you downloaded.

Anaconda Hash Verification

Install Anaconda

Now that our hashes match run the following command to run the installer.

$ bash anaconda.sh

You will receive the following output.

Welcome to Anaconda3 2021.05

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>

Press Enter to continue and press Enter again to read through the license. Once you're done, type yes to accept the license terms.

Do you accept the license terms? [yes|no]
>>>

Next, you will be prompted for the location of the installation. Press Enter to default the location or specify a different one.

Anaconda3 will now be installed into this location:
/home/navjot/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/navjot/anaconda3] >>>

The installation process will start and will continue for some time. Once the installation is complete, you will be prompted again to initialize Anaconda 3.

Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]

Type yes to proceed. Anaconda will then proceed to make some changes to some of the directories for it to work. If the installation is successful, you will receive the following output.

==> For changes to take effect, close and re-open your current shell. <==

If you'd prefer that conda's base environment not be activated on startup,
   set the auto_activate_base parameter to false:

conda config --set auto_activate_base false

Thank you for installing Anaconda3!

===========================================================================

Activate the installation by sourcing the ~/.bashrc file.

$ source ~/.bashrc

You should see the following output.

(base) navjot@howtoforge:~$

This indicates the default base programming environment. You should create separate environments for your applications to keep them isolated from each other.

To verify your installation, you can run the following command to check the current setup information.

(base) navjot@howtoforge:~$ conda info

You should receive the following output.

     active environment : base
    active env location : /home/navjot/anaconda3
            shell level : 1
       user config file : /home/navjot/.condarc
 populated config files :
          conda version : 4.10.1
    conda-build version : 3.21.4
         python version : 3.8.8.final.0
       virtual packages : __linux=5.4.72=0
                          __glibc=2.31=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /home/navjot/anaconda3  (writable)
      conda av data dir : /home/navjot/anaconda3/etc/conda
  conda av metadata url : https://repo.anaconda.com/pkgs/main
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/navjot/anaconda3/pkgs
                          /home/navjot/.conda/pkgs
       envs directories : /home/navjot/anaconda3/envs
                          /home/navjot/.conda/envs
               platform : linux-64
             user-agent : conda/4.10.1 requests/2.25.1 CPython/3.8.8 Linux/5.4.72-microsoft-standard-WSL2 ubuntu/20.04.2 glibc/2.31
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False

Set up Anaconda Environments

Having separate virtual environments for your Python applications allow you to keep your projects use different Python versions and packages.

Let us check the different versions of Python available with the following command.

(base) navjot@howtoforge:~$ conda search "^python$"

You will receive a list of all Python 2 and 3 versions that you can use. For our tutorial purpose, we will stick to Python 3. It means you can only access packages compatible with Python 3. The above command uses a Regular expression search to look for Python versions where the ^ character specifies the start of the search term and $ the end of the term.

Create an environment using the latest version of Python. Run the following command to create a new Python 3 based environment named py3env.

(base) navjot@howtoforge:~$ conda create --name py3env python=3

The above command will download all the packages for the environment and prompts you for confirmation. Type y to finish creating the environment.

Activate the environment using the following command.

(base) navjot@howtoforge:~$ conda activate py3env

Your shell will reflect the newly created environment.

(py3env) navjot@howtoforge:~$  

Verify the version of Python.

(py3env) navjot@howtoforge:~$ python --version
Python 3.10.0

To deactivate your environment, use the following command.

(py3env) navjot@howtoforge:~$ conda deactivate

To activate another environment with a different python version, use the following command.

(base) navjot@howtoforge:~$ conda create -n py37env python=3.7

You can check all the available environments using the following command.

(base) navjot@howtoforge:~$ conda info --envs
# conda environments:
#
base                  *  /home/navjot/anaconda3
py37env                  /home/navjot/anaconda3/envs/py37env
py3env                   /home/navjot/anaconda3/envs/py3env 

The asterisk (*) indicates the currently active environment.

Use the following command to remove an existing environment.

(base) navjot@howtoforge:~$ conda --name py37env --all

Anytime you want to return to the `base` environment, use the conda activate command without specifying any environment. Using conda deactivate is not recommended since if you run that command from inside the `base` environment, you may lose the ability to run Conda temporarily.

Configure Conda Environments

Switch to the py3env environment created above.

(base) navjot@howtoforge:~$ conda activate py3env

Every time you create a new environment, conda installs several packages by default. You can get a list of them by using the following command.

(py3env) navjot@howtoforge:~$ conda list

You will get the following output.

# packages in environment at /home/navjot/anaconda3/envs/py3env:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main
_openmp_mutex             4.5                       1_gnu
bzip2                     1.0.8                h7b6447c_0
ca-certificates           2021.9.30            h06a4308_1
certifi                   2020.6.20          pyhd3eb1b0_3
ld_impl_linux-64          2.35.1               h7274673_9
libffi                    3.3                  he6710b0_2
libgcc-ng                 9.3.0               h5101ec6_17
libgomp                   9.3.0               h5101ec6_17
libstdcxx-ng              9.3.0               hd4cf53a_17
libuuid                   1.0.3                h7f8727e_2
ncurses                   6.2                  he6710b0_1
openssl                   1.1.1l               h7f8727e_0
pip                       21.2.4          py310h06a4308_0
python                    3.10.0               h12debd9_1
readline                  8.1                  h27cfd23_0
setuptools                58.0.4          py310h06a4308_0
sqlite                    3.36.0               hc218d9a_0
tk                        8.6.11               h1ccaba5_0
tzdata                    2021a                h5d7bf9c_0
wheel                     0.37.0             pyhd3eb1b0_1
xz                        5.2.5                h7b6447c_0
zlib                      1.2.11               h7b6447c_3

If you want to install an additional package, use the following command. Here we are installing the popular numpy package used for scientific computations which enable array operations, linear algebra and more.

(py3env) navjot@howtoforge:~$ conda install numpy

If you want to install a package in another environment when it is not active, you can do that in the following manner.

(py3env) navjot@howtoforge:~$ conda install --name py37env numpy

You can also install additional packages while creating a new environment.

(py3env) navjot@howtoforge:~$ conda create --name py3env1 python=3 numpy

To install multiple packages at once, use the following command.

(py3env) navjot@howtoforge:~$ conda install scipy curl

Anaconda can also be used to create environments to work with R language. Run the following command to create a new environment with R language by including the r-essentials and r-base packages.

(base) navjot@howtoforge:~$ conda create --name renv r-essentials r-base

Activate the newly created environment.

(base) navjot@howtoforge:~$ conda activate renv

List the packages in the newly created environment.

(renv) navjot@howtoforge:~$ conda list

Updating Anaconda

Updating Anaconda can be done with a couple of commands. The first step is to update the conda package.

(base) navjot@howtoforge:~$ conda update conda

When prompted, type y to proceed with the update.

The next step is to update the Anaconda distribution.

(base) navjot@howtoforge:~$ conda update anaconda

Type y to finish the update process.

You can update packages the same way. To update the numpy package, use the following command.

(base) navjot@howtoforge:~$ conda update numpy

Since we didn't specify the environment, it will update the package installed in the current environment. To update the package in a different environment, use the following command.

(base) navjot@howtoforge:~$ conda update --name py37env numpy

Uninstalling Anaconda

To uninstall Anaconda, you need to install the anaconda-clean module. This module will remove all the configuration files along with the Anaconda distribution.

(base) navjot@howtoforge:~$ conda install anaconda-clean

Type y to complete the installation.

Run the following command to run the anaconda-clean package. You will be prompted to enter y for every package.

(base) navjot@howtoforge:~$ anaconda-clean

To skip the confirmation prompts, use the following command instead.

(base) navjot@howtoforge:~$ anaconda-clean --yes

Type yes to finish uninstalling Anaconda. This commands also creates a backup in the ~/.anaconda_backup directory.

You need to remove the ~/anaconda3 directory to finish removing all the left-over files.

$ rm -rf ~/anaconda3

The final step is to remove the left-over code from the ~/.bashrc file. Open the file for editing.

$ nano ~/.bashrc

Scroll down to the end of the file and locate the following block of code.

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/navjot/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/navjot/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/navjot/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/navjot/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Delete the entire code and save the file by pressing Ctrl + X and entering Y when prompted.

Anaconda distribution is finally removed from your server. If you haven't deactivated the base programming environment, you can do it by logging out and logging back into your server.

Conclusion

This concludes our tutorial on installing and configuring Anaconda Python distribution on a Ubuntu 20.04 server. If you have any questions, post them in the comments below.

Share this page:

0 Comment(s)