Skip to content

Instantly share code, notes, and snippets.

@BigglesZX
Created January 14, 2012 10:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BigglesZX/1610950 to your computer and use it in GitHub Desktop.
Save BigglesZX/1610950 to your computer and use it in GitHub Desktop.
Installing a custom version of Python on Dreamhost

Dreamhost supplies a fairly old version of Python in their shared hosting environments. It recently became necessary for me to use a newer version, and since this involved a bit of juggling, and since I’m also likely to forget how I did it, I thought I’d detail the steps here. These instructions should work with any Dreamhost shared hosting user, but follow them at your own risk.

The end result of this process is being able to run a current version of Python from your shared hosting user’s shell. It requires compiling, installing and running Python from your home directory rather than the system bin directories.

1. Create a helpful working directory
I chose to install all Python-related stuff in a python/ directory under my user’s home directory.

$ mkdir ~/python
$ cd ~/python

2. Download an up-to-date version of the Python source
At the time of writing this was 2.7.2. You may wish to use a different version.

$ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
$ tar -xzf Python-2.7.2.tgz
$ cd Python-2.7.2

3. Establish the full path to your new Python working directory
When running the Python configure script, we need to supply a prefix that tells the compiler to set everything up locally. We’ll be running Python out of ~/python and we need to know full path to that directory.

$ pwd
/home/biggleszx/python/Python-2.7.2

Keep a mental note of this.

4. Run the Python configure script
We’ll supply the path to our python dir as established above, thus:

$ ./configure --prefix=/home/biggleszx/python

This sets everything up to install locally. You will see lots of technical output, but hopefully everything will progress OK (the Dreamhost environment has just about everything we need to install Python). If your project requires certain special Python features, you may need to install additional dependencies.

5. make, make install
Once ./configure has run, we can perform the actual compilation.

$ make

This might take a while. Once it’s done:

$ make install

This completes the compilation and installs the compiled Python binaries.

6. Reconfigure your environment to use the newly created local Python binaries
If you check your Python version now, you should still see the older Dreamhost-supplied system version:

$ python --version
Python 2.5.2

We need to add our new Python bin dir to our $PATH, the set of directories our shell searches through when we type a command. At the moment, it is picking up the system-wide Python binary (the older version).

$ nano ~/.bash_profile

This file is run whenever you log in to a bash shell. At the end of it, add this line, substituting the path to your own python dir created earlier, being sure to preserve the bin part at the end, since this is where the Python binaries actually reside:

export PATH=/home/biggleszx/python/bin:$PATH

You may also wish to add the same line to your ~/.bashrc file, which is used for non-login shells.

Now we get our shell to read this file and update its configuration. You can either log out and log in again, or type:

$ source ~/.bash_profile

Check your Python version again; it should be updated:

$ python --version
Python 2.7.2

Hurrah!

7. For bonus points: install easy_install and pip
If you’re working with Python packages, you’ll find it helpful to install setuptools and pip.

$ cd ~/python
$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ python ez_setup.py
$ easy_install pip
$ pip --version
pip 1.0.2 from /home/biggleszx/python/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg (python 2.7)

8. Final note: cron jobs
If you want to use Python from the shell, you’re now all set. But if you want to run Python scripts from cron, note this: cron doesn’t know about your $PATH preference for using the local Python binaries we created, so you will need to prefix your cron jobs with a command to read your ~/.bash_profile or ~/.bashrc file. Here’s what your original cron job might have looked like:

0 0 * * * cd /home/biggleszx/maintenance; python maintenance.py 1>/dev/null 2>&1

And here’s what it’ll need to look like now:

0 0 * * * source /home/biggleszx/.bashrc; cd /home/biggleszx/maintenance; python maintenance.py 1>/dev/null 2>&1

Otherwise you’ll find your cron jobs run using the older system-wide Python binary. You can double-check which Python binary is being used by cron by creating a new job to just run python --version, or similar, and inspect the output.

Fin.

@aivoric
Copy link

aivoric commented Nov 3, 2013

Hi James,

Just wanted to say that I found this REALLY useful! :-)

Would appreciate if you could write another one on how to install a python-mysql connector on Dreamhost.

Thank you!

~Aivoric

@jowr
Copy link

jowr commented May 14, 2014

Hi and thanks for sharing. I forked and updated some minor things. feel free to update your code.

@SoulCA
Copy link

SoulCA commented May 20, 2014

great tutorial/guide. Quick question, what is the path to python that should be the first line of each file?

@BigglesZX
Copy link
Author

@SoulCA – using the paths from the listed example it would be /home/biggleszx/python/bin/python

Sorry for the late reply! GitHub doesn't notify gist comments :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment