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:
- 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- 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- Build and install locally:
make -j$(nproc)
make install- Verify that Python is installed:
$PYTHON_HOME/python39/bin/python3 --version- Ensure
pipis 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- Add local Python bin directory to your PATH:
export PATH=$PATH:$PYTHON_HOME/python39/binTo make this permanent, add the above line to your ~/.bashrc or ~/.profile.
7. Verify pip availability:
pip3 --version- 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>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 .venvsource .venv/bin/activateAfter activation, your shell prompt will typically change to indicate the virtual environment is active.
python --version
pip --versionBoth should point to the locally installed python version environment.
pip install <package_name>deactivate