Recently, I have wanted to experiment with the latest features of Python3 (3.5.2 as of this date). Unfortunately, Ubuntu 14.04 is stuck on 3.4.3 and it is unlikely that 3.5 will ever come to this LTS release (as per the way the release-schedules work, or my understanding thereof).
During the discovery stage, I investigated and found a variety of ways to install different versions of Python on the same system. Some of these options are:
- PyEnv (quite popular)
- Pythonbrew (an old favourite of mine, but the project is not being maintained anymore)
- Containers (this is a lazy answer, and you will see why below)
I didn't want to risk borking my machine with a newer Python version being installed (even if it was installed in parallel to the system-version), so I settled on doing everything within an LXC container.
My goals were:
- Create the lightest possible containerized-option
- Try to keep the install process as simple as possible
- Avoid any need to modify/add anything on the host system
We can create/start/attach our container using the LXC Ubuntu template:
sudo lxc-create --name py35 --template ubuntu sudo lxc-start --name py35 --daemon sudo lxc-attach --name py35
I have called my container:
You should now either be connected as the 'root' user (within the container) or if you had to go through the login process, as the 'ubuntu' user. The rest of this tutorial will assume that you are the 'ubuntu' user.
The Ubuntu template lacks the the required package/s to add the 'deadsnakes' PPA, which is what we will use to get the latest Python version (which we will then use with virtualenv). Let's add the PPA:
sudo apt-get update sudo apt-get upgrade sudo apt-get install software-properties-common sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python3.5
Check if it successfully installed:
sudo pip3 install virtualenv
I will now create a new folder for my virtualenv project. I will call this folder "project1". Then I will create the virtualenv itself:
mkdir /home/ubuntu/project1 virtualenv -p /usr/bin/python3.5 /home/ubuntu/project1/
Going through the virtualenv process, you should also have the latest
Let us test that everything worked:
cd /home/ubuntu/project1/ source /home/ubuntu/project1/bin/activate python --version # should output 3.5.2 pip --version # should output 8.1.2
Containers = lazy answer?
Above, I mentioned that using containers for different Python versions is a lazy answer. The simple reason for this is that even if you use containers, you are still stuck with the system-version. So the only way to bump up/down your Python version is to move to a newer/older Ubuntu release. This still doesn't guarantee you will get the version you wish. My instructions above get you the version you want, whilst keeping your Host protected from different Python installs.
You have now successfully installed Python 3.5.2 on Ubuntu 14.04. You could easily install multiple Pythons/projects within a single container, but things could quickly get bloated. I recommend using containers as the layer of abstraction/isolation for each large-enough project and only using
virtualenv (within a container) when you require the latest/fixed Python versions.