Python is significantly useful for scientific researchers and industry data analysis. So installing the correct version of Python is critical for your work.

Installing Python as root user is straightforward, I will not discuss this in this post. And it has been explained on the following nice tutorials:

Installing Python 3 on Linux Installing Python 2 on Linux

What we are talking today is: Install Python and pip as local user on shared Linux

You will probabely encounter this kind of situation, since most HPC(High-performance computing) are having shared users, and you are one of these without root privilege to install packages you need.

Install Python and pip as local user on shared Linux

Python3 and pip3


1. Create a folder in home directory, download the python source and extract it

mkdir ~/python
cd ~/python
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
tar zxfv Python-3.6.3.tgz
find ~/python -type d | xargs chmod 0755
cd Python-3.6.3

2. Compiled the source following its guideline

./configure --prefix=$HOME/python
make && make install

Notice the prefix option, it is mandatory for this to work. The value of prefix option is to specify where to put the related output of make command, by default it is in the /usr/local/ and we don't want that so we use our own customized directory.


3. Update the environment variables

Here comes another important step. By the default, if we type python command, it will use the default python of the system. We are going to update the environment variables to force the shell to use our new python. Edit ~/.bashrc_profile using : nano or any other editor you prefered and add the following lines:

export PATH=$HOME/python/Python-3.6.3/:$PATH
export PYTHONPATH=$HOME/python/Python-3.6.3

4. Refresh and check python version:

source ~/.bashrc_profile

You might need to logout and login again for the environment to update properly. At this point, you should be able to see a new python. To check, run this command:

which python

it should show you the path to the python binary file, which is located in your home directory: ~/python/Python-3.6.3/python


5. Install pip3

Pip is a program used to help us easily install python packages, it is similar to rubygems in Ruby world. After installing python locally as described in the first step, it is very easy to install pip.

Run the following command to install pip as a local user

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python - --user

After finishing the installation, we need to update our PATH variable. Open ~/.bashrc_profile and add the following line:

export PATH=$HOME/.local/bin:$PATH

Again, reload the session by the command source ~/.bashrc_profile or logout and login. Then, check if pip command is available:

which pip

It should show a path pointing to your local directory: ~/python/bin

 


Install Python2 and pip will be very similar to the above Python3, incase of unexpected errors, I write done my whole process for your reference.

Python2 and pip


1. Create a folder in home directory, download the python source and extract it

mkdir ~/python
cd ~/python
wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
tar zxfv Python-2.7.11.tgz
find ~/python -type d | xargs chmod 0755
cd Python-2.7.11

2. Compiled the source following its guideline

./configure --prefix=$HOME/python
make && make install

Notice the prefix option, it is mandatory for this to work. The value of prefix option is to specify where to put the related output of make command, by default it is in the /usr/local/ and we don't want that so we use our own customized directory.


3. Update the environment variables

Here comes another important step. By the default, if we type python command, it will use the default python of the system. We are going to update the environment variables to force the shell to use our new python. Edit ~/.bashrc_profile using : nano or any other editor you prefered and add the following lines:

export PATH=$HOME/python/Python-2.7.11/:$PATH
export PYTHONPATH=$HOME/python/Python-2.7.11

4. Refresh and check python version:

source ~/.bashrc_profile

You might need to logout and login again for the environment to update properly. At this point, you should be able to see a new python. To check, run this command:

which python

it should show you the path to the python binary file, which is located in your home directory: ~/python/Python-2.7.11/python


5. Install pip

Pip is a program used to help us easily install python packages, it is similar to rubygems in Ruby world. After installing python locally as described in the first step, it is very easy to install pip.

Run the following command to install pip as a local user

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python - --user

After finishing the installation, we need to update our PATH variable. Open ~/.bashrc_profile and add the following line:

export PATH=$HOME/.local/bin:$PATH

Again, reload the session by the command source ~/.bashrc_profile or logout and login. Then, check if pip command is available:

which pip

It should show a path pointing to your local directory: ~/python/bin

Having both python and pip installed as a local user, you can install any other packages you want without worrying about other parts of the whole system. This is extremely useful in case you want to experiment with new things.

I hope this tutorial is helpful to you. If you have any trouble following the tutorial, please let me know.

Happy Coding!