Python: Install Python 3 on local /home directory in Linux

Install Python 3 locally under /home directory without root or sudo permission!

Download

  • Download the tarball of the stable release of Python 3 from its archive https://www.python.org/ftp/python/
  • Uncompress the tarball using tar
tar -xzvf Python-3.7.2.tgz

Install

  • Change directory to Python-3-7-2
cd Python-3.7.2
  • Run configure file
./configure
  • Create directory you want to install Python 3. For example, under my home
mkdir -p $HOME/usr/local
  • Install Python 3
make altinstall prefix=$HOME/usr/local exec-prefix=$HOME/usr/local

wait for install 5 minutes or so. Python 3 will be installed in $HOME/usr/local/bin.

ls $HOME/usr/local/bin

2to3-3.7  easy_install-3.7  idle3.7  pip3.7  pydoc3.7  python3  python3.7  python3.7m  python3.7m-config  pyvenv-3.7

Run Python 3

  • Now you can use Python 3.
/home/jsyu/usr/local/bin/python3.7

Then, Python 3 shell starts.

Python 3.7.2 (default, Jan 28 2019, 13:06:35)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 

To exit, just type

exit()

Setting Virtual Environment

Change directory to /where/you/install/python3/bin directory again.

cd $HOME/usr/local/bin

Create your own Python 3 virtual environment file

ln -s pyvenv-3.7 pyvenv 

Edit pyvenv-3.7 using vi (or other text editor)

vi pyvenv-3.7

At the first line of file, edit the absolute path of your python. So the correct path should be like this

#!/home/rangsiman/usr/local/bin/python3.7

Optional

To make life easier, we do not always want to call Python 3 via its full path. So we can add it to PATH environment variable.

  • Change directory to where Python 3 installed.
cd $HOME/usr/local/bin
  • Create a link
ln -s python3.7 python3
  • Add Python 3.7.2 executable to your $PATH.
echo "# Python 3.7.2" >> ~/.bashrc
echo "export PATH=\$HOME/usr/local/bin:\$PATH" >> ~/.bashrc
  • Activate .bashrc
source ~/.bashrc 
  • Now you can run Python 3 anywhere.
python3


Rangsiman Ketkaew