If you’re running Ubuntu, you should avoid upgrading or modifying the Python version that came with your system because doing so could lead to unwanted results, conflicts, and even system breakage. In the case of Xubuntu 14.04 (Trusty Tahr), the Python versions that come with it are 2.7.6 and 3.4.3.
Now, if you feel that these versions are outdated and you want to try the newer Python versions, the best course of action is to use virtual environments. Virtual environments allow you to sandbox multiple Python versions depending on your development needs so that you don’t have to upgrade or downgrade your system’s Python version. For python 2.7-3.4, virtualenvwrapper is probably your best option. From version 3.3 onwards, Python has included the venv module for creating virtual environments as part of its standard library.
I personally use virtualenvwrapper so that’s what I’m going to show you here. First of all, make sure the virtualenvwrapper is already installed in your machine then follow the steps below.
Install the following dependencies if they aren’t already.
sudo apt-get install -y \
autotools-dev \
blt-dev \
build-essential \
bzip2 \
dpkg-dev \
g++-multilib \
gcc-multilib \
libbluetooth-dev \
libbz2-dev \
libexpat1-dev \
libffi-dev \
libffi6 \
libffi6-dbg \
libgdbm-dev \
libgpm2 \
libncursesw5-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libtinfo-dev \
mime-support \
net-tools \
netbase \
python-crypto \
python-dev \
python-mox3 \
python-pil \
python-pip \
python-ply \
python-setuptools \
python-smbus \
quilt \
tk-dev \
zlib1g-dev
wget https://www.python.org/ftp/python/X.X.X/Python-X.X.X.tgz
If you want to get Python 2.7.13 (the latest Python 2 version)
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
The line below will unpack the contents of the archive to a Python-2.7.13 directory.
tar xfz Python-2.7.13.tgz
Go to the directory and configure where you want to install the new Python version. Here we’ll install it in /usr/local/lib/python2.7.13.
cd Python-2.7.13/
./configure --prefix /usr/local/lib/python2.7.13 --enable-ipv6
Run make and make install.
make
sudo make install
Remember where we installed our new Python version? Let’s check if the compiler will run.
Enter the following in your command line.
/usr/local/lib/python2.7.13/bin/python
It should result in something similar below.
Python 2.7.13 (default, Mar 27 2017, 13:19:48)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
mkvirtualenv --python=/location/where/you/installed/python <name-of-virtualenv>
or
mkvirtualenv --python=/usr/local/lib/python2.7.13/bin/python python2.7.13
Upon making the virtual environment, you will be able to immediately start working on it.
python
should result in:
Python 2.7.13 (default, Mar 27 2017, 13:19:48)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
From here you can start developing using the Python version of your virtual environment. Write code, download libraries, etc.
virtualenvwrapper has a lot of stuff that makes working with virtual environments easier. Some of these are:
To list the virtual environments on your machine
lsvirtualenv
To work on a virtual environment
workon <name-of-virtualenv>
To create a virtual environment
mkvirtualenv <name-of-virtualenv>
This uses the default Python of your machine. See #5 above if you want to use a different Python version.
To remove/delete a virtual environment
rmvirtualenv <name-of-virtualenv>
If you find my website or any of the materials I share useful, you can consider donating to the cause below.
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)