Skip to content

Instantly share code, notes, and snippets.

@alexolinux
Last active November 23, 2025 00:26
Show Gist options
  • Select an option

  • Save alexolinux/f5371657aae4ccf0382d986a08c8fd0f to your computer and use it in GitHub Desktop.

Select an option

Save alexolinux/f5371657aae4ccf0382d986a08c8fd0f to your computer and use it in GitHub Desktop.
Compiling and installing Python for NonSudo Users

Python Compilation


To have another Python versions for the Linux Users without impacting the operating system, the best approach is to install Python locally in the user's home directory. This method avoids requiring root/sudo access and does not alter the system-wide Python installation.

To install Python (e.g.: 3.9.25) locally for a user without sudo access, including the pip module, follow these steps:

Step-by-step installation of python + pip module

  1. Download Python version source code:
cd /tmp
PYTHON_VERSION="3.9.25"
curl -O https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz
tar -xzvf Python-$PYTHON_VERSION.tgz
cd Python-$PYTHON_VERSION
  1. Configure the build for local installation:

Choose a local installation prefix under your home directory:

PYTHON_HOME="$HOME/.local/usr/python"
mkdir -p "$PYTHON_HOME"
./configure --prefix=$PYTHON_HOME --enable-optimizations
  1. Build and install locally:
make -j$(nproc)
make install
  1. Verify that Python is installed:
$PYTHON_HOME/python39/bin/python3 --version
  1. Ensure pip is installed and updated:

Python Version comes with ensurepip module:

$HOME/python39/bin/python3 -m ensurepip --upgrade
$HOME/python39/bin/python3 -m pip install --upgrade pip
  1. Add local Python bin directory to your PATH:
export PATH=$PATH:$PYTHON_HOME/python39/bin

To make this permanent, add the above line to your ~/.bashrc or ~/.profile. 7. Verify pip availability:

pip3 --version
  1. Use pip to install packages locally without sudo:

You can install packages globally for your local Python with:

pip3 install <package_name>

Or with user site-packages using:

pip3 install --user <package_name>

Extras

After locally compiling and installing python version with pip, you can create a virtual environment (virtualenv) using the built-in venv module that comes with Python 3.3+.

python3 -m venv .venv

Activate the virtual environment

source .venv/bin/activate

After activation, your shell prompt will typically change to indicate the virtual environment is active.

python --version
pip --version

Both should point to the locally installed python version environment.

pip install <package_name>

Deactivate the virtual environment

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