Installing the Anaconda Python Distribution on Linux

Installation Video

Introduction

Python beginners are recommended to install Anaconda opposed to installing Python.

What is Anaconda and what is the difference? Anaconda is a Python distribution. The Anaconda Python distribution contains the core Python programming language and its inbuilt modules, the Spyder and JupyterLab Integrating Development Environments (IDEs), and the most common libraries or modules for data science such as numpy, scipy, pandas, matplotlib and seaborn as well as a multitude of others. IDEs can be thought as programs for working with Python code, similar to how one might use Libre Office or Microsoft Word to work with a document.

Why have a Python distribution? Anaconda performs a number of checks for incompatibilities when the user attempts to install additional Python modules. Such compatibilities arise when one module is dependent on another. The seaborn library for example is used for data visualization. Under the hood it uses the matplotlib plotting library. matplotlib itself is dependent on numpy which is used for array manipulation and will only install if a specific version of numpy is installed. When the user blindly installs the latest version of Python and then numpy and then matplotlib and then seaborn incompatibilities are very likely to arise. If an underlying object in numpy is changed or deprecated, this can break the functionality of matplotlib which can in turn break the functionality of seaborn and so on and so forth. This can lead to endless frustrations particularly for a new user who doesn't yet know what they are doing. Such frustrations are alleviated to a great extent by use of Anaconda.

Installation on Linux requires a bash install of a .sh via the terminal and will work in popular Linux distributions such as Ubuntu, Mint, Fedora, KDE Neon and DeepIn. Once installed there are no shortcuts on the applications menu and the IDEs and Anaconda Navigator have to be ran by typing a command in the terminal. The most common installation issue on Linux is that Anaconda is installed but the user has skipped the initialization prompt and therefore cannot launch anything via the terminal. I have included instructions for such a scenario. Additional issues may arise if is an old Anaconda installation is present. The uninstaller does not purge the system of all the old configuration files which may reintroduce a problem after a reinstallation if they are problematic. I have included additional instructions to uninstall and purge an old installation in this scenario.

Uninstalling Anaconda and Kite

Before installing Anaconda, you should remove any old Kite and Anaconda installation to prevent conflicts. Click show more for more details. Skip this section if on a clean install.

Open up files and go to your Home folder:

Select View and then Show Hidden Files:

Uninstalling Kite

Go to the .local folder:

Go to the share subfolder:

Go to the Kite subfolder:

Right click uninstall and rename to uninstall.sh:

Right click an empty space in the folder and select open in terminal:

Type in:

bash uninstall.sh

Close the Terminal.

Uninstalling Anaconda

Delete the following folders anaconda3, .anaconda, .cache, .conda, .ipython and .kite. Also delete the .condarc file:

Next go to the .config folder:

Delete the Kite, matplotlib and spyder-py3 folders:

Return to the Home folder and right click the .bashrc file and select Open with Text Editor:

Press [Ctrl] + [ f ] to open up find and search, look for conda. If the search results come up positive delete the conda initalize:

Then save the file:

You may wish to hide the hidden files again:

The old installation of Anaconda is now uninstalled:

Installation

Installing Anaconda

Go to the Anaconda website:

Scroll down to the bottom of the page and select the Linux installer.

Select Save File and then OK:

Wait for the Download to complete:

Go to your Downloads folder, right click an empty space and select Open in terminal:

In the downloads folder right click the .sh file and copy the file name including the extension. Type in bash and then a space and paste in the file name.

For example:

bash Anaconda3-2020.11-Linux-x86_64.sh

Then press [↵]

Next the license agreement will display. Annoyingly it will require you to press [↵] to proceed to each new line. Hold down [↵] to rapidly scroll down to the end of the license agreement.

Then type in:

yes

And then press [↵] to accept the agreement and proceed with the install.

Type in [↵] to install Anaconda in the default location:

Initializing Anaconda During Installation

During the install you will be asked whether you wish to initialize Anaconda. This will add the conda commands to the bash-rc file.

To initialize Anaconda during installation type in:

yes

Then [↵] to run conda init.

Anaconda is now installed and the .bashrc file is updated to include conda commands:

