Since I write python code that should be running on different python versions, I have to install multiple python versions on my workstation. As usually, I believe that we should do everything well as we can :).
This post is a description of my procedure to get different python versions installed in my Ubuntu workstation.
Installing Multiple Versions
Ubuntu typically only supports one python 2.x version and one 3.x version at a time. There’s a popular PPA called deadsnakes that contains older versions of python. To install it you should run the below commands:
$ sudo add-apt-repository ppa:fkrull/deadsnakes $ sudo apt-get update
I’ve a Ubuntu 14.04 already installed in my workstation (So I’ve both python2.7 and python3.4). So, I’ll install versions 2.6 and 3.3.
$ sudo apt-get install python2.6 python3.3
Let’s check the default python version by running `python – V`
$ python -V Python 2.7.6
Now, to manage the different python versions I will use an amazing Linux command: update-alternatives. According to Linux man page, ” update-alternatives maintain symbolic links determining default commands ”
Firstable, let’s install the different alternatives:
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 10 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.3 30 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 40
To choose the default python version you should run the below command:
$ sudo update-alternatives --config python
Secondly, I can switch between the different Python versions easily with the previous command. However, Ubuntu runs multiple maintenance scripts and those script may break if I choose Python 2.6 as a default version.
Using virtualenv
I assume that we have different python version installed on your machine and you didn’t change the default python version (which is 2.7 in my case).
1. Installing virtualenv
$ sudo apt-get install python-virtualenv
2. Managing different python version
Suppose that I will start a new project which will run on Python 2.6. Using this solution, I will be able to manage different version of python and different version of any package I use. Great!
$ virtualenv -p /usr/bin/python2.6 ~/.envs/project_x_py2.6 Running virtualenv with interpreter /usr/bin/python2.6 New python executable in ~/.envs/project_x_py2.6/bin/python2.6 Also creating executable in ~/.envs/project_x_py2.6/bin/python Installing distribute....................................done. Installing pip.....................done.
3. Activating virtualenv
Before that you can install any package for this project, you should activate it:
$ source ~/.envs/project_x_py2.6/bin/activate
Now, If we check the default python version used for this project:
$ python -V Python 2.6.9 $ which python ~/.envs/project_x_py2.6/bin/python
When you’re gone with the project, just deactivate its virtualenv and you can back to it when you need by activating it
$ deactivate