Created
May 31, 2012 00:16
-
-
Save wting/2839765 to your computer and use it in GitHub Desktop.
Dreamhost Python Setup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.htmlCheers, hope this helps.