Testing the Anaconda Installation

Close and reopen the terminal to refresh the terminal with the updated bash-rc containing the conda commands. Type in the following command to test:

conda

If you get information about the conda command, everything is successfully installed.

If you get the following error message "Conda: command not found" then you haven't initialized your conda environment within your bash-rc file and must do so manually.

Manually Initializing Anaconda

If the conda command above works then ignore this step.

If during installation you pressed [↵] at the prompt to run conda init then no (the default option) was automatically highlighted and a notification (that is also pretty easy to skim past) at the end of the installed stated:

You have chosen to not have conda modify your shell scripts at all. To activate conda's base environment in your current shell session:

eval "$(/home/philip/anaconda3/bin/conda shell.YOUR_SHELL_NAME hook)"

This means your .bash.rc file lacks the conda commands and therefore your terminal will not be able to use conda commands although Anaconda is installed. This is the most common installation issue of Anaconda on Linux.

To rectify this, go to your Home folder and view hidden files. Open up your .bashrc file in a text editor and copy and paste the following lines of code at the bottom of the file:

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

Replace my username philip with your username (4 occurrences):

Save the updated .bashrc file.

Close any open terminals and then open up a new terminal and try using the conda command again.

Updating Anaconda

To view a list of installed packages type in:

conda list

The list of displayed packages will display:

To update these packages to the latest stable versions type in:

conda update --all

To proceed type in:

y

Installing Additional Packages

The Anaconda Python distribution contains the most commonly used data science libraries (modules) but inevitably you will need to install additional modules as you begin to use Python for your desired application. Because you are using an Anaconda environment you should make an effort to use:

conda install package-to-be-installed

And avoid (where possible):

pip install package-to-be-installed

The conda command will in general check for any incompatibilities before installing the module and suggest updating several modules to result in a stable configuration where applicable. The pip command will install the module and not care about the consequences of installing such a module i.e. other modules breaking. Most modules will have a pip and conda install instructions and the conda instructions can normally be found on the anaconda website or the top of a google search for conda install package-to-be-installed. Some of the more obscure packages only have a pip install method. These more obscure packages are unlikely to be dependencies for commonly used modules and can generally be installed without breaking things.

Spyder 5 IDE

Installing Spyder 5

If updating Anaconda doesn't update Spyder to version 5.0.3 (or later). Type in the following command:

conda search --channel anaconda spyder

Version 5.0.3 isn't listed in the official anaconda channel so we must move to the conda-forge developer channel:

conda search --channel conda-forge spyder

Now version 5.0.3 is listed:

We can install it using:

conda install --channel conda-forge spyder=5.0.3

Note this must be done with the updated base anaconda environment (preferably on a clean install), indicated with the prompt beginning with (base). Creating a separate conda environment and trying to update spyder doesn't work unfortunately giving:

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

To proceed type in:

y

Spyder 5.0.3 will now be installed:

Launching Spyder

There are no Start Menu shortcuts for Spyder on Linux and we must use the Terminal to launch Spyder. To launch spyder type:

spyder

Installing the Kite Code Completion Engine

If using the Spyder IDE, you will need Kite for autocompletion to work correctly. Select Tools → Install Kite Completion Engine:

Select Install Kite:

Kite will now install:

Optionally input an email and select Continue:

You can test out Kite autocompletion by importing a Python inbuilt module such as datetime. Kite should recognise this as a Python module:

import datetime
datetim

You can use the dot notation to call objects from the module, kite should list the objects which can be called from the module, usually referencing the most commonly used objects first, then all the objects alphabetically listed with captilized objects (such as classes) listed separately before lower case objects (such as functions and instances):

import datetime
datetime.

If a class or function is typed, kite will recognize it as an object.

import datetime
datetime.datetime

To view the input arguments for the class or function, type it followed by open parenthesis:

