All major Linux distributions come up with a base Python package. It ships with Python because the system actually needs it. But If you update the core Python interpreter, you risk breaking certain OS utilities, such as yum in CentOS. However, if you use a third-party distribution, you’re free to add/remove packages and update Python as much as you want, and your OS will remain fully operational.
A relatively new and widely-used tool for managing Python installations is Anaconda by Continuum.io, which makes building Python quite easy. We manage the packages within this using conda command.
In this article, I'll explain on how we can install and setup Python Anaconda in most popular OS distributions.
Overview
Conda command is the primary interface for managing installations of various packages using Anaconda. It can query and search the Anaconda package index and current Anaconda installation. It also helps to create new conda environments. Furthermore, we can install and update packages into existing conda environments.
For a smaller server environment, I would recommend using Miniconda, a mini version of Anaconda that includes only conda and its dependencies.
Anaconda contains conda plus over 720 open source packages, it is the most robust type of installation.You do not need administrative or root permissions to install Anaconda, hence we can install it for the local user with writable install location. If needed you can even install it server wide with administrator permissions.
Installing Python Anaconda on CentOS 7.4/Ubuntu 16.04
A minimum of 3G disk space is required to download and install all of its packages. Before proceeding with this installation, make sure you've installed wget & bzip2 in your server. If not, you just install it by the below command:
#yum install bzip2 wget//*In CentOS //*
Or #apt-get install bzip2 wget*// In Ubuntu //
*
Next, we need to choose a directory where the new Python binaries will be installed. In this tutorial, we’ll be using the installation path (/usr/local/anaconda)
. You can change the default installation path to the one which you need during the installation. Let's walk through the steps on how to install Python 3.6 using the Anaconda bash installer to the above directory.
-
Download the installer
Depending on the preferred Python version, you can download the Anaconda installer. I've downloaded the Anaconda 5 with Python 3.6 for 64-Bit (x86)
architecture.
# wget https://repo.continuum.io/archive/Anaconda3-5.0.0.1-Linux-x86_64.sh
-
Run the installer
Once you've downloaded the installer, you can open up a terminal and run it. You can run the installer using shell command sh just like this.
# sh Anaconda3-5.0.0.1-Linux-x86_64.sh
The installer will then begin and proceed with a series of questions. Accepting the Terms and Conditions is pretty self-explanatory. If you are unsure about any setting, accept the defaults. You can change them later. I've changed the default installation path during the installation phase just like this:
Do you accept the license terms? [yes|no] [no] >>> Please answer 'yes' or 'no':' >>> yes 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] >>> /usr/local/anaconda/ PREFIX=/usr/local/anaconda
Towards the end, when it asks if you want to prepend Anaconda to your OS’s PATH variable, select ‘Yes’. This will make running Conda commands a lot easier.
Do you wish the installer to prepend the Anaconda3 install location to PATH in your /root/.bashrc ? [yes|no] [no] >>> yes Appending source /usr/local/anaconda/bin/activate to /root/.bashrc A backup will be made to: /root/.bashrc-anaconda3.bak For this change to become active, you have to open a new terminal. Thank you for installing Anaconda3!
To make the changes take effect, close and then re-open your Terminal window.
-
Testing your installation
Our next step is to test our installation. To test your installation, in your Terminal window run the command conda list
.
If your installation is successful, then it will list all the installed packages in the alphabetical order just like this.
# conda list # packages in environment at /root/anaconda3: # _ipyw_jlab_nb_ext_conf 0.1.0 py36he11e457_0 alabaster 0.7.10 py36h306e16b_0 anaconda 5.0.0.1 py36hfb0b618_1 anaconda-client 1.6.5 py36h19c0dcd_0 anaconda-navigator 1.6.8 py36h672ccc7_0 anaconda-project 0.8.0 py36h29abdf5_0 asn1crypto 0.22.0 py36h265ca7c_1 astroid 1.5.3 py36hbdb9df2_0 astropy 2.0.2 py36ha51211e_4 babel 2.5.0 py36h7d14adf_0 backports 1.0 py36hfa02d7e_1 backports.shutil_get_terminal_size 1.0.0 py36hfea85ff_2 beautifulsoup4 4.6.0 py36h49b8c8c_1 bitarray 0.8.1 py36h5834eb8_0 bkcharts 0.2 py36h735825a_0 blaze 0.11.3 py36h4e06776_0 bleach 2.0.0 py36h688b259_0 bokeh 0.12.7 py36h169c5fd_1 boto 2.48.0 py36h6e4cd66_1 bottleneck 1.2.1 py36haac1ea0_0
You can confirm with the newly installed Python location by just typing the command which python.
# python -V Python 3.6.2 :: Anaconda, Inc. # which python /usr/local/anaconda/bin/python
And there you have it! Anaconda is ready to be used. As mentioned before, using conda
command we can easily manage the packages. For instance, we can update any package using conda update [package_name] or install a package using conda install [package_name]. Please see the example below for updating our Python using conda command.
# conda update python Fetching package metadata ........... Solving package specifications: . Package plan for installation in environment /root/anaconda3: The following packages will be UPDATED: anaconda: 5.0.0.1-py36hfb0b618_1 --> custom-py36_0 conda: 4.3.27-py36h2866c0b_0 --> 4.3.29-py36ha26b0c0_0 python: 3.6.2-hdfe5801_15 --> 3.6.3-hc9025b9_1 Proceed ([y]/n)? y python-3.6.3-h 100% |###############################################################################################| Time: 0:00:00 37.86 MB/s anaconda-custo 100% |###############################################################################################| Time: 0:00:00 2.68 MB/s conda-4.3.29-p 100% |###############################################################################################| Time: 0:00:00 34.78 MB/s
You can get familiar with more of these commands usage using conda commands cheat sheet.
Installing Miniconda on CentOS 7.4/Ubuntu 16.04
As mentioned before, for a smaller server environment with a minimum capacity less than 3GB disk space. I would recommend Miniconda to be installed which is a mini version of Anaconda that includes only conda and its dependencies. With this installation, we can obtain conda command. I'll walk through the installation steps below:
-
Download the installer
Depending on the preferred Python version, you can download the Miniconda installer. I've downloaded the Miniconda with Python 3.6 for 64-Bit (x86) architecture.
#wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
-
Run the installer
Once you've downloaded the installer, you can open up a terminal and run the installer using shell command sh just like this.
# sh Miniconda3-latest-Linux-x86_64.sh
The installer will then begin and proceed with a series of questions. Accepting the Terms and Conditions, you will be asked for the installation path. If you are unsure about any setting, accept the defaults. You can change them later.
As the previous installation, towards the end, it asks whether you want to prepend Miniconda to your OS’s PATH variable. You need to select ‘Yes’
for a smoother package management using conda
.
installation finished. Do you wish the installer to prepend the Miniconda3 install location to PATH in your /root/.bashrc ? [yes|no] [no] >>> yes Appending source /root/miniconda3/bin/activate to /root/.bashrc A backup will be made to: /root/.bashrc-miniconda3.bak For this change to become active, you have to open a new terminal. Thank you for installing Miniconda3!
-
Testing your installation
To make the changes take effect, just make sure to close and re-open your Terminal window. You can test your installation, in your Terminal window by running the command conda list
.
# conda list # packages in environment at /root/miniconda3: # asn1crypto 0.22.0 py36h265ca7c_1 ca-certificates 2017.08.26 h1d4fec5_0 certifi 2017.7.27.1 py36h8b7b77e_0 cffi 1.10.0 py36had8d393_1 chardet 3.0.4 py36h0f667ec_1 conda 4.3.27 py36h2866c0b_0 conda-env 2.6.0 h36134e3_1 cryptography 2.0.3 py36ha225213_1 idna 2.6 py36h82fb2a8_1 libedit 3.1 heed3624_0 libffi 3.2.1 h4deb6c0_3 libgcc-ng 7.2.0 hcbc56d2_1 libstdcxx-ng 7.2.0 h24385c6_1 ncurses 6.0 h06874d7_1 openssl 1.0.2l h9d1a558_3 pip 9.0.1 py36h30f8307_2 pycosat 0.6.2 py36h1a0ea17_1 pycparser 2.18 py36hf9f622e_1 pyopenssl 17.2.0 py36h5cc804b_0
If your installation is successful, then it will list all the installed packages in the alphabetical order as above. This time, it will list only a few packages comparing to the previous Anaconda installation.
Uninstalling Python Anaconda/Miniconda
To uninstall Python Anconda/Miniconda, you can just remove the installation folder and remove the environment variables set in .bashrc file
. For my installation, it will be just like this.
rm -rf /usr/local/anaconda *// For Anaconda //* Or rm -rf /root/miniconda3 *// For Miniconda //*
And finally, you can edit the ~/.bashrc
file and remove the following entries added for Anaconda/Miniconda directory from your PATH environment variable.
For Anaconda: # added by Anaconda3 installer export PATH="/usr/local/anaconda/bin:$PATH" For Miniconda: # added by Miniconda3 installer export PATH="/root/miniconda3/bin:$PATH"
Wrapping up
Python Anaconda is a popular distribution among the developers which make it easier for them to independently manage the Python installations without tampering with the system Python installed at /usr/bin/python. It makes it possible to installs the new version side-by-side with the old one. By installing this distribution, you’re free to add or remove packages and update Python to any new versions as you want, without interfering with your underlying OS.
Very informative and useful.
Thanks alot.
Thanks for the reply. Happy to hear it :-)