Installing new version of Python without root

UPDATE 2019: If you have access to sudo, I’d definitely recommend installing python through system manager and using venv i.e.

# install python3.7 
sudo apt install python3.7-venv python3.7-dev
# create virtual environment using python3.7
python3.7 -m venv py37
# activate environment
source py37/bin/activate
# check version
python --version # python -c 'import sys; print(sys.version_info)'
# install new package
pip install numpy
# leave virtual environment
deactivate

Some time ago I was recommending to use Python virtual environment to install local version of Python packages. However this will not solve the issue of outdated version Python in the server your are working in. Here, pythonbrew may be help for you.

# install pythonbrew to ~/.pythonbrew
curl -kL http://xrl.us/pythonbrewinstall | bash

# add to ~/.bashrc to automatically activate pythonbrew
[[ -s "$HOME/.pythonbrew/etc/bashrc" ]] && source "$HOME/.pythonbrew/etc/bashrc"                                                         

# open new terminal tab (Ctrl+Shift+T) or window (Ctrl+Shift+N)

# install python 2.7.10
pythonbrew install 2.7.10

# and enable the new version
pythonbrew switch 2.7.10

# from now on, you can enjoy the version of your choice and install dependencies
which python
#/home/.../.pythonbrew/pythons/Python-2.7.10/bin/python
python --version
#Python 2.7.10
which pip
#/home/.../.pythonbrew/pythons/Python-2.7.10/bin/pip

Virtual environment (venv) in Python

Working on the machines with no root access is sometimes annoying, especially if you depend on multiple Python packages and your distro is somehow outdated… You may find Python virtual environment (venv) very useful.
First, you need to create venv directory structure (this is done only once):

mkdir -p ~/src/venv
cd ~/src/venv
virtualenv py27

Then you can open new BASH terminal and activate your venv by:

source ~/src/venv/py27/bin/activate

After that, you can install / upgrade any packages using pip / easy_install (even including PIP ) ie.

pip install --upgrade pip
pip install --upgrade scipy

Insipired by python-guide.