How To Set up and Use Anaconda on Ubuntu 22.04

In this guide from the Linux Tutorials, we want to teach you How To Set up and Use Anaconda on Ubuntu 22.04.

Anaconda is an open-source distribution of the Python and R programming languages for data science that aims to simplify package management and deployment. Package versions in Anaconda are managed by the package management system, conda, which analyzes the current environment before executing an installation to avoid disrupting other frameworks and packages.

How To Set up and Use Anaconda on Ubuntu 22.04

To set up Anaconda, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 22.04.

When you are done with this, you can start to install Anaconda on Ubuntu 22.04.

Install Anaconda on Ubuntu 22.04

To install Anaconda on a Ubuntu 22.04 server, you should download the latest Anaconda installer bash script, verify it, and then run it.

First, you need to update your local package index with the following command:

sudo apt update

Then, Go to the Anaconda Distribution Page. Search for the latest version of Anaconda for Python 3. and copy the link address of it.

It’s recommended to change your directory to the /tmp with the following command:

cd /tmp

Next, install the curl tool that you can download the Anaconda bash script:

sudo apt install curl

Now, use curl to download the link that you have copied from the Anaconda website:

curl -O https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh

After you download the Anaconda installer, you can run the script with the following command:

bash Anaconda3-2022.05-Linux-x86_64.sh

In your output you will see:

Output
Welcome to Anaconda3 2022.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 to read through the license. then, press yes to approve the license terms.

At this point, you will be asked to choose the location of the installation. you can press Enter to accept the default or specify a different location to modify it.

Output
Anaconda3 will now be installed into this location:
/root/anaconda3

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

[/root/anaconda3] >>>

When your installation is completed, you will see:

Output
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> yes

Type yes that you don’t want to add Anaconda to the PATH manually.

Here you need to activate the installation by sourcing the ~/.bashrc file:

source ~/anaconda3/bin/activate

You will be in Anaconda’s base programming environment which is named base.

Your prompt will change similar to this:

(base) [email protected]:~#

Now you can run the conda init command to initialize your environment:

(base) [email protected]:~# conda init

Then, you can verify your installation with the following command:

(base) [email protected]:~# conda list
# packages in environment at /root/anaconda3:
#
# Name                  Version  Build        Channel
_ipyw_jlab_nb_ext_conf  0.1.0    py38_0
_libgcc_mutex           0.1      main
alabaster               0.7.12   pyhd3eb1b0_0
anaconda                2021.05  py38_0
...

As you can see in your output you will receive all the available packages through the Anaconda installation.

When you have finished installing Anaconda on Ubuntu 22.04, you can set up Anaconda environments.

Set up Anaconda Environments on Ubuntu 22.04

Anaconda environments allow you to keep projects organized by Python versions and the packages needed.

First, you need to check which versions of Python are available for you to use with the following command:

(base) [email protected]:~# conda search "^python$"

In your output, you will see the different versions of Python. In this article, because you use the Anaconda with Python 3, you will have access only to the Python 3 versions of packages.

Here you can create an Anaconda environment using Python 3 on Ubuntu 22.04. Here we name the environment “my_env”, you can replace it with your own name.

To do this run the following command:

(base) [email protected]:~# conda create --name my_env python=3

Type Y to complete the creation of your environment.

You need to activate your new environment with the following command:

(base) [email protected]:~# conda activate my_env

After this, your command prompt will look like this:

(my_env) [email protected]:~#

Verify which versions of Python you use with the following command:

(my_env) [email protected]:~# python --version
Output
Python 3.10.4

Deactivate your Anaconda environment on Ubuntu 22.04 with the following command:

(my_env) [email protected]: conda deactivate

Also, you can create an Anaconda environment with a specific version of Python. for example we use Python 3.7:

(base) [email protected]:~# conda create -n my_env37 python=3.7

Then, activate it with the following command:

(base) [email protected]:~# conda activate my_env37

Your command prompt will look like this:

(my_env37) [email protected]:~#

Now update your version of Python along the same branch within a respective environment with the following command:

(my_env37) [email protected]:~# conda update python

Deactivate your Anaconda environment on Ubuntu 22.04 with the following command:

(my_env37) [email protected]:~# conda deactivate

Here you can list all of the environments that you have set up with the following command:

(base) [email protected]:~# conda info --envs
Output
# conda environments:
#
base /root/anaconda3
my_env /root/anaconda3/envs/my_env
my_env37 * /root/anaconda3/envs/my_env37

Every environment you create with conda create will come with several default packages like openssl, Python, wheel, etc.

You can add additional packages like NumPy with the following command:

(base) [email protected]:~# conda install --name my_env37 numpy

If you would like a numpy environment upon creation, you can target it in your conda create command:

(base) [email protected]:~# conda create --name my_env python=3 numpy

If you are not working on a specific project, you can remove the environment with the following command:

(base) [email protected]:~# conda remove --name my_env37 --all

List the available environments with the following command:

(base) [email protected]:~# conda info --envs

In your output, you will see that the my_env37 environment does not exist:

Output
# conda environments:
#
base * /root/anaconda3
my_env /root/anaconda3/envs/my_env

Let’s see how to update the Anaconda on Ubuntu 22.04.

How To Update the Anaconda

At this point, you need to be sure that Anaconda is up to date and working with all the latest package releases.

First, you should update the conda utility with the following command:

(base) [email protected]:~# conda update conda

Then, you can update the Anaconda distribution with the following command:

(base) [email protected]:~# conda update anaconda

With this command, you will be sure that you are using the latest releases of conda and Anaconda.

Here you can uninstall Anaconda on Ubuntu 22.04.

How To Uninstall Anaconda

If you don’t want to use Anaconda, you can uninstall it by following these steps.

First, you need to deactivate the base Anaconda environment with the following command:

(base) [email protected]:~# conda deactivate

Next, you need to install an Anaconda module on Ubuntu 22.04 that removes the configuration files. To install it run the following command:

conda install anaconda-clean

You can now remove your entire Anaconda directory on Ubuntu 22.04 by entering the following command:

rm -rf ~/anaconda3

Finally, you need to remove the PATH line from your .bashrc file. To do this, open the file with your favorite text editor, here we use vi:

vi ~/.bashrc

Then, search for the conda initialize part and comment on all of the lines with # in this section like this:

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

When you are finished, save and close the file.

Anaconda is now removed from your Ubuntu 22.04 server.

Conclusion

At this point, you learn to Set up Anaconda on Ubuntu 22.04. Also, you can use conda, a package, and an environment manager to create environments for isolating our projects that use different versions of Python and/or different versions of packages. and you can also use it to install, uninstall, and update packages in your project environments.

Hope you enjoy this article about How To Set up and Use Anaconda on Ubuntu 22.04.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular