Install python and pip as local user on shared Linux

A few days ago, I was struggling to install python on a Linux machine, in which I had no root permission. That machine was shared with many other users and there was only an old python version already installed on the machine. After a bit of search and trial, I was able to install a newer version of python on this machine to my local directory.

My goal was to run a program developed under python 2.7 on the Linux server which already had python 2.6 installed. As you can see, it was impossible for me to upgrade python on the system because I had no root permission. The only solution was to install the python 2.7 into my local directory and override the necessary environment variables. Even though this blog aims for python 2.7, I believe it should also be the same for python 3.0. The detailed steps are as follows:

  1. Install python to local directory

Firstly, I create a folder in my home directory, download the python source and extract it

mkdir ~/python      
cd ~/python
tar zxfv Python-2.7.11.tgz
find ~/python -type d | xargs chmod 0755
cd Python-2.7.11

Then I 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.

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 and add the following lines:

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

Finally, refresh the current session by running the command:

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

  1. 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 -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: ~/.local/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.

The solution is originally taken from this article, with some additional details and instructions:

Happy coding!