Let’s be honest: Most of the time, the Python version included with a Linux distro isn’t the newest. For example, at the time of this writing, the version of Python 2 included with our CentOS 7 Storm images is 2.7.5, and Python 3 isn’t even available out of the box. For reference (again, at the time of this writing), the latest versions are 2.7.14 and 3.6.4 respectively.
Chances are that if you’re reading this article, you already have a reason to run a different version than what’s included (especially if 3.x is needed). However, if you stumbled on this article by chance, various reasons might include new features, a software package or library that only works up to a certain version, etc.
While alternate versions of Python can be installed by way of additional YUM repositories or building from source, arguably the easiest way is by using conda. An additional benefit is that it can be installed per-user (it actually takes a little bit of extra work for a system-wide conda install, so that won’t be covered in this article).
Conda can be thought of a mix between a package and environment manager, except unlike pip and virtualenv, it can install different versions of Python. With conda, you could easily have environments running Python 2.7.5, 2.7.14, 3.5.2, and 3.6.4 if you wanted, all without requiring root privileges.
Anaconda vs. Miniconda
Before we begin, one thing to mention is that you don’t just install conda per se. Rather, you’ll install what might be analogized to a distribution – Anaconda and Miniconda being the two. Miniconda, which is what we’ll be using for this article, is the barebones “distro”. The installer comes in at ~56MB and provides the base conda environment.
In contrast, Anaconda comes not just with the conda environment, but with a suite of packages geared for data scientists (such as pandas, numpy, jupyter notebook, etc). It’s also significantly larger, with the documentation calling for about 3GB of storage being required (the installer alone is ~551MB).
If you are using Python for data science applications, you can install it after installing Miniconda. But really, the installation steps are virtually identical between Miniconda and Anaconda. The installer for Anaconda can be found here.
I just want to mention that the steps described from this point on will be conducted against a freshly created US Central Zone C 2GB VPS instance with the Fully Managed CentOS 7 image. I’ll be using a user called conda. That said, these steps should be reproducible verbatim on any of our Storm or dedicated hosted offerings.
After connecting as the user you want to install conda into, we need to download the installer for Miniconda. The latest installer can be found here.
$ wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
Once we get that’s downloaded, installation is simple:
$ bash Miniconda3-latest-Linux-x86_64.sh
It will ask to review and agree to the license. After that, it will ask to confirm the default location (for this example, it’s /home/conda/miniconda3) or specify an alternate. Once the location is confirmed, you’ll see it install a few packages (and quite a few more if Anaconda was installed).
After the packages are done installing, it will ask if you want to prepend the install location to the PATH environment variable in your .bashrc file. You’ll most likely want to do this. Don’t worry, a backup will be created. In my case, the backup file is .bashrc-miniconda3.bak
Now that the install has been completed and the PATH environment variable has been adjusted, you’ll either need to re-open the terminal or run the following command:
$ source .bashrc
and to confirm that things are as they should be:
$ python --version
Python 3.6.4 :: Anaconda, Inc.
Obviously, the version of Python may be slightly different, but it should make mention of conda or Anaconda.
Some Basic Commands
Even though we grabbed the latest installer, there’s still usually an update available. This is a good time to get a feel for working with conda.
$ conda update conda
The following packages will be UPDATED: conda: 4.4.10-py36_0 --> 4.4.11-py36_0 Proceed ([y]/n)? y
Installing A Different Base Python Version
While the installer comes with a particular version of Python (3.6.4 in this case) that serves as the base, you can always choose another. Let’s get a look at the available versions by running the following:
$ conda search python
This will come back with quite a few versions from which you can choose. One thing to note is that python in this case is treated as a package. This command can be used to search for available versions of other packages.
But for now, let’s assume we wanted to go to Python 3.5.4 for our base environment:
$ conda install python=3.5.4
It will ask for you to confirm after showing the changes it will need to make. When it’s done, you can confirm that the correct version of Python was installed as shown above. Congratulations, you’ve painlessly installed an alternate version of Python that is ready for use by the user that it was installed for!
If other users on the server need versions other than what’s provided by the system, just repeat the process for them.