Skip to content

Instantly share code, notes, and snippets.

@mp035
Created July 18, 2024 07:48
Show Gist options
  • Save mp035/a13fcbea0e8ad3d3b0628d24570558f8 to your computer and use it in GitHub Desktop.
Save mp035/a13fcbea0e8ad3d3b0628d24570558f8 to your computer and use it in GitHub Desktop.
Shell script to create a python virtualenv with it's own python version different to any installed python.
#!/bin/bash
set -e
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
# buid interpreter if it does not exist.
PYEXEC="python_interpreter/bin/python3"
if [ ! -f "$PYEXEC" ]
then
# if the interpeter directory exists, it must be corrupt (no interpreter) so delete it.
if [ -d python_interpreter ]
then
rm -rf python_interpreter
fi
mkdir python_interpreter
PYHOME=$(realpath python_interpreter)
PYVER="3.8.19"
PYSRC="Python-$PYVER"
PYTAR="$PYSRC.tar.xz"
echo "$PYTAR"
echo "#########"
if [ ! -f "$PYTAR" ]
then
wget "https://www.python.org/ftp/python/$PYVER/$PYTAR"
fi
tar -xvf "$PYTAR"
cd "$PYSRC"
./configure --enable-optimizations --prefix=$PYHOME
make
make install
cd ..
rm -r "$PYSRC"
fi
PYENV="python_environment"
if [ ! -d "$PYENV" ]
then
sudo apt install -y python3-pip
pip3 install virtualenv
virtualenv --python=$(realpath "$PYEXEC") "python_environment"
fi
"$PYENV/bin/pip" install --upgrade pip
"$PYENV/bin/pip" install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment