Install Ubuntu and Set Up a Flask API on Linode

Ubuntu is one of the most popular and user-friendly Linux distributions for servers. Installing it on a cloud server provides an efficient way to build and deploy applications. This guide walks you through setting up Ubuntu on your Linode server and building a simple Flask API. By the end, you’ll have a functional Ubuntu server running Flask, ready to handle API requests.

To get started, you can also take advantage of Linode’s $100 credit offer, which allows you to use a server for free for 60 days. Just use this Linode link to claim your credit and begin your Ubuntu journey.

Step 1: Connecting to Your Ubuntu Server

After setting up your Linode server with Ubuntu, you can connect to it using SSH. Open your terminal and run the following command, replacing the IP address with your server’s public IP address:

ssh root@your_server_public_ip

For example, if your server’s public IP is 172.105.35.217, your command will look like this:

ssh [email protected]

This will connect you to your Ubuntu server, allowing you to manage and configure it from your terminal.

Step 2: Updating and Upgrading Ubuntu

After logging into your server, it’s important to make sure that the system is up to date. This will help ensure your Ubuntu server has the latest security patches and software versions. To update and upgrade the server, run the following commands:

sudo apt update
sudo apt upgrade

These commands will download and install the latest updates for Ubuntu and its installed packages.

Step 3: Installing Python and Pip

Python is widely used for server-side web development, and you’ll need it to run your Flask application. First, install pip, Python’s package manager, by running the following command:

apt install python3-pip

This will install pip for Python 3, allowing you to easily install and manage Python libraries on your server.

Step 4: Installing Flask on Ubuntu

Flask is a powerful, lightweight Python web framework that’s ideal for creating simple APIs. To install Flask on your Ubuntu server, use this command:

apt install python3-flask

With Flask installed, you’re ready to start building web applications and APIs.

Step 5: Building and Running Your Flask API

Here’s an example of a simple Flask application that sets up an API to return a list of users:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/users', methods=['GET'])
def get_users():
    users = [
        {"id": 1, "name": "John Doe"},
        {"id": 2, "name": "Jane Doe"}
    ]
    return jsonify(users)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Save this code in a file called app.py and run it with this command:

python3 app.py

This will start your Flask API, and you can access it at the following URL:

http://your_server_public_ip:5000/users

For example, if your server’s IP is 172.105.35.217, the URL would be:

http://172.105.35.217:5000/users

When you visit this URL in your browser or API client, you’ll see a list of users in JSON format.

By following these steps, you’ll have an Ubuntu server running Flask in no time. Using Ubuntu provides a solid foundation for running applications, and its reliability makes it a great choice for both development and production environments. Additionally, don’t forget to take advantage of Linode’s $100 free credit for 60 days by using this referral link to explore Ubuntu and build your web applications without upfront costs.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x