import datetime
datetime.datetime(

To view the tooltip in the help pane, highlight the function or class and press [Ctrl] + [ i ] (i for inspect):

Unfortunately Kite autocompletion is immensely slow for numpy, pandas and matplotlib. Typing the two lines for each library requires about a 30s – 1 minute for the tooltip to display on my machine. This is due to be addressed in later Spyder versions:

import numpy as np
np.
import pandas as pd
pd.
import matplotlib.pyplot as plt
plt.

Spyder Settings

Select Tools and then Preferences:

If you prefer the light theme, select Appearance. Under Syntax Highlighting Theme change Spyder Dark to Spyder:

It can be helpful to change the Editor settings to show indent guide and blank spaces. Select Editor and enable these options:

Once you have changed your selected options. select Apply and then select yes to restart Spyder

Spyder will relaunch with the new settings applied:

Run Settings

Let's create a variable in the script editor:

a = 1

We can then select the run button to run the script. At first launch the run dialog will display, it is recommended to check Run in consoles namespace instead of an empty one:

These options can later be accessed by going to Tools→Preferences→Run. Note to apply changes in the Run settings, the Kernel will have to be reset by selecting Kernel→Restart Kernel or closing and relaunching Spyder.

The Variable Explorer

Spyder has the best Python variable explorer. It can be accessed by selecting the variable explorer tab.

Objects in the variable explorer can be accessed in the consoles if the run settings run in the consoles namespace. This is generally done to test a line of code quickly.

More complicated objects such as nested collections can be accessed by double clicking them

b = [ [1, 2, 3], ["a", "b"] ]

Spyder Plot Settings

We can put together a very basic test script to create a plot:

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(start=0,stop=10,step=1)
y=np.arange(start=0,stop=10,step=1)
plt.figure()
plt.clf()
plt.plot(x,y)
plt.show()

By default plots will display inline within the plots pane.

The matplotlib backend can be changed to automatic after importing it in order to plot a graph in its own dedicated window by typing the following in the console:

%matplotlib auto

It can also be reverted to inline using:

%matplotlib inline

This change will be made only for the current session and will revert to a default setting once the kernal has been restarted. To change the default setting, go to Tools and Preferences:

To the left hand side, select IPython console. Select the graphics tab. Change the backline to either automatic or inline. Then select Apply:

Select Apply to all existing consoles and restart all kernels:

JupyterLab 3 IDE

Launching JupyterLab

There are no Start Menu shortcuts for JupyterLab on Linux and we must use the Terminal to launch JupyterLab. Before launching JupyterLab it is recommended to change your directory in the terminal by using the command cd

cd Documents

Or:

cd Documents/folder

To launch JupyterLab, type in:

jupyter-lab

JupyterLab File Explorer

JupyterLab will open as a new tab within your default browser:

To the left hand side is the file explorer, because we launched JupyterLab from the Documents folder we are in the Documents folder, although we could use the button to change directory if we wanted. Alternatively we can right click within the file explorer to create a new folder:

JupyterLab will open with a new launcher. Additional launchers can be created with the + button. Additional will launch new subtabs.

Select Notebook:

A new Notebook will open:

The notebook can be renamed within JupyterLabs file explorer or by right clicking the subtab:

The file will show up within the native Linux file explorer but it is not opened up by double clicking the file. JupyterLab must always be launched within the terminal and existing notebooks can be opened using the File menu, the file menu has the usual additional options such as save, save as and close:

Notebook Code Blocks

JupyterLab by default uses code cells:

Each cell can be ran by pressing the run button. The shortcut key to run the currently highlighted cell is [Shift] + [Enter]:

JupyterLab Code Completion Engine

JupyterLab has it's own code completion. Typing in:

import numpy as np
np.

Won't display anything like Spyder with Kite. You have to type [Tab] after the dot to view a list of objects which can be accessed from the module numpy (alias np).

To view a docstring for a function or class as a tooltip. Type in the function or class name followed by a [Shift] and [Tab]:

The docstring can be output to a cell by typing in ? before the class or function name:

The cell output works similar to an individual console in Spyder. Unassigned output is printed:

Assigned output is instead stored to the object and not printed:

JupterLab Plot Settings

Plots by default are inline and outputted to a cell:

The plot backend can be changed to auto to display the plot as a dedicated window, similar to the option in Spyder.

Note code cells can be rerun. The number to the left hand side of the cell shows the order they were run it. Note the cell to generate the plot has changed from 10 when it was an inline plot to 12 and is now an automatic plot. To run a middle cell and add an empty cell below it you can use the shortcut key [Alt]+[Enter]. [Shift]+[Enter] will only run the cell.

To close JupyterLab, close the tab in the browser and press [Ctrl]+[C] in the terminal.

The Anaconda Navigator

Launching the Anaconda Navigator

There are no Start Menu shortcuts for Anaconda on Linux and we must use the Terminal to launch the Anaconda Navigator. To launch the Anaconda Navigator type in:

anaconda-navigator

From the Anaconda Navigator one can use the tiles to launch Python IDEs such as Spyder and JupyterLab (these can be launched directly from the terminal as seen above).

I have not used all of the IDEs but essentially Jupyter Notebook can be considered as an old version of JupyterLab and QTConsole is analogues to working only within the script editor of Spyder.

Anaconda Navigator – High DPI and Zoom Problem

The Anaconda Navigator has a setting "Enable High DPI Scaling" which is supposed to scale it properly on a High DPI screen. This is enabled by default and unfortunately it does not work well on high DPI screens which use a zoom of 150 %-200 % such as newer Dell XPS models and the tiles may be too large with only a fraction of the Anaconda Navigator displaying.

To get around this, the JupyterLab and Spyder IDEs can either be launched directly from the terminal.

To resolve this, the High DPI Setting can be disabled in preferences.

Select File and then Preferences:

Look for "Enable high DPI scaling" and uncheck it:

On my XPS 13, the Preferences Window did not display properly and I could not access the checkbox or the Apply button. Press [Alt] + [F4] to close down the Anaconda Navigator.

This setting can be change manually by pasting in the following into the File Explorer Address Bar. Press [Ctrl} + [ l ] to toggle pasting into the address bar

home/.anaconda/navigator/

This will take you to the anaconda navigator configuration file which is anaconda-navigator.ini.

Right click anaconda-navigator and select Edit.

Here you will be able to change enable_high_DPI_scaling from True to False. Once done save the file:

Once this is done, the Anaconda Navigator should display as normal.

Managing Conda Environments

Conda environments are designed so you can install developer versions of packages without compromising your base anaconda installation. I had planned to demonstrate this with the installation of Spyder 5 but for some reason it seems that Spyder 5 can only be installed using the base Anaconda installation. So instead I will demonstrate using the latest development version of JupyterLab.

First we will create a new environment called myenv by making a clone of the base install:

conda create --name myenv --clone base

Note you could create a new blank env by using:

conda create --name myenv

This would require individual installation of every module you want to use.

This creates a new environment in

$Home/anaconda3/envs

To select the environment, type in:

conda activate myenv

Notice how the prompt changes from (base) to (myenv) indicating that myenv is now active:

With your env folder there will be a subfolder called lib. This will contain a folder indicating your Python version:

This folder contains Python and all the modules automatically inbuilt into Python such as datetime, turtle and tkinter. There is also a folder called site-packages which contains the additional added data science libraries bundled by anaconda:

To view the list of modules installed, type in:

conda list

In this case we are interested in JupyterLab, we can see that version 3.0.14 is installed:

We can remove it by typing in:

conda remove jupyterlab

To confirm the removal input:

y

Now we can check the official channel and developer channel by using:

conda search --channel anaconda jupyterlab
conda search --channel conda-forge jupyterlab

We can see that the official anaconda channel offers only 3.0.14:

Whereas the developer channel offers version 3.0.16:

We can go ahead and install this using:

conda install --channel conda-forge jupyterlab=3.0.16

JupyterLab 3.0.16 is now installed within myenv. The base env still has JupyterLab 3.0.14.

The user can select their environment by using:

conda activate myenv
conda activate base

And the JupyterLab version will change depending on the env selected. Note when the terminal is closed and reopened, the base env will be the default environment.

Environments can be viewed in the Anaconda Navigator and some basic operations carried out via the console can be replicated. The Anaconda navigator is more geared towards data science teams where one can create a specific conda environemtn for a data science project to deploy around multiple computers for multiple users. This is outwith the use of the Anaconda Individual Edition and would instead require one to use the anaconda Team Edition:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.