VPS: Installation of Python
This guide was created with the following products:
(Details may vary with products from different providers but the main concepts remain the same)
Introduction
This guide provides steps for the installation of the Python runtime and venv. These commands must be executed via SSH, if you don't know how to connect to your server via SSH, please use our Initial Access (SSH) guide to learn more.
Prerequisites
Before you install anything on a server it is recommended to run the update command corresponding to your operating system to keep your server secure.
- Ubuntu & Debian
- CentOS
- OpenSUSE
- Fedora
sudo apt update
sudo yum update
sudo zypper update
sudo dnf update
Installation
Most Linux distros come preinstalled with Python however the version might not be up to date or the system might have been installed without some packages. You can check if the python installation exists (python3 --version
) and run the following commands to either update or install the runtime.
- Ubuntu & Debian
- CentOS
- OpenSUSE
- Fedora
// Check version
python3 --version
// Update / install the runtime
sudo apt install python3
// Check version
python3 --version
// Update the runtime
sudo yum install python3
// Check version
python3 --version
// Update the runtime
sudo zypper install python3
// Check version
python3 --version
// Update the runtime
sudo dnf install python3
Running code
Now that you have Python installed on your server, you can start running your Python programs.
Interpreter mode
Running the python3
command will start the Python interpreter. You can start writing any valid Python code after the >>>
prefix and it will be executed after pressing Enter
. You can close the interpreter once you are finished by running exit()
into the console.
Running .py files
In order to run .py
Python files, you can simply use the python3 [filename].py
command, replacing [filename]
with the path to the target file name you wish to run.
Most programs you can find online can be run with the python3 main.py
because main.py
is the common starting point of most Python programs.
Virtual environments
When writing a Python program you might need to install external packages from pip. These can be installed globally and thus be accessible for all .py
scripts or you can create a virtual environment (venv).
Creating the venv
Firstly, navigate to the folder where you wish to setup your venv using cd
and once ready, run python3 -m venv .
which will install the required files at the current location.
Activating & deactivating
To run commands like pip install
inside your venv you need to activate it by running source /bin/activate
. Now your console is only going to run inside the venv and scripts will only have access to locally installed packages.
When you have finished working inside the venv you can go back by running the deactivate
command.
Conclusion
Congratulations, you have successfully installed and configured Python! If you have any further questions or problems, please contact our support team, who are available to help you every day!