Python is a versatile and powerful programming language that’s become increasingly popular for web development, data analysis, artificial intelligence, and more. This comprehensive guide will walk you through the process of installing Python 3 and setting up a robust programming environment on a fresh Ubuntu 22.04 server, using the root account.
1: Deploying a Cloud Instance on Shape.host
- Log in to Shape.host Dashboard:
- Navigate to the Shape.host website and log in to your account.
- Create a New Instance:
- Click on the “Create” button located at the top right corner of the dashboard.
- From the dropdown menu, select “Instances”.
- Select Instance Location:
- Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
- Choose a Plan:
- Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
- Select an Operating System:
- Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
- Configure Additional Options:
- (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
- Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
- Click on the “Create instance” button to deploy the instance.
2: Connecting to Your Instance
- Retrieve SSH Credentials:
- Note the IP address of your newly created instance from the Shape.host dashboard.
- Connect via SSH:
- Open a terminal on your local machine.
- Use the following command to connect to your instance:
ssh root@your_instance_ip
- Replace
your_instance_ip
with the actual IP address of your instance.
- Update and Upgrade System Packages
Before we begin, it’s crucial to ensure your system is up-to-date:
apt update && apt upgrade -y
- Install Python 3 and Python3-pip
Let’s install Python 3 and pip, Python’s package installer:
apt install python3 python3-pip -y
After installation, verify the Python version:
python3 --version
You should see output similar to “Python 3.10.x” (where x is the minor version number).
- Install Python3-venv for Creating Virtual Environments
Virtual environments are crucial for managing project dependencies:
apt install python3-venv -y
- Set Up a Python Virtual Environment
It’s a best practice to create a virtual environment for each Python project. Let’s create one:
mkdir ~/python_projects
cd ~/python_projects
python3 -m venv myproject_env
This creates a new virtual environment named “myproject_env”.
- Activate the Virtual Environment
To use the virtual environment, you need to activate it:
source myproject_env/bin/activate
Your prompt should change to indicate that the virtual environment is active.
- Install Python Packages
With the virtual environment activated, you can install packages using pip. For example, let’s install the popular requests library:
pip install requests
- Create a Python Script
Let’s create a simple Python script to test our environment:
nano test_script.py
Add the following content to the file:
import requests
response = requests.get('https://api.github.com')
print(f"GitHub API Status Code: {response.status_code}")
print(f"GitHub API Response: {response.json()}")
Save and exit the file (in nano, press Ctrl+X, then Y, then Enter).
- Run the Python Script
Execute the script:
python test_script.py
You should see output displaying the status code and JSON response from the GitHub API.
- Deactivate the Virtual Environment
When you’re done working on your project, you can deactivate the virtual environment:
deactivate
- Install Additional Python Development Tools
For a more comprehensive development environment, consider installing these tools:
apt install build-essential libssl-dev libffi-dev python3-dev -y
These packages provide necessary components for building Python modules and working with various libraries.
- Set Up an Integrated Development Environment (IDE)
While you can write Python code in any text editor, an IDE can significantly enhance your productivity. Let’s install Visual Studio Code, a popular choice among Python developers:
apt install software-properties-common apt-transport-https wget -y
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | apt-key add -
add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
apt update
apt install code -y
- Configure VS Code for Python Development
Once VS Code is installed, you can open it by typing code
in the terminal. Install the Python extension from the Extensions marketplace within VS Code for enhanced Python support.
- Set Up Version Control with Git
Version control is crucial for any programming project. Let’s install Git:
apt install git -y
Configure Git with your information:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
- Create a .gitignore File
In your project directory, create a .gitignore file to prevent unnecessary files from being tracked by Git:
nano .gitignore
Add these common Python-related entries:
__pycache__/
*.py[cod]
*$py.class
venv/
.env
- Initialize a Git Repository
In your project directory:
git init
git add .
git commit -m "Initial commit"
By following these steps, you’ve set up a comprehensive Python development environment on your fresh Ubuntu 22.04 system. You now have Python 3 installed, along with tools for package management, virtual environments, and even an IDE for efficient coding.
For those looking for a more streamlined experience in setting up development environments, Shape.host offers excellent Cloud VPS services. Their Ubuntu-based VPS solutions provide the perfect foundation for setting up Python development environments, allowing you to focus on coding rather than server management. With Shape.host’s Linux SSD VPS, you can enjoy the flexibility of a customizable environment with the convenience of a clean, fresh Ubuntu installation, making it an ideal choice for Python developers of all levels.