The Anaconda Python Distribution (Linux)

Installing the Anaconda Python Distribution with the Spyder 5 IDE and JupyterLab 3 IDE on Windows 11 and Best Practices for Managing a conda Environment

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

What is Anaconda/Miniconda and what is the difference with Python?

Anaconda is a Python distribution. The Anaconda Python distribution contains the conda package manager which can be used to install Python conda (the Anaconda official channel, maintained by the Anaconda company) or conda-forge (the Community/Developer channel) packages. Anaconda also includes a base environment which 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.

Miniconda has the same conda package manager as Anaconda but instead of having a versatile base environment packed together with data science libraries it essentially has an empty base environment. Although the base environment can be modified, it is recommended to create your own conda environment opposed to modifying the base environment in most cases.

In both Miniconda and Anaconda a conda environment can be created from scratch. In theory new users can just install Anaconda and use the base conda environment directly. Unfortunately the Anaconda team are very slow in updating the IDEs (Spyder specifically) in the base conda environment and there are conflicts in the conda base environment that cannot be solved when attempting to update these IDEs. This guide will instruct in creating a new conda environment for the Spyder IDE and a new conda environment with the JupyterLab IDE. It will use the Anaconda Individual Edition but the instructions are more or less equally applicable to Miniconda.

Why have a Python distribution and use the conda package manager?

The conda package manager 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 a plotting library for data visualization and acts as a wrapper around the matplotlib plotting library. The matplotlib plotting library itself is dependent on numpy which is used for array manipulation. 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 in turn 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 the conda package manager.

Cost and Licensing

Python alongside the most commonly used data science libraries included in the Anaconda Python distribution can be used as an open-source alternative to expensive commercial scientific software such as MATLAB. This guide will focus on installation of Python and at the end link to additional guides which I put together which examine the use of Python to replace MATLAB functionality.

Anaconda itself is a company. The Anaconda Individual Edition is Free but as the name suggests designed only for Individual use. Anaconda has a Commercial, Team and Enterprise Edition for Commercial use. The license essentially includes product support for Commercial use from the Anaconda company and funds the software development (similar to RedHat with Fedora and RedHat Enterprise Linux). The Team and Enterprise Editions may include additional support when it comes to rolling out a custom base environment for a Team or Large Corporation of Data Scientists for example.

Miniconda has the same conda package manager as Anaconda and an empty base environment. Miniconda can be used with the conda-forge community channel for Free and does not have the commercial restrictions included in the Anaconda Individual Edition.

Operating System

Anaconda is compatible with Windows, Linux and Mac.

This guide is Linux focused and will use Ubuntu 20.04 LTS but instructions are more or less identical for Ubuntu flavours and Ubuntu based distros such as Linux Mint 20.2, Zorin OS 16, KDE Neon and other Linux distros such as Fedora 34 and DeepIn 20.2.

There are usually no installation issues on a clean install of Linux. Installation issues usually arise when there is an old Anaconda installation present. Unfortunately 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.

Installation of Anaconda on Linux is slightly different to installation on Windows. Installation on Windows uses a .exe and Start Menu shortcuts are created. Installation on Linux requires use of a shell script that has to be launched via the terminal. Once installed there are no shortcuts and the Terminal has to be used to launch the Anaconda Navigator. This can be confusing for new users.

Installation Video

Update Commands (Advanced)

For those experienced with the conda package manager. The following lines of code can be used in the Anaconda PowerShell Prompt or the Miniconda PowerShell Prompt. These are explained in far more detail with screenshots in subsequent sections for beginners.

Update the conda base environment and conda package manager to the latest version:

conda update --all

Clone a conda environment:

conda create -n base2 --clone base

Create a new environment for Spyder 5.1.5:

conda create -n spyder515
conda activate spyder515
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install spyder=5.1.5
conda install cython matplotlib numpy pandas scipy sympy seaborn openpyxl xlrd xlsxwriter
spyder

Create a new environment for JupyterLab 3.1.14 with additional Lab Extensions:

conda create -n jupyterlab3114
conda activate jupyterlab3114
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install jupyterlab=3.1.14
conda install cython matplotlib numpy pandas scipy sympy seaborn openpyxl xlrd xlsxwriter
conda install nodejs ipywidgets jupyterlab-variableinspector ipympl plotly jupyterlab-drawio
jupyter-lab

To list conda environments and delete old conda environments use (replacing spyder514 and jupyterlab3113 with the name of your old conda environment):

conda env list
conda env remove -n spyder514
conda env remove -n jupyterlab3113

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 and Purging the Anaconda Configuration

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

The Anaconda Individual installer or Miniconda installer can be downloaded from the Anaconda website:

Select the Linux installer.

Then select 64 Bit Installer:

Select Save File and then OK:

Wait for the Download to complete:

Open up the Terminal:

To change to the Downloads folder, type in:

cd downloads

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

Right click your downloaded .sh file and select Rename:

Select the full name including the .sh extension and copy it:

Type in bash and then a space and paste in the file name.

For example:

bash Anaconda3-2021.05-Linux-x86_64.sh

Then hold down [↵] to scroll through the license agreement:

To accept the License Agreement type in:

yes

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

This will be in your Home folder:

Initializing Anaconda During Installation

During the install you will be asked whether you wish to initialize Anaconda which adds the conda commands to the Terminal.

In most cases this is recommended, note however no is automatically selected. If you press [↵] by mistake, you will install Anaconda but it won't be initialized meaning you won't be able to use it.

Initializing will add the conda commands to the bash-rc file. The bash-rc file is found in Home but it is a hidden file:

It can be viewed in a text editor:

No conda commands are present until conda is initialized:

To initialize Anaconda during installation type in:

yes

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

The bash-rc file is updated:

You need to close and reopen the Terminal for it to refresh the commands available to it from the bash-rc file:

Note to the left hand side in parenthesis is the name of the currently selected conda environment (base) is the default conda environment.

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.

The pip (Python install package) command

In Python library documentation you will see frequent references to pip (Python install package). When using a conda environment, although the Anaconda Powershell Prompt recognises the:

pip install package-to-be-installed

command, it is frequently problematic. It should be avoided where possible as there is normally a conda equivalent command that behind the scenes performs additional checks to solve (address any conflicts and check for dependencies) the conda environment.

Search for the name of your package on the Anaconda Website before following any other installation instructions involving pip

The conda command

We will use the conda command to use the conda package manager. Observe that the terminal begins with (base) indicating that the base conda environment is currently selected. Type in:

conda

This will give you an overview of using the conda package manager. We are interested in the following commands update, list, search, install, remove, config and create:

The syntax is usually of the form:

conda command command_argument

Most of the arguments have additional options (note the use of a double dash before the option):

conda commmand --option option_setting command_argument

The most commonly used options often have a one letter abbreviation (note the use of a single dash before it):

conda commmand -o option_setting command_argument

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.

update

The first command we will look at is update. update can be followed by the name of the python library we wish to update or alternatively –all which updates everything in the base environment. In the case of Anaconda, it updates the conda package manager and all the data science libraries to the latest version that the Anaconda team have tested together in the conda channel. In the case of miniconda, the base environment is empty by default and –all will just update the conda package manager.

We can use the command to explicitly update the base conda environment using the conda channel:

conda update --name base --channel conda --all

This can be abbreviated using the single letter abbreviation for the optional arguments:

conda update -n base -c conda --all

Because the (base) environment is selected as highlighted in parenthesis and the default channel is the conda (Anaconda official channel) these options do not need to be explicitly specified and we can use:

conda update --all

Type:

y

to confirm.

The base environment will be updated:

list

We can use the command list to list the installed packages within a conda environment. Once again we can include the option to specify the base environment specifically but this isn't necessary as the base environment is selected:

conda list --name base
conda list -n base
conda list

You will see a multitude of packages listed. I have edited the screenshot to highlight only the conda base environment, the python version, the jupyterlab and spyder version as well as the numpy, pandas, matplotlib and seaborn libraries versions:

We can also type in the option –revision:

conda list --revision

Which gives details about the conda environment and its previous revisions:

The next command we are going to use is search to search for the name of a package. Once again we can use the optional argument name to specify the name of the conda environment. We can also use the optional argument channel to specify the channel to search in. These have one letter abbreviations. The default environment is (base) highlighted in parenthesis and the default channel for Anaconda is conda (the official channel maintained by the Anaconda company):

conda --name base --channel conda spyder
conda -n base - conda spyder
conda search spyder

We see that the latest version of Spyder that they offer is 5.0.5:

We can also switch the channel to the community channel conda-forge which has submissions by the Spyder development team:

conda search --channel conda-forge spyder
conda search -c conda-forge spyder

We see they offer 5.1.5:

Likewise we can check for the latest version of JupyterLab:

conda search jupyterlab
conda search -c conda-forge jupyterlab

install

We can use the install command to install a package:

conda install --channel conda spyder=5.0.5
conda install -c conda spyder=5.0.5
conda install spyder=5.0.5
conda install --channel conda-forge spyder=5.1.5
conda install -c conda-forge spyder=5.1.5

Unfortunately Spyder is an IDE with a lot of dependencies which clash with dependencies of other packages in the base environment. It hangs when trying to solve dependencies for the latest version of spyder from the conda-forge channel:

I needed to cancel the operation as it hung by pressing [Ctrl] + [c]:

We will instead in a moment install Spyder in its own empty conda environment.

We can also use the install command to revert to a previous revision of a conda environment:

conda install --revision 0

The base conda environment

Before looking creating a new conda envronment. Let's examine the base environment in more detail. Let's go to the Home/anaconda3 folder:

Within this is the lib folder:

And within this is the Python3.x folder:

Within this folder are the inbuilt modules such as the datetime module. When we type in:

import datetime as dt

We are importing the datetime.py module.

Within the python3.x folder is the site-packages subfolder which contains the additional datascience libraries/packages included in the Anaconda base environment:

It contains the IDEs such as Spyder and JupyterLab:

It includes numpy:

When we type in:

import numpy as np

We are referencing the __init__.py within the numpy folder.

It includes pandas:

When we type in:

import pandas as pd

We are referencing the __init__.py within the pandas folder.

It includes matplotlib:

When we type in:

import matplotlib.pyplot as plt

We are referencing the pyplot module within the matplotlib folder.

Creating a conda environment for Spyder 5.1.5

In the anaconda3 folder there is the envs subfolder. This folder will be empty by default:

We will now look at creating a new conda environment for Spyder 5.1.5:

conda create --name spyder515
conda create -n spyder515

The location of the new env will be specified. Input:

y

We now see a folder spyder515 which corresponds to the name of the env created:

This folder will be pretty empty as nothing so far is installed:

We need to select the conda environment usign the activate command:

conda activate spyder515

Notice that now the parenthesis say (spyder515) opposed to (base). This means the spyder515 conda environment is selected and this means any of the conda commands we use will now apply to this environment opposed to the base environment.

We will configure the conda environment to use the conda-forge channel with a strict channel priority using the config command:

conda config --add channels conda-forge
conda config --set channel_priority strict

Now we can install the latest version of spyder, spyder 5.1.5:

conda install spyder=5.1.5

Because we have added the conda-forge channel and set it to strict priority and the spyder515 conda environment is selected we do not need to specify these options.

This will list Spyder 5.1.5 and all its mandatory dependencies:

Input:

y

to proceed.

You will see the spyder515 folder fill up. It will have its own lib subfolder:

Alongside python3.x subfolder:

This will contain the inbuilt modules which correspond to this python version:

There will also be a site-packages subfolder:

This will contain the latest version of spyder 5.1.5:

It will however lack the optional spyder dependencies. We will install these using the install command:

conda install cython matplotlib numpy pandas scipy seaborn openpyxl xlrd xlsxwriter

Note that we can use the install command in the fashion above to install multiple packages simultaneously. We could if we wanted also use the assignment operator after each package to specify a version, like we did with spyder.

Input

y

to proceed:

Spyder 5.1.5 is now installed:

Installing Kite Autocompletion

Spyder 5.1.5 is designed to work alongside Kite autocompletions. Kite was downloadable for their website:

However their website has said Kite is temporarily unavailable for some months now. I think their main page is pulled as they handle some lawsuit/licensing agreement.

Direct links are listed on a Kite thread in GitHub:

This should download a .sh file:

However it is quite frequently down. Attempt to reaccess the links 30 minutes later and it will likely work:

Right click empty space in your downloads folder and select open in terminal:

Type in:

bash kite-installer.sh

Press [↵]:

Kite should now be installed:

Kite is now installed and can be launched from the Start Menu:

Input your email and select Continue or alternatively select Continue without Email:

Check Automatically integrate Kite when new editors are installed and select Let's Go!

Kite should now be ready for use with Spyder:

Creating a conda environment for JupyterLab 3.1.14

We will create a conda environment for JupyterLab using a very similar procedure as above.

conda create -n jupyterlab3114
conda activate jupyterlab3114
conda config --add channels conda-forge
conda config --set channel_priority strict

To search for the latest version of JupyterLab we will use:

conda search jupyterlab

At the time of writing it is 3.1.14. To install this alongside its mandatory dependencies we will use:

conda install jupyterlab=3.1.14

To install its optional dependencies we will use:

conda install cython matplotlib numpy pandas scipy sympy seaborn openpyxl xlrd xlsxwriter

To install JupyterLab widgets which are written using nodejs and have a nodejs dependency we will use:

conda install nodejs ipywidgets jupyterlab-variableinspector ipympl plotly jupyterlab-drawio

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. The Anaconda Navigator is essentially a GUI interface of the conda command line.

To launch the Anaconda Navigator type in:

anaconda-navigator

The Anaconda Navigator should launch but unfortunately starts up minimized, so you will need to maximize it from the sidepanel.

From the Anaconda Navigator one can use the tiles to launch Python IDEs such as Spyder and JupyterLab which are installed in the base environment. This guide only focuses on Spyder and JupyterLab:

Alternatively a conda environment can be selected and Spyder or JupyterLab can be launched from its own tile:

The environments tab can be selected to view a GUI version of conda list where there is some limited scope to update some packages. In general the Anaconda Navigator is very buggy and it is more reliable to use the conda command line to manage a conda environment:

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.

Spyder 5 IDE

Launching Spyder

There are no Start Menu shortcuts for Spyder on Linux and we must use the Terminal to launch Spyder. To launch spyder, activate your conda environment and type in:

conda activate spyder515
spyder

Failing to activate your conda environment will launch the old version of spyder from your base conda environment:

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:

Using Kite Autocompletion

You can test out Kite autocompletion by importing a Python inbuilt module such as datetime. Kite should recognise the keystroke and suggest the inbuilt module:

If the module is imported and then typed followed by a dot. Kite should display a list of objects that can be called from the module:

import datetime
datetime.

If a class or function is typed, the docstring should display as a tooltip:

The docstring can also be viewed in the help pane by highlighting the class or function and pressing [Ctrl] + [ i ]:

This should work for the datascience libraries such as numpy, pandas and matplotlib however it make take a while to cache the documentation upon first time use:

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

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.

Leave a Reply

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