How to install Django using virtualenv

Using virtualenv to install Django is recommended. This is because when you use virtualenv, you create an isolated environment with its own installation directories. This allows you to install a custom version of Python and its different packages which is not connected to the global installation on the server. This also solves the issue with permissions when installing software.

Installing a custom Python version

As of April 2015, DreamHost currently runs Python v2.7.3.

To install a custom Python version, you must download the version from The following describes how to install Python 2.7.7:

  1. Log into your server via SSH, and then run the following commands one at a time:
    [server]$ cd ~
    [server]$ mkdir tmp
    [server]$ cd tmp
    [server]$ wget
    [server]$ tar zxvf Python-2.7.7.tgz 
    [server]$ cd Python-2.7.7
    [server]$ ./configure --prefix=$HOME/opt/python-2.7.7 [server]$ make [server]$ make install
    • These commands install your local version of python to /home/<username>/opt/python-2.7.7.
  2. Add the following line to the bottom of your .bashrc file:
    source .bash_profile
  3. To add a path in order to use the new version of Python over the system default, enter the following line to your .bash_profile file which is in your user's directory:
    export PATH=$HOME/opt/python-2.7.7/bin:$PATH
  4. Run the following command to update this file:
    [server]$ . ~/.bash_profile
  5. Check which version of Python you're now using by entering the following command:
    [server]$ which python
    It should respond with this:

    If it responds with a path like /usr/bin/python, then the newly downloaded copy is not being used. Most often this is due to the .bash_profile not being updated correctly. Try logging out and back in again. If necessary, repeat the steps above.

Installing pip

In order to install various python packages, and Django in particular, you will need pip. You may find it is already available since Python 2.7.9 and later (for Python 2) and Python 3.4 and later (for Python 3) now include it by default. To find out:

  1. Log into your server via SSH.
  2. Check to see if pip exists.
    [server]$ which pip

If a path is listed, such as /usr/bin/pip, you are able to use pip to install virtualenv.

If it's not installed, proceed with the following steps:

  1. Log into your server via SSH.
  2. Run the following command to download the file to your /tmp directory:
    [server]$ curl > ~/tmp/
  3. Run the following command to install pip:
    [server]$ python ~/tmp/

Install virtualenv using pip

In order to "freeze" the package versions for your website (so they don't automatically update when you set up another site at a later date), install virtualenv:

[server]$ pip install virtualenv

Setting up your Django site in virtualenv

  1. Run the following command to set up a new virtualenv within your domain’s directory:
    [server]$ virtualenv $HOME/<domain>/env
    • This creates a local copy of your environment specific to this website.
  2. To activate the local environment in order to make sure you're working with the right versions of your tools and packages while working on this website, run the following command:
    [server]$ source $HOME/example/env/bin/activate
    • Replace 'example' with your domain name.
    • The name of the current virtual environment now appears on the left of the prompt. For example, (env)[servername]$ to let you know that it’s active.
    • If you now run ‘which python’, you’ll see it’s using the virtualenv install of python which is in the domain’s /env folder.
    • From now on, any package that you install using pip will be placed in the env folder, isolated from the global Python installation.
    • You can then install packages as usual. For example:
    [server]$ pip install requests
  3. Install a custom version of Django and any other required packages (e.g., MySQL-Python if you're going to use a MySQL database) for your website using pip:
    [server]$ pip install Django
    [server]$ pip install MySQL-python

    If you are finished working in the virtual environment for the moment, you can deactivate it by running the following:

    [server]$ deactivate
    • This puts you back to the system’s default Python interpreter with all of its installed libraries.
    • To delete a virtual environment, just delete /env folder. In this case, it would be:
      [server]$ rm -rf /home/username/

See also