How to install and configure Python on a hosted server
Developers know that you can’t spell LAMP (Linux, Apache, MySQL, Perl/PHP/Python) on many systems without Python. And although, you are probably well aware that LAMP is the server stack powering today’s Internet, you might be wondering why you should pay attention to Python. Read on for more about the power of Python and how to install and configure this high-level programming language on a hosted server.
What is Python?
How to check whether Python is already installed
1. Run the following from your ssh terminal:
1
|
$ ls -la /usr/bin/python
|
2. Use the following command to see which version of Python you’re working with:
1
|
$ Python -V
|
How to check which version of Python you are running
What you’ll need to install and configure Python
Which version of Python to use
How to install Python 3
1. Transfer the compression version of the files to your server.
1
|
$ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
|
2. Decompress the files with the following command:
1
|
$ tar xvzf Python-3.4.3.tgz
|
3. Go into that directory with:
1
|
$ cd Python-3.4.3
|
4. Once inside that directory, install your new version of Python.
1
|
$ ./configure --prefix=$HOME/.local
|
5. Then run this command:
1
|
$ make
|
6. And follow that up with:
1
|
$ make install
|
- pip: Python’s recommended tool for installing Python packages.
- setuptools: Enables you to download, build, install, and uninstall Python packages.
1. Go into your Bash profile configuration file:
1
2
|
$cd $home
$ vi .bash_profile
|
2. With an editor, add the following lines, then save the file:
1
2
|
# Python 3
export PATH="$HOME/.local/bin:$PATH"
|
3. Run the following to get your environment up to date:
1
|
$ source ~/.bash_profile
|
4. Then run the following from the shell and you should see Python 3.4.3:
1
|
$ python3 -V
|
Why not just run “python”?
How to create a virtual Python environment
1. Install virtualenv for all users on your server with this basic command:
1
|
$ python3 -m pip install virtualenv
|
2. Or, if you’d rather keep this to the current user, use this command:
1
|
$ python -m pip install –user virtualenv
|
3. Then create a directory to use your new environment:
1
|
$ mkdir PythonTest
|
4. Enter the new directory:
1
|
$ cd PythonTest
|
5. And run the following to create a virtual Python environment, named PythonTest, in the PythonTest directory:
1
|
$ virtualenv PythonTest
|
6. Activate this new Python “container” with:
1
|
$ source PythonTest/bin/activate
|