Created
February 22, 2023 15:54
-
-
Save joba-1/1b0ae5514cc2e448b184f6afbf29f6df to your computer and use it in GitHub Desktop.
get, build and install python from python.org on linux
This file contains 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
#!/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'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should work for any linux as long as relevant development environment and libraries are available. Tested on opensuse 15.x which needs
Download script and use:
To use directly from github: