Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active March 7, 2024 12:05
Show Gist options
  • Save Hansimov/a3887ac0a2ed9edf609331e650effde0 to your computer and use it in GitHub Desktop.
Save Hansimov/a3887ac0a2ed9edf609331e650effde0 to your computer and use it in GitHub Desktop.
Install Python latest version in Linux without sudo access

See this link before you really want to install pure Python by yourself instead of miniconda:

Method 1: (Recommend) Use Pyenv

  • Note: If pyenv install <version> connect to python.org time out
    • Visit this secret gist to setup proxies: Hansimov/proxies.md

Download pyenv-installer and run it:

wget https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer
bash pyenv-installer

Add following lines to ~/.bashrc:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

Relauch the shell to make it work:

bash # Or exec "$SHELL"

Then run the commands:

# Install the latest Python from source code
pyenv install 3.9.6
pyenv versions

# Switch Python version
pyenv global 3.9.6

# Check where Python is actually installed
pyenv prefix
# > /home/admin/.pyenv/versions/3.9.6

# Check the current Python version
python -V
# > Python 3.9.6

References:

Method 2: (Backup) Use .tgz located in npmmirror

Visit following link to get latest Python

Download the .tgz via Chrome browser and drag & drop to target path in server via MobaXterm.

tar -zxvf Python-3.9.6.tgz
# find ./Python-3.9.6/Python -type d | xargs chmod 0755
cd Python-3.11.3
./configure --prefix=$PWD/Python-3.9.6/Python
make
make install
mv ~/Python-3.9.6 ~/bin/

Add Python to path by adding this line to .bashrc:

export PATH=~/bin/Python-3.9.6:$PATH

or add alias to .bash_aliases:

alias python="~/bin/Python-3.9.6/python"

Do not forget to relauch bash to make the path work:

bash

If create Python venv,

python -m venv ~/bin/pyvenv

remove the above export and alias for root python, replace with the following ones:

# .bashrc
export PATH=~/bin/pyvenv/bin:$PATH
export pv=~/bin/pyvenv
# .bash_aliases
alias python="~/bin/pyvenv/bin/python"
bash

Activate python venv:

source $pv/bin/activate

or use alias:

alias sv="source ~/bin/pyvenv/bin/activate"

See the following link for more setup:

References:

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