Skip to content

Instantly share code, notes, and snippets.

@saravanabalagi
Last active April 27, 2022 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saravanabalagi/ac2b2e89c4e2ac46db341a504c45418b to your computer and use it in GitHub Desktop.
Save saravanabalagi/ac2b2e89c4e2ac46db341a504c45418b to your computer and use it in GitHub Desktop.
Download and Install Python without sudo privileges

Install Python

Builds python from source and installs to ~/.local/share/python/python-version without requiring sudo privileges.

Quick Start

Specify python version when calling the script and it will download, build and install the specified python version from python.org

sh ./install_python.sh 3.8.1

Check installation of Python in a new terminal with

python3.8 --version

Note: Make sure you have ~/.local/bin added to you PATH. If not, add export PATH="$HOME/.local/bin:$PATH" to .bashrc

Options

Argument Required Type Default Description
version required string (version number) null Version Number. Example: 3.8.10
install_path optional string (path, will be created if it does not exist) $HOME/.local/share/python/$PYTHON_VERSION Path will be created if it does not exist
--symlink optional null null Symlink will be created at $HOME/.local/bin

If you would like to access directly using python3 or python, you can add symlinks as shown below:

cd ~/.local/bin/
ln -s python3.8 python3
ln -s python3 python
cd -

python --version
set -e
if [ -z $1 ] || [ $1 = "--help" ] || [ $1 = "help" ]; then
echo usage: $0 version [install_path] [--symlink]
exit
fi
PYTHON_VERSION=$1
PYTHON_MAJOR_VERSION=${PYTHON_VERSION%.*}
LOCAL_BIN_DIR=$HOME/.local/bin
PYTHON_INSTALL_DIR=$HOME/.local/share/python/$PYTHON_VERSION
if [ "$2" ]; then
PYTHON_INSTALL_DIR=$2
fi
mkdir -p $PYTHON_INSTALL_DIR $LOCAL_BIN_DIR
curl -k -O https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz
tar zxfv Python-$PYTHON_VERSION.tgz
cd Python-$PYTHON_VERSION
./configure --prefix=$PYTHON_INSTALL_DIR
make
make install
cd -
rm Python-$PYTHON_VERSION.tgz
rm -rf Python-$PYTHON_VERSION
if [ $3 = "symlink" ]; then
ln -s $PYTHON_INSTALL_DIR/bin/python$PYTHON_MAJOR_VERSION $LOCAL_BIN_DIR/python$PYTHON_MAJOR_VERSION
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment