Thursday, July 03, 2014

How to install virtual environment of Python 3.4.x from source in Ubuntu 14.04

Below is a recipe that I use to used to install and set-up virtual environment of Python 3.4.x in Ubuntu 14.04 x64.
# Below are commands used to install and set-up virtual environment of Python 3.4 in Ubuntu 14.04.
# First we need to install dependencies. Please not that this is much larger list than is required for setting up only Python.
# The reason is that usually I and my users install other python packages through pip (list of the popular ones is at the
# end) and these packages also require some packages to be installed first. Thus, I prefer to install everything at
# once.
sudo apt-get install build-essential libc6-dev libreadline-dev libncursesw5-dev libssl-dev libgdbm-dev libsqlite3-dev libbz2-dev liblzma-dev tk-dev bzip2 libbz2-dev sqlite3 tk8.5-dev zlib1g-dev liblzma-dev libblas3 liblapack3 gfortran libopenblas-dev liblapack-dev libmagickwand-dev libxml2-dev libxslt1-dev qtmobility-dev git cmake libqt4-dev libwebp-dev
# Download latest Python, unpack it, and enter into Python folder.
# When writing this post, Python 3.4.1 was the latest release.
wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz
tar xvfJ ./Python-3.4.1.tar.xz
cd Python-3.4.1/
# Configure, compile and install in /opt/python341
./configure --prefix=/opt/python341 --enable-shared
make
sudo make install
make clean
# Create Python virtual invornoment in /home/<yourusername>/mypy34env/
/opt/python341/bin/pyvenv ~/mypy34env/
# active the python virtual environment
source ~/mypy34env/bin/activate
# Check if we are really using the just installed python
which python pip
# expected output
#/home/<yourusername>/mypy34env/bin/python
#/home/<yourusername>/mypy34env/bin/pip
# In some cases, python shared libraries will be need. Thus we can add the location of python's libraries to the linker.
export LD_LIBRARY_PATH=/opt/python341/lib:$LD_LIBRARY_PATH
# I and many my users usually require other packaged (such as numpy, pillow, pyside) to be installed in python.
# With all dependencies installed before, installing these packages is very simple.
# Please note that installing all of them will take a little while. From my experience, pyside takes the longest
# time to compile.
pip install ipython numpy scipy sympy matplotlib pandas pillow wand flask simplejson requests arrow lxml pyside