How To Install Anaconda Python on Ubuntu 18.04 LTS


Introduction

Anaconda is an open-source environment and package manager that is designed to handle machine learning and data science related workflows. It also helps in the distribution of programming languages such as Python and R.

With over 1000 different data science packages; Anaconda helps in the processing of data in large-scale, predictive analytics, and scientific computing. You can get this package in both free and paid versions.

This article will take you through the steps of installing the Anaconda Python 3 version on a server running on Ubuntu 18.04 set up with a Python hosting service (i.e., a host that supports Python).

Step 1 - Installing Anaconda

The first step is to download the latest stable version of Anaconda. Go to Anaconda Downloads page and download the latest Anaconda version for Python 3. The current version is 5.2 at the time of writing this article. Check the link above to get the latest version available.

Now, go to your server and shift to the /tmp directory where you can download ephemeral items such as the Anaconda bash script.

$ sudo cd /tmp

Using curl, download the link you copied earlier from the Anaconda website:

$ sudo curl -O
https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh

Now, you can verify the installer’s data integrity with cryptographic hash verification using the SHA-256 checksum:

$ sudo sha256sum Anaconda3-5.2.0-Linux-x86_64.sh

This will give you an output like the one below:

09f53738b0cd3bb96f5b1bac488e5528df9906be2480fe61df40e0e0d19e3d48  Anaconda3-5.2.0-Linux-x86_64.sh

Check to see whether your output is part of the hashes available at Anaconda with Python 2 on 64-bit Linux page for the right version of Anaconda. If it matches the hash on this page, you can proceed to the next step.

Now, go ahead and run the script:

$ sudo bash Anaconda3-5.2.0-Linux-x86_64.sh

You should see the output below:

Welcome to Anaconda3 5.2.0

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

Select ENTER button to proceed and then click ENTER to read the license terms. You will be asked to approve these terms:

Do you approve the license terms? [yes|no]

If you agree with the terms, type yes.

Now, choose the ideal location for the installation. Press ENTER to accept the location of your choice or select another location that you wish to include:

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

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

[/home/hostadvice/anaconda3] >>>

Now, the installation of Anaconda will resume. Wait for a while until the process is complete.

After the installation is done, you should see the output below:

...
installation finished.
Do you wish the installer to prepend the Anaconda3 install location
toPATHin your /home/hostadvice/.bashrc ? [yes|no]
[no] >>>

Enter yes to allow to use the conda command. You should see the output below:

Appending source /home/hostadvice/anaconda3/bin/activate to /home/sammy/.bashrc
A backup will be made to: /home/hostadvice/.bashrc-anaconda3.bak
...

After this, you will be prompted to either download Visual Studio Code or not.

You can choose to download this editor by responding with a yes or no.

Anaconda is partnered with Microsoft! Microsoft VSCode is a streamlined
code editor with support for development operations like debugging, task
running and version control.

To install Visual Studio Code, you will need:
  - Administrator Privileges
  - Internet connectivity

Visual Studio Code License: https://code.visualstudio.com/license

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>>

Go ahead and activate your installation by sourcing the file ~/.bashrc:

$ sudo source ~/.bashrc

After that, go ahead and verify the installation using the conda command:

$ sudo conda list

You should see the output below of all the available packages on Anaconda:

# packages in environment at /home/sammy/anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0            py36he11e457_0  
alabaster                 0.7.10           py36h306e16b_0  
anaconda                  5.2.0                    py36_3  
...

Once Anaconda is successfully installed, we can go ahead and set up Anaconda environments.

Step 2 - Configuring Anaconda Environments

By creating virtual environments for Anaconda, it’s easier to keep organized projects based on Python versions and packages. You can specify the exact version of Python used for each Anaconda environment you create and keep all these files together in one directory.

Start by checking the available Python versions you can use:

$ sudo conda search "^python$"

This will present a list of different Python versions including both Python 2 and Python 3 versions. For this article, we will use the Python 3 packages.

To create a new environment for Python 3, we start by assigning the version to python argument. We can name the environment my_env but you can use any name for the environment.

$ sudo conda create--name my_env python=3

You will be presented with the appropriate information on what version is downloaded and the packages installed. You will also be asked to confirm by typing yes or no.

Now, the conda command will collect all the packages for the virtual environment and alert you once the process is done.

To activate your environment, type command below:

$ sudosource activate my_env

Once you have activated the environment, the prefix of your command will change as follows:

(my-env) hostadvice@ubuntu:~$

Now, confirm that you’re using the right Python version

(my-env) hostadvice@ubuntu:~$ python --version

The output should look like this:

Python 3.7 :: Anaconda, Inc.

To deactivate the Anaconda environment, type the info below:

(my-env) hostadvice@ubuntu:~$ source deactivate

On the information above, you can decide to replace the word source and include . and you will get the same results.

You can specify the Python version you want to use to the python argument:

$ conda create -n my_env35 python=3.5

Use the command below to update your Python version to a newer version:

(my-env35) hostadvice@ubuntu:~$ conda update python

To target a specific Python version, pass it to the python argument like python=3.3.2.

To check all the environments that have already been set to run the command below:

$ sudo conda info --envs

You should get the output below:

# conda environments:
#
base                  *  /home/hostadvice/anaconda3
my_env                   /home/hostadvice/anaconda3/envs/my_env
my_env35                 /home/hostadvice/anaconda3/envs/my_env35

Each environment created with conda command will present some default packages as shown below:

openssl
pip
python
readline
setuptools
sqlite
tk
wheel
xz
Zlib

If you want, you can create other additional packages for your environment. For instance, for numpy, you can use the command below:

$ sudo conda install--name my_env35 numpy

To create an environment for this, use the command below:

$ sudo conda create--name my_env python=3 numpy

If you want to remove this project, run the command below:

$ sudo conda remove--name my_env35 --all

Step 3 - Updating Anaconda

Make sure Anaconda is always up-to-date to receive the latest packages available.

But first, ensure the conda utility is updated:

$ sudo conda update conda

Type yes to continue with the update.

After the update for conda utility is done, update the Anaconda distribution:

$ conda update anaconda

When asked to confirm, type yes to continue.

At this point, you should have the latest versions of conda and Anaconda.

Step 4 - Uninstalling Anaconda

If you want to uninstall Anaconda from your system, you can start with the module anaconda-clean, to remove all the configuration files for Anaconda.

$ sudo conda install anaconda-clean

Type yes if asked to do so.

Now run the command below. You will be asked to answer with y before deleting each file.

Anaconda-clean

This command will create a new backup folder named .anaconda_backup in the home directory:

Backup directory: 
/home/hostadvice/.anaconda_backup/2018-05-23T213826

Now, get rid of the whole Anaconda root directory by running the command below:

$ sudo rm -rf ~/anaconda3

Then, go to the .bashrc file on Anaconda and remove the PATH line. To do so, open a text editor like nano:

$ sudo nano ~/.bashrc

Search for the Anaconda file from the list. Now, delete it or uncomment the export PATH line:

/home/hostadvice/.bashrc

...
# added by Anaconda3 installer
export PATH="/home/hostadvice/anaconda3/bin:$PATH"

Once you’re done making the changes, exit and type y to save the changes.

Now, Anaconda is completely removed from your server.

Conclusion

At this point, you should know how to install Anaconda using the conda command utility to set up environments, perform updates, and delete Anaconda if you don’t need it. Now, you can comfortably manage large-scale data and workloads associated with data science as well as scientific computing.

Check out these top 3 Linux hosting services

Was this article helpful?