Python is one of the most popular programming languages, widely used for web development, data science, automation, and more. This guide will walk you through installing Python on your AnonVM VPS and configuring it for your development projects.
System Requirements
To install Python on your AnonVM VPS, ensure your system meets these requirements:
- Operating System: Ubuntu 18.04+, Debian, or CentOS 7+
- Permissions: Root or sudo privileges
Step 1: Update Your System
Start by updating your package list to ensure compatibility and avoid conflicts.
For Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y
For CentOS:
sudo yum update -y
Step 2: Check the Current Python Version
Many Linux distributions come with Python pre-installed. To check if Python is already available, use:
python3 --version
If Python 3.x is already installed, you can skip to Step 5 to set up a virtual environment. If not, proceed with the installation steps below.
Step 3: Install Python on Ubuntu/Debian
To install the latest version of Python on Ubuntu/Debian:
-
Install Python:
sudo apt install -y python3
-
Install Additional Packages (Optional):
For development, it’s helpful to have
pip
(Python’s package manager) andvenv
(for virtual environments):sudo apt install -y python3-pip python3-venv
-
Verify the Installation:
python3 --version pip3 --version
Step 4: Install Python on CentOS
For CentOS, you can use yum
to install Python:
-
Enable the EPEL Repository (if required):
sudo yum install -y epel-release
-
Install Python:
sudo yum install -y python3
-
Verify the Installation:
python3 --version pip3 --version
If you need a specific version of Python, such as Python 3.8 or Python 3.9, download it from Python’s official website and install manually using the tar file.
Step 5: Set Up a Virtual Environment
A virtual environment allows you to create isolated Python environments for different projects, helping manage dependencies effectively.
-
Create a Project Directory (Optional):
mkdir ~/my-python-project && cd ~/my-python-project
-
Create a Virtual Environment:
python3 -m venv myenv
This creates a folder called
myenv
containing the Python executable and packages for that environment. -
Activate the Virtual Environment:
source myenv/bin/activate
You’ll see
(myenv)
in your terminal, indicating that the virtual environment is active. -
Install Packages Using
pip
:For example, to install
requests
, use:pip install requests
-
Deactivate the Virtual Environment:
To exit the virtual environment:
deactivate
Step 6: Install Essential Python Packages
While Python comes with a vast standard library, some additional packages are commonly used in Python projects. Below are a few examples:
-
Flask - For web development:
pip install Flask
-
Django - For a more full-featured web framework:
pip install Django
-
NumPy and Pandas - For data science and analysis:
pip install numpy pandas
-
Requests - For making HTTP requests:
pip install requests
-
Jupyter Notebook - For interactive data analysis:
pip install notebook
Step 7: Running a Python Script
To test your Python installation, create a simple script and run it.
-
Create a Python File:
nano hello.py
Add the following code:
pythonprint("Hello, AnonVM!")
-
Run the Script:
python3 hello.py
If everything is set up correctly, you should see:
plaintextHello, AnonVM!
Step 8: Setting Python as the Default Version (Optional)
If your VPS has multiple Python versions and you want to set Python 3 as the default:
-
Update the Default Python:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
-
Verify the Change:
python --version
This should show the Python 3 version.
Step 9: Automate Package Installation with a requirements.txt
File
For larger projects, managing dependencies in a requirements.txt
file simplifies the installation process.
-
Generate a
requirements.txt
File:If you already have a virtual environment with installed packages:
pip freeze > requirements.txt
-
Install Packages from
requirements.txt
:For other environments or team members:
pip install -r requirements.txt
This command installs all packages listed in the file with the specified versions.
Step 10: Advanced - Installing Python Using pyenv
If you need to manage multiple Python versions on your VPS, consider using pyenv
.
-
Install
pyenv
Dependencies:For Ubuntu:
sudo apt install -y make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ libncurses5-dev libncursesw5-dev xz-utils tk-dev
-
Install
pyenv
:curl https://pyenv.run | bash
-
Add
pyenv
to PATH:Add these lines to your
.bashrc
:export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)"
-
Install a Specific Python Version:
For example, to install Python 3.9.1:
pyenv install 3.9.1 pyenv global 3.9.1
Verify the installation:
python --version
Conclusion
You have successfully installed and configured Python on your AnonVM VPS. With Python and virtual environments set up, you’re ready to develop, test, and deploy Python applications. If you’re working on a larger project, consider setting up a package manager (like pip
) and using pyenv
to manage different Python versions efficiently.