Installing and using a different Python distribution of our choice in shared hosting - server
Every step below need to be done on server through ssh. So, if you don't have an ssh access to your server then contact your service provider to activate it.I hope you selected a Python version to install already. If no then do select one based on your needs and download that particular Python distribution source code from Python official site using wget or curl directly into your server. If you don't have the above tools to download then download the source in your local system and use SFTP or SCP or by any other means and put them in your server.
Here I'm downloading the Python 2.7.8
$ wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
Then unzip the file
$ tar -zxvf Python-2.7.8.tgz
The above command will unzip the source code into a directory named Python-2.7.8. Now going ahead into the source code directory,
$ cd Python-2.7.1
~/Python-2.7.8$
create a directory where you want your Python to be installed.
~/Python-2.7.8$ mkdir ~/.localpython
Now we are ready to install our new Python distribution.
Installing:
Lets configure the Python to install in our custom path by passing our custom installation path through command line argument 'prefix'.
~/Python-2.7.8$ ./configure --prefix=/home/user/.localpython
In the above command replace the user with your user name or replace the whole prefix path with the installation path of your choice.
Then run make,
~/Python-2.7.8$ make
Skip this section if you already have 'make' installed in your server [most of the case]:
If you don't have 'make' already installed(only for deb based systems), then you could install a local version of it.
Download it from here.
Then create a directory to install make in local environment
$ mkdir ~/.localmake
Then install the make from downloaded deb file as
$ dpkg-deb -x make.deb /home/user/.localmake
It will install the make application inside the ~/.localmake and now you could use this make for our Python build by calling the make as,
$ /home/user/.localmake/usr/bin/make
Then finally, installation:
~/Python-2.7.8$ make install
[ ~/Python-2.7.8$ /home/user/.localmake/usr/bin/make install ] #for users with local make
Now, we can check our new Python distribution by running
~/Python-2.7.8$ /home/user/.localpython/bin/python
Python 2.7.8 (default, Apr 3 2015, 15:18:55)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The above command will open up our installed Python distribution which it will print in the first line [version].
We have successfully installed our required Python version/distribution. Yet, we have to solve another issue.
Every time we wanted to use our Python version, we need to call it with the full path of the binary, as the default 'python' command would open the default Python version that was installed system wide. In most cases, we are about to run Python applications by just calling the command 'python' and it's the default method for most programs too. So, lets solve it by creating a symlink for python for our installed version.
create a local bin directory,
$ mkdir ~/bin
Then create symlink ,
$ ln -s ~/.localpython/bin/python ~/bin/python
$ ln -s ~/.localpython/bin/python-config ~/bin/python-config
Also, some of the python programs call python with 'python2.7' command, so lets create a symlink for it too.
$ ln -s ~/.localpython/bin/python2.7 ~/bin/python2.7
$ ln -s ~/.localpython/bin/python2.7-config ~/bin/python2.7-config
Now, add ~/bin to our PATH,
$ export PATH=~/bin:$PATH
Then test if everything works fine by opening Python interpreter.
$ python
Python 2.7.8 (default, Apr 3 2015, 15:18:55)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
and
$ python2.7
Python 2.7.8 (default, Apr 3 2015, 15:18:55)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
or by issuing the command,
$ which python
/home/user/bin/python
Add this path permanently by putting it into ~/.bashrc file so that it will work on every further sessions.
$ nano .bashrc
and add the below line and save it with CTRL+x.
$ export PATH=~/bin:$PATH
Finally activate the modified .bashrc file
$ source ~/.bashrc
Installing pip:
Download the Python script get-pip.py and run it.
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py
The above script will automatically download and install pip and setup tools for us. Once it has been installed you could install whatever the packages you need using pip. Before that let's check,
$ pip freeze