.. title: Install Python from Source in Linux .. slug: install-python-from-source-in-linux .. date: 2015-02-11 00:36:10 UTC .. tags: linux, python, ubuntu .. category: .. link: .. description: .. type: text Install Python from Source in Linux =============================================== This guide is for when you need to install a Python release different from what comes with your distribution in the repos. We'll install it to our home directory. This guide was tested to be working on Ubuntu 14.04. .. TEASER_END: Read more Get Ready ----------------------------------------------- Install pre-requisites for building Python. :: user@host:~$ sudo aptitude build-dep python2.7 python3.4 Setup directory structure. :: user@host:~$ mkdir -p ~/opt/{src,python279,python326,python336,python342} You'll need to install pip after building and installing Python. Download the script from the official source. We'll use the same script to install pip in all the different versions of Python installed using this guide. :: user@host:~$ cd ~/opt/src user@host:~/opt/src$ curl -O https://bootstrap.pypa.io/get-pip.py You should really use virtualenvs for your projects. You'll find many advantages to using them but these are some that I find very useful: * virtualenv is self-contained and doesn't pollute system or local packages * Different projects can install exactly the packages and their versions they need with no conflicts with other projects As an example, create a directory to contain all your virtualenvs. :: user@host:~$ mkdir ~/opt/myvenvs Python 2.7.9 ----------------------------------------------- Download Python 2.7.9 source tarball, extract it, and build the source. :: user@host:~$ cd ~/opt/src user@host:~/opt/src$ curl -O https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz user@host:~/opt/src$ tar xvzf Python-2.7.9.tgz user@host:~/opt/src$ cd Python-2.7.9 user@host:~/opt/src/Python-2.7.9$ ./configure --prefix=$HOME/opt/python279 user@host:~/opt/src/Python-2.7.9$ make -j4 user@host:~/opt/src/Python-2.7.9$ make install Install pip and virtualenv. :: user@host:~/opt/src/Python-2.7.9$ cd ~/opt/python279/bin user@host:~/opt/python279/bin$ ./python ~/opt/src/get-pip.py user@host:~/opt/python279/bin$ ./pip install virtualenv Create a virtualenv. :: user@host:~$ ~/opt/python279/bin/virtualenv ~/opt/myvenvs/py279venv user@host:~$ source ~/opt/myvenvs/py279venv/bin/activate (py279venv)user@host:~$ which python (py279venv)user@host:~$ deactivate Install pip in the virtualenv. :: (py279venv) user@host:~$ python ~/opt/src/get-pip.py Python 3.2.6 ----------------------------------------------- Although Ubuntu doesn't have Python 3.2 in its official repos in Ubuntu 14.04, the build dependencies we installed earlier were sufficient to build 3.3. Download Python 3.2.6 source tarball, extract it, and build the source. :: user@host:~$ cd ~/opt/src user@host:~/opt/src$ curl -O https://www.python.org/ftp/python/3.2.6/Python-3.2.6.tgz user@host:~/opt/src$ tar xvzf Python-3.2.6.tgz user@host:~/opt/src$ cd Python-3.2.6 user@host:~/opt/src/Python-3.2.6$ ./configure --prefix=$HOME/opt/python326 user@host:~/opt/src/Python-3.2.6$ make -j4 user@host:~/opt/src/Python-3.2.6$ make install Install pip and virtualenv. :: user@host:~/opt/src/Python-3.2.6$ cd ~/opt/python326/bin user@host:~/opt/python326/bin$ ./python3 ~/opt/src/get-pip.py user@host:~/opt/python326/bin$ ./pip install virtualenv Create a virtualenv. :: user@host:~$ ~/opt/python326/bin/virtualenv-3.4 ~/opt/myvenvs/py326venv user@host:~$ source ~/opt/myvenvs/py326venv/bin/activate (py326venv)user@host:~$ which python3 (py326venv)user@host:~$ deactivate You should install pip in the virtualenv. :: (py326venv) user@host:~$ python3 ~/opt/src/get-pip.py Python 3.3.6 ----------------------------------------------- Although Ubuntu doesn't have Python 3.3 in its official repos in Ubuntu 14.04, the build dependencies we installed earlier were sufficient to build 3.3. Download Python 3.3.6 source tarball, extract it, and build the source. :: user@host:~$ cd ~/opt/src user@host:~/opt/src$ curl -O https://www.python.org/ftp/python/3.3.6/Python-3.3.6.tgz user@host:~/opt/src$ tar xvzf Python-3.3.6.tgz user@host:~/opt/src$ cd Python-3.3.6 user@host:~/opt/src/Python-3.3.6$ ./configure --prefix=$HOME/opt/python336 user@host:~/opt/src/Python-3.3.6$ make -j4 user@host:~/opt/src/Python-3.3.6$ make install Install pip. Python 3.3 already provides virtualenv, called pyvenv, so you don't need to install it afterwards. :: user@host:~/opt/src/Python-3.3.6$ cd ~/opt/python336/bin user@host:~/opt/python336/bin$ ./python3 ~/opt/src/get-pip.py According to the `Python docs `_, Python 3.3's virtualenv doesn't install pip. There's an additional step to install it. :: user@host:~$ ~/opt/python336/bin/pyvenv-3.3 ~/opt/myvenvs/py336venv user@host:~$ source ~/opt/myvenvs/py336venv/bin/activate (py336venv) user@host:~$ python3 ~/opt/src/get-pip.py (py336venv) user@host:~$ which python3 (py336venv) user@host:~$ deactivate Python 3.4.2 ----------------------------------------------- Download Python 3.4.2 source tarball, extract it, and build the source. :: user@host:~$ cd ~/opt/src user@host:~/opt/src$ curl -O https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz user@host:~/opt/src$ tar xvzf Python-3.4.2.tgz user@host:~/opt/src$ cd Python-3.4.2 user@host:~/opt/src/Python-3.4.2$ ./configure --prefix=$HOME/opt/python342 user@host:~/opt/src/Python-3.4.2$ make -j4 user@host:~/opt/src/Python-3.4.2$ make install Python 3.4 already provides pip and virtualenv, called pyvenv, so you don't need to install them afterwards. According to `Installing Python Modules `_, Python 3.4 installs pip by default when pyvenv creates a virtualenv. :: user@host:~$ ~/opt/python342/bin/pyvenv-3.4 ~/opt/myvenvs/py342venv user@host:~$ source ~/opt/myvenvs/py342venv/bin/activate (py342venv) user@host:~$ which python3 (py342venv) user@host:~$ deactivate Final Comments ----------------------------------------------- This is an easy-ish, tested way to get multiple releases of Python installed to your home directory. A better method may be to use Nix package manager to find and install Python releases. Maybe you could leverage it to create your own packages (derivates)? Additional Resources ----------------------------------------------- * `How to manually update a deb package from source `_ * `Debian New Maintainers' Guide `_ * `Python Developer’s Guide `_