Skip to content

Instantly share code, notes, and snippets.

@wting
Created May 31, 2012 00:16
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save wting/2839765 to your computer and use it in GitHub Desktop.
Save wting/2839765 to your computer and use it in GitHub Desktop.
Dreamhost Python Setup script
#!/usr/bin/env bash
# Written by William Ting for the following blog post:
# http://williamting.com/posts/2012/04/18/set-up-python-and-django-on-dreamhost/
rcfile="${HOME}/.bashrc"
version="2.7.3"
setuptools_version="2.7"
tmp_dir="${HOME}/tmp-${RANDOM}"
if [[ ${#} == 0 ]]; then
echo "Option --pythonbrew or --source required."
exit 1
fi
function install_tools {
# Install setuptools
wget http://pypi.python.org/packages/${setuptools_version}/s/setuptools/setuptools-0.6c11-py${setuptools_version}.egg
sh ./setuptools--0.6c9-py${setuptools_version}.egg
# Install pip
curl -L https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
# Install django
#pip install django
}
if [[ ${1} == "--pythonbrew" ]]; then
mkdir ${tmp_dir} || exit 1
cd ${tmp_dir}
# Install pythonbrew
curl -L https://raw.github.com/utahta/pythonbrew/master/pythonbrew-install | bash
# Modify rcfile:
echo "# Load pythonbrew" >> ${rcfile}
echo "alias pb='pythonbrew'" >> ${rcfile}
echo "export PYTHONPATH=~/.pythonbrew/pythons/Python-${version}/lib" >> ${rcfile}
echo "[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc" >> ${rcfile}
echo "" >> ${rcfile}
alias pb='pythonbrew'
export PYTHONPATH=~/.pythonbrew/pythons/Python-${version}/lib
[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc
# Install Python
pythonbrew install ${version}
pythonbrew switch ${version}
# Install commonly used tools, django
install_tools
# Initialize virtualenv
pythonbrew venv init
# Wrapup
cd ${HOME}
rm -fr ${tmp_dir}
echo
echo "pythonbrew environment installed, pythonbrew is aliased to 'pb'."
echo
echo "To remove this environment, delete ~/.pythonbrew and remove the lines from your ${rcfile}."
echo
echo "Refer to pythonbrew documentation for more details:"
echo
echo " http://pypi.python.org/pypi/pythonbrew/"
echo
echo "Please run:"
echo
echo " source ${rcfile}"
echo " pb switch ${version}"
echo
elif [[ ${1} == "--source" ]]; then
mkdir ${tmp_dir} || exit 1
cd ${tmp_dir}
# Install Python
wget http://python.org/ftp/python/${version}/Python-${version}.tgz
tar -xvzf Python-${version}.tgz
cd Python-${version}
./configure --prefix=$HOME/opt/python-${version}
make && make install
# Modify rcfile:
echo "# Load custom python installation" >> ${rcfile}
echo -e "export PATH=~/opt/python-${version}/bin:\${PATH}" >> ${rcfile}
echo "export PYTHONPATH=~/opt/python-${version}/lib" >> ${rcfile}
echo "" >> ${rcfile}
export PATH=~/opt/python-${version}/bin:\${PATH} >> ${rcfile}
export PYTHONPATH=~/opt/python-${version}/lib >> ${rcfile}
# Install commonly used tools, django
install_tools
# Install virtualenv
pip install virtualenv
# Wrapup
cd ${HOME}
rm -fr ${tmp_dir}
echo
echo "Custom Python environment installed to ~/opt/python-${version}"
echo
echo "To remove this environment, delete ~/opt/python-${version} and remove the lines from your ${rcfile}."
echo
echo "Please run:"
echo
echo " source ${rcfile}"
echo
else
echo "Invalid argument. Option --pythonbrew or --source required."
exit 1
fi
@klrmn
Copy link

klrmn commented Oct 5, 2012

on line 18, 0.6c9 needs to be replaced with 0.6c11

@nehalecky
Copy link

Hey @wting, thanks for the script.

Per your instructions at the blog posting, I ran it, and installed without a hitch. So nice to see an updated python installation in my DreamHost env.

Now, I am trying to upgrade some package dependencies for my application, and keep getting a timeout error with every attempt to install something via pip, ie:

$ pip install django

fails with error:

Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0MB): 94kB downloaded
Exception:
Traceback (most recent call last):
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/basecommand.py", line 139, in main
    status = self.run(options, args)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/commands/install.py", line 266, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/req.py", line 1033, in prepare_files
    self.unpack_url(url, location, self.is_download)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/req.py", line 1161, in unpack_url
    retval = unpack_http_url(link, location, self.download_cache, self.download_dir)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/download.py", line 554, in unpack_http_url
    download_hash = _download_url(resp, link, temp_location)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/site-packages/pip/download.py", line 458, in _download_url
    chunk = resp.read(4096)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/socket.py", line 380, in read
    data = self._sock.recv(left)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/httplib.py", line 561, in read
    s = self.fp.read(amt)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/socket.py", line 380, in read
    data = self._sock.recv(left)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/ssl.py", line 241, in recv
    return self.read(buflen)
  File "/home/nehalecky/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/ssl.py", line 160, in read
    return self._sslobj.read(len)
SSLError: The read operation timed out

It seems that this is due to the download being slow? Any tips for how to circumvent this?

Thanks a lot!

@guinunez
Copy link

didn' t installed pip, I had to run line 17 a little different:

wget http://pypi.python.org/packages/${setuptools_version}/s/setuptools/setuptools-0.6c11-py${setuptools_version}.egg --no-check-certificate

and on line 18, 0.6c9 needs to be replaced with 0.6c11 as klrmn said.

Other than that, excellent script

@kmcodes
Copy link

kmcodes commented Jun 12, 2014

Can someone teach a noob how to run this script?

@NathanFlurry
Copy link

@kmcodes - Upload it to your server, SSH to it, and say "bash dreamhost_python_setup.sh --pythonbrew". (Make sure you're in the path with the SH file.)

@TomSchober
Copy link

Doing this on a Virtual Private Server (VPS) results in the following error:
bash dreamhost_python_setup.sh --source
...

gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE
-DSVNVERSION=""LC_ALL=C svnversion .""
-DHGVERSION=""LC_ALL=C hg id -i .""
-DHGTAG=""LC_ALL=C hg id -t .""
-DHGBRANCH=""LC_ALL=C hg id -b .""
-o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
hg identify: option -i not recognized
hg identify: option -t not recognized
hg identify: option -b not recognized
./Modules/getbuildinfo.c: In function ‘Py_GetBuildInfo’:
./Modules/getbuildinfo.c:45: error: missing terminating " character
./Modules/getbuildinfo.c:45: error: expected expression before ‘)’ token
./Modules/getbuildinfo.c:46: error: missing terminating " character
./Modules/getbuildinfo.c:46: error: missing terminating " character
./Modules/getbuildinfo.c:47: error: missing terminating " character
./Modules/getbuildinfo.c:47: error: missing terminating " character
./Modules/getbuildinfo.c:53: error: ‘buildinfo’ undeclared (first use in this function)
./Modules/getbuildinfo.c:53: error: (Each undeclared identifier is reported only once
./Modules/getbuildinfo.c:53: error: for each function it appears in.)
./Modules/getbuildinfo.c: In function ‘_Py_hgversion’:
./Modules/getbuildinfo.c:72: error: missing terminating " character
./Modules/getbuildinfo.c:72: warning: ‘return’ with no value, in function returning non-void
./Modules/getbuildinfo.c: In function ‘_Py_hgidentifier’:
./Modules/getbuildinfo.c:79: error: missing terminating " character
./Modules/getbuildinfo.c:79: error: expected expression before ‘;’ token
./Modules/getbuildinfo.c:83: error: missing terminating " character
./Modules/getbuildinfo.c:83: error: expected expression before ‘;’ token
make: *** [Modules/getbuildinfo.o] Error 1
dreamhost_python_setup.sh: line 17: wget: command not found
dreamhost_python_setup.sh: line 18: sh: command not found
dreamhost_python_setup.sh: line 21: curl: command not found
dreamhost_python_setup.sh: line 21: python: command not found
dreamhost_python_setup.sh: line 22: -check-certificateuu: command not found
dreamhost_python_setup.sh: line 98: pip: command not found
dreamhost_python_setup.sh: line 102: rm: command not found

@zbagley
Copy link

zbagley commented Feb 14, 2017

For anyone attempting to install/upgrade Python/Packages on their DreamHost (or any other) VPS: This script is massively outdated, and you're sure to run into plenty of errors (from the start the links don't work anymore).

If you found yourself on this page because of version issues, dependency issues, or something similar I'd highly recommend installing fresh. Chances are if you're here you're not an expert who builds massive Python projects for a living, but probably just someone just starting with Python or someone who utilizes Python with simple scripts (I love python's SciPy and NumPy, and write tons of scripts utilizing these modules). This is undoubtedly the quickest, lightest weight, and simplest way to get your Python scripts running.

SSH to your server

Python3
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
OR
Python2
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
The most recent version can be found here: https://www.python.org/downloads/ (just copy link location and replace)

Open/untar file (rest will be for Python3, but it's the same for 2)
tar -xvf Python-3.6.0.tar.xz

Go to directory
cd Python-3.6.0

Prepare config for make/install
./configure --prefix=$HOME/.local

Make and install:
make
make install

This should install the most recent version for your VPS user alongside the root version.

Next you need add the path to your environment.

cd $home
vi .bash_profile

While in vi press insert to edit, and add the following anywhere on empty lines.
# Python
export PATH="$HOME/.local/bin:$PATH"
type :write then :quit in order to write and leave vi

Source your new profile
'source ~/.bash_profile'

Voila! You can now start using the most recent version of Python on your VPS. Check the version with python3 -V. There's lots of guides for setting up virtual environments, but here's a link to the official Python3 doc: https://docs.python.org/3/library/venv.html

Cheers, hope this helps.

@ryanwc
Copy link

ryanwc commented May 6, 2017

@zbagley thanks, very useful

@NinjyaMaster
Copy link

@zbagley it doesn't install pip for me.

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