Install Python on Linux without Sudo

I was running my machine learning project on a Linux server and needed to install Python 3.8 for some packages. I decided to install natively to keep everything under control, see this nice blog. Without sudo right, things get a little different from standard installation but I found this nice blog by Bobby Durrett making this super simple.

[hous@ada-25:Downloads] $ wget https://www.python.org/ftp/python/3.8.18/Python-3.8.18.tgz
Python-3.8.18.tgz   100%[===================>]  26.07M  --.-KB/s    in 0.1s    
2024-04-20 12:36:05 (230 MB/s) - ‘Python-3.8.18.tgz’ saved [27337549/27337549]
[hous@ada-25:Downloads] $ 
  • Unzip it
  • Go to the folder
[hous@ada-25:Downloads] $ tar zxfv Python-3.8.18.tgz 
Python-3.8.18/
Python-3.8.18/Lib/
......
Python-3.8.18/config.guess
Python-3.8.18/Makefile.pre.in
[hous@ada-25:Downloads] $ cd Python-3.8.18/
[hous@ada-25:Python-3.8.18] $ 
  • Check the absolute path of home directory
  • Make a directory where you want to put your python. I called my .localpython3.8 (name it whatever you want) and make it under the home directory.
  • Configured the Python make
[hous@ada-25:Python-3.8.18] $ pwd
/u/hous/Downloads/Python-3.8.18
[hous@ada-25:Python-3.8.18] $ mkdir /u/hous/.localpython3.8
[hous@ada-25:Python-3.8.18] $ ./configure --prefix=/u/hous/.localpython3.8
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
......
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
[hous@ada-25:Python-3.8.18] $ 
  • Make and make altinstall (see difference between install and altinstall here)
[hous@ada-25:Python-3.8.18] $ make
gcc -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall  
...
[hous@ada-25:Python-3.8.18] $ make altinstall
......
Successfully installed pip-23.0.1 setuptools-56.0.0
[hous@ada-25:Python-3.8.18] $ 

Now we can already use ~/.localpython3.8/bin/python3.8 to run python

[hous@ada-25:~] $ ~/.localpython3.8/bin/python3.8
Python 3.8.18 (default, Apr 20 2024, 12:51:15) 
[GCC 13.2.1 20240316 (Red Hat 13.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[hous@ada-25:~] $ 

Perfect! Just to make life easier, we go to ~/.localpython3.8/bin/ and link python3.8 to python

[hous@ada-25:Python-3.8.18] $ cd ~/.localpython3.8/bin
[hous@ada-25:bin] $ ln -s python3.8 python
[hous@ada-25:bin] $ ln -s pip3.8 pip
[hous@ada-25:bin] $ 

Now, we only need to type ~/.localpython3.8/bin/python. Still a bit inconvenient, so next we add the folder path to PATH variable (abundant nice tutorial online explaining this). This is not just for convenient but avoid you running the wrong python.

export PATH=/u/hous/.localpython3.8/bin:$PATH to .bashrc file.

[hous@ada-25:Python-3.8.18] $ cd ~
[hous@ada-25:~] $ vi .bashrc
# user's bashrc
#
export PATH=/u/hous/.localpython3.8/bin:$PATH
...
[hous@ada-25:~] $

After this, we logout and login. Now we can simply type python and check the installation.

[hous@ada-25:~] $ python
Python 3.8.18 (default, Apr 20 2024, 12:51:15) 
[GCC 13.2.1 20240316 (Red Hat 13.2.1-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[hous@ada-25:~] $ pip -V
pip 23.0.1 from /u/hous/.localpython3.8/lib/python3.8/site-packages/pip (python 3.8)
[hous@ada-25:~] $ pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 56.0.0

[notice] A new release of pip is available: 23.0.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
[hous@ada-25:~] $ 

We are done! I usually then create a virtual environment and play with my machine learning codes, see here why venv is a good idea.

Reference