A much easier way to install custom versions of Python
Recently, I wrote a rather extensive blog post about compiling Python from source: Installing Python, Keras, and Tensorflow from source.
Since then, I've learnt of multiple other different ways to do that which are much easier as it turns out to achieve that goal.
For context, the purpose of running a specific version of Python in the first place was because on my University's High-Performance Computer (HPC) Viper, it doesn't have a version of Python new enough to run the latest version of Tensorflow.
Using miniconda
After contacting the Viper team at the suggestion of my supervisor, I discovered that they already had a mechanism in place for specifying which version of Python to use. It seems obvious in hindsight - since they are sure to have been asked about this before, they already had a solution in the form of miniconda.
If you're lucky enough to have access to Viper, then you can load miniconda like so:
module load python/anaconda/4.6/miniconda/3.7
If you don't have access to Viper, then worry not. I've got other methods in store which might be better suited to your environment in later sections.
Once loaded, you can specify a version of Python like so:
conda create -n py python=3.8
The -n py
specifies the name of the environment you'd like to create, and can be anything you like. Perhaps you could use the name of the project you're working on would be a good idea. The python=3.8
is the version of Python you want to use. You can list the versions of Python available like so:
conda search -f python
Then, to activate the new environment, do this:
conda init bash
conda activate py
exec bash
Replace py
with the name of the environment you created above.
Now, you should have the specific version of Python you wanted installed and ready to use.
Edit 2022-03-30: Added conda install pip
step, as some systems don't natively have pip
by default which causes issues.
The last thing we need to do here is to install pip inside the virtual conda environment. Do that like so:
conda install pip
You can also install packages with pip, and it should all come out in the wash.
For Viper users, further information about miniconda can be found here: Applications/Miniconda Last
Gentoo Project Prefix
Another option I've been made aware of is Gentoo's Project Prefix. Essentially, it installs Gentoo (a distribution of Linux) inside a directory without root privileges. It doesn't work very well on Ubuntu, however due to this bug, but it should work on other systems.
They provide a bootstrap script that you can run that helps you bootstrap the system. It asks you a few questions, and then gets to work compiling everything required (since Gentoo is a distribution that compiles everything from source).
If you have multiple versions of gcc
available, try telling it about a slightly older version of GCC if it fails to install.
If you can get it to install, a Gentoo Prefix install allows the installation whatever software you like!
pyenv
The last solution to the problem I'm aware of is pyenv. It automates the process of downloading and compiling specified versions of Python, and also updates you shell automatically. It does require some additional dependencies to be installed though, which could be somewhat awkward if you don't have sudo
access to your system. I haven't actually tried it myself, but it may be worth looking into if the other 2 options don't work for you.
Conclusion
There's always more than 1 way to do something, and it's always worth asking if there's a better way if the way you're currently using seems hugely complicated.