Installing and Setting up Python 2.7 on Shared Hosting

Most shared hosting providers offer out of the box support for PHP, some let you use Perl via CGI or Ruby, but Python is rarely usable without additional setup. Often it is not installed at all or the Python version is hopelessly outdated.

In this post I'll outline the steps to install and set up Python on a shared hosting platform that runs on Linux and offers SSH access to the server.

Python 2.7.10 Installation

I show the process using the current Python 2 version, the steps for the Python 3 core distribution are equivalent. After you logged in to your hosting platform via SSH, execute the following commands.

mkdir ~/python27
cd ~/python27
wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tar.xz

When the download is finished, verify the checksum to make sure the archive has not been corrupted.

md5sum Python-2.7.10.tar.xz

Extract the archive and change the permissions of directories contained in the archive so they can be read and accessed by all groups and users.

tar xfv Python-2.7.10.tar.xz
find ~/python27 -type d -exec chmod 755 {} \;

Now execute the following commands to configure and actually install Python, these commands will take a while to finish.

cd Python-2.7.10
./configure --prefix=$HOME/python27
make
make install

Python 2.7.10 Setup

For the just installed version of Python to be accessible you need to edit your .bashrc file.

vim ~/.bashrc

Add the following 2 lines to the .bashrc.

PYTHON=$HOME/python27/bin
export PATH=$PYTHON:$PATH

Now execute the commands in your .bashrc and check the Python interpreter version. If your installation was successful the output should correspond to the downloaded version.

source ~/.bashrc
python -V

Python Packages

Python has a "batteries included" philosophy, which means that the standard distribution offers a lot of functionality. Nonetheless, you'll want to be able to install additional Python packages from PyPI and use them in your applications.

In the remainder of this post, you'll see how to install the prerequisite packages and create virtual environments for your applications. First install easy_install.

wget https://bootstrap.pypa.io/ez_setup.py
python ez_setup.py

Quit your SSH session and log in again. Now you can install pip

easy_install pip

and then virtualenvwrapper.

pip install virtualenvwrapper

Finally make the virtualenvwrapper commands available by modifying your .bashrc again.

export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export VIRTUALENVWRAPPER_PYTHON=~/python27/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=~/python27/bin/virtualenv
source ~/python27/bin/virtualenvwrapper.sh

You can choose another WORKON_HOME directory. Setting VIRTUALENVWRAPPER_VIRTUALENV_ARGS to --no-site-packages makes sure that newly created virtual environments are isolated from the system site-packages.

Conclusion

As you saw in this tutorial installing and setting up Python on a shared hosting platform requires a bit of work and SSH access. But it certainly pays off to be able to write your applications in a sane and accessible programming language that offers many features out of the box and a large number of additional packages for creating all kinds of applications. I've used this procedure on Hostmonster, where I host several Python applications.


This post was written by Ramiro Gómez (@yaph) and published on . Subscribe to the Geeksta RSS feed to be informed about new posts.

Tags: hosting linux python tutorial

Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.


Share post: Facebook LinkedIn Reddit Twitter

Merchandise