Skip to content

Instantly share code, notes, and snippets.

@joba-1
Created February 22, 2023 15:54
Show Gist options
  • Save joba-1/1b0ae5514cc2e448b184f6afbf29f6df to your computer and use it in GitHub Desktop.
Save joba-1/1b0ae5514cc2e448b184f6afbf29f6df to your computer and use it in GitHub Desktop.
get, build and install python from python.org on linux
#!/bin/bash
# Get version of python souce from python.org ftp, compile and install in given directory
# Result for target directory $dir and version $ver:
# * python interpreter $dir/python/$ver/bin/python3
# * ready to use virtual environment activated by "source $dir/python/$ver/venv/base/bin/activate"
# * create more virtual environments by "$dir/python/$ver/bin/python3 -m venv $venv_dir
# * source code is in $dir/Python-$ver
# * install log is in $dir/Python-$ver.log
# Needs relevant dev packages installed:
# zypper in tk-devel sqlite3-devel gdbm-devel readline6-devel openssl-devel libffi-devel
if [ -z "$1" ]; then
echo "${0##*/} major.minor[.patch] [targetdir]"
echo "${0##*/} -l"
exit 1
fi
if [ "$1" == -l ]; then
wget -q -O- "https://www.python.org/ftp/python" | awk -F'["/]' '/^<a href="[2-9]/ {print $2}' | sort -V | awk '{printf l; l=$0"\n"}'
exit 0
fi
ver="${1}"
dir="${2-$PWD}"
url="https://www.python.org/ftp/python"
instdir="$dir/python/$ver"
if [ -d "$instdir" ]; then
echo "$0 Target directory '$instdir' exists: aborting"
exit 2
fi
log="$instdir/Python-$ver.log"
echo "Installing python to '$instdir'"
mkdir -p "$instdir"
(
cd "$instdir" && \
wget -O- "$url/$ver/Python-$ver.tar.xz" | tar xJf - && \
cd "Python-$ver" && \
./configure --enable-optimizations --with-lto=full --prefix="$instdir" --libdir="$instdir/lib" && \
make -j8 && \
make install
) | tee "$log"
"$instdir/bin/python3" -m venv "$instdir/venv/base"
. "$instdir/venv/base/bin/activate"
python3 --version
echo "Build and installation log in $log"
echo "Activate virtual environment with '. \"$instdir/venv/base/bin/activate\"'"
echo "Create your own virtual environments with '\"$instdir/bin/python3\" -m venv your-env-path'"
@joba-1
Copy link
Author

joba-1 commented Feb 22, 2023

should work for any linux as long as relevant development environment and libraries are available. Tested on opensuse 15.x which needs

sudo zypper in tk-devel sqlite3-devel gdbm-devel readline6-devel openssl-devel libffi-devel

Download script and use:

wget https://gist.github.com/joba-1/1b0ae5514cc2e448b184f6afbf29f6df/raw/130b173632192b75fc25e6d9de53387645f46df3/getpy.sh
sh getpy.sh 3.9.16

To use directly from github:

wget -q -O- https://gist.github.com/joba-1/1b0ae5514cc2e448b184f6afbf29f6df/raw/130b173632192b75fc25e6d9de53387645f46df3/getpy.sh | sh -s - 3.9.16

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