To get Anaconda Python installed on Ubuntu, you need to follow some important steps including configuring the Anaconda environment.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Software Installation tasks.
In this context, we shsll look into how to install Anaconda Python on Ubuntu.
How to install Anaconda?
To install Anaconda, you need to download the latest Anaconda installer bash script from the official website .
To do this, change to the /tmp directory on the server and use curl to download the link that you copied from the Anaconda website as shown below;
curl -O https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh
Next, verify the data integrity of the installer with cryptographic hash verification using the command below;
Check the output against the hashes available at the Anaconda with Python 3 on the 64-bit Linux page for the appropriate Anaconda version. As long as the output matches the hash displayed in the sha2561 row, we are good to go.
Now we can run the script to start the installation by running the bash command below;
This installation script prompts to choose the location of the installation. We can press ENTER to accept the default location, or specify a different location to modify it.
Once the installation is complete, you will receive the following output that “installation finished”. It also asks if we want to prepend the Anaconda3 install location the .bashrc file.
Now activate the installation by sourcing the "~/.bashrc" file by executing the following command;
Now that Anaconda is installed, we can go on to configuring Anaconda environments.
How to configure Anaconda Environments?
Anaconda virtual environments allow us to keep projects organized by Python versions and packages needed. For each Anaconda environment, we can specify which version of Python to use and can keep all of the related programming files together within that directory.
First, we can check to see which versions of Python are available for us to use the following command;
conda search "^python$"
We will receive output with the different versions of Python that we can target.
To create an environment using the most recent version of Python 3, use the command below. Here my_env is the environment name:
conda create --name my_env python=3
The conda utility will now fetch the packages for the environment and let us know when it is complete. We can activate the new environment by typing the following command;
conda activate my_env
As the environment is active now, the command prompt prefix will change to include the environment name. Now to verify the python version, use the command below:
Python 3.7 :: Anaconda, Inc.
Now, to deactivate the Anaconda environment, use the command below:
Further, to update the version of Python along the same branch within a respective environment with the following command:
conda update python
To update to a more specific version of Python, we can pass that to the python argument, as in python=3.3.2.
Also, we can inspect all of the environments set up with this command:
conda info --envs
Each environment we create with conda comes with several default packages like the ones given below:
Additionally, you can add additional packages, such as NumPy for example, with the following command:
conda install --name my_env35 numpy
To add a package at the time of creating the environment, use the command below(Here NumPy is the package that needs to be added):
conda create --name my_env python=3 numpy
To remove an environment, use the command below:
conda remove --name my_env35 --all
How to Update Anaconda?
It is important to keep Anaconda up to date to work with the latest package releases. To do this, we should first update the conda utility by running the command below;
conda update conda
Once the update of conda is complete, we can update the Anaconda distribution:
conda update anaconda
This will ensure that you are using the latest releases of conda and Anaconda.
How to uninstall Anaconda?
To uninstall anaconda start with the anaconda-clean module, which will remove configuration files during the uninstall process.
conda install anaconda-clean
Once installed, we can run the following command:
This will also create a backup folder called .anaconda_backup in the home directory. We can now remove the entire Anaconda directory by entering the following command:
rm -rf ~/anaconda3
Finally, we can remove the PATH line from the .bashrc file that Anaconda added. To do so, first, open this file with a text editor and then scroll down to the end of the file. Now, Delete or comment out the export PATH line.
Anaconda is now removed from your server.