How to Install and Use Curl on Linux

Published on  

10 min read

How to Install and Use Curl on Linux

Learn how to install and use Curl on Linux with our comprehensive guide. Discover the power of Curl for data transfers, web scraping, and more.

Welcome to our comprehensive guide on installing and using Curl on Linux! Curl is an incredibly versatile and powerful command-line tool that allows you to transfer data across various protocols. Whether you need to make HTTP requests, download files, or scrape data from websites, Curl has got you covered. In this tutorial, we'll take you through the step-by-step process of installing Curl on Linux and explore its extensive functionality. Let's dive in!

Understanding Curl

Before we get started with the installation, let's understand what Curl is all about. Curl is a command-line utility that enables you to send and receive data using various network protocols. Its primary purpose is to facilitate the transfer of data, making it an invaluable tool for Linux users.

Curl supports a wide range of protocols, including HTTP, HTTPS, FTP, SMTP, and more. It's lightweight, fast, and highly customizable, making it a favorite among developers, system administrators, and enthusiasts alike.

Preparing the Linux Environment

To ensure a smooth installation process, let's first check if Curl is already installed on your Linux system. Fortunately, many Linux distributions come with Curl pre-installed. You can simply open a terminal and enter the following command to verify:

curl --version

If Curl is installed, the command will display its version information. However, if Curl is not found or bash: curl: command not found or an outdated version is displayed, it's recommended to update your package manager before proceeding with the installation.

Installing Curl

Now, let's move on to the installation process. The method of installing Curl may vary depending on your Linux distribution. We'll cover the installation steps for some popular distributions:

Ubuntu and Debian

For Ubuntu and Debian-based systems, open a terminal and execute the following commands:

Update the package manager:

sudo apt update

Install Curl:

sudo apt install curl

CentOS

If you're using CentOS, you can install Curl by following these steps:

Update the package manager:

sudo yum update

Install Curl:

sudo yum install curl

Fedora

For Fedora users, the installation process is as follows:

Update the package manager:

sudo dnf update

Install Curl:

sudo dnf install curl

Once the installation is complete, you'll have Curl ready to use on your Linux system.

Verifying the Installation

To ensure that Curl has been installed successfully, we can perform a quick verification test. Open a terminal and run the following command:

curl --version

If Curl is properly installed, you should see the version information displayed on your screen. This confirms that Curl is ready for action!

Basic Curl Usage

Now that we have Curl installed, let's explore some of its basic functionalities. Curl commands typically follow a straightforward structure. You specify the URL or endpoint you want to interact with and add any necessary options or flags.

Making GET Requests

One of the most common use cases for Curl is making GET requests to retrieve data from web servers. To make a simple GET request, use the following command:

curl https://example.com

This command sends a GET request to the specified URL (in this case, https://example.com) and displays the response on your terminal.

Making POST Requests

If you need to send data to a web server, you can use Curl to make POST requests. Here's an example:

curl -X POST -d 'name=John&age=25' https://example.com/api

In this command, we use the -X flag to specify the HTTP method as POST, and the -d flag to provide the data to be sent in the request body. Adjust the URL (https://example.com/api) and data payload according to your needs.

Exploring Command-Line Options

Curl provides a wide range of command-line options and flags to customize your requests. Here are a few commonly used ones:

  • -O: Downloads a file from the given URL and saves it with the same name as the remote file.
  • -d: Sends data as the request body when making POST requests.
  • -H: Adds custom headers to the request.
  • -i: Displays both the response headers and body.

Feel free to explore the Curl documentation to discover additional options and tailor your commands according to your requirements.

Advanced Curl Features and Options

Curl offers a plethora of advanced features and options that can enhance your experience. Let's explore a few of them:

Changing File Name while Downloading

Use either the -o or -O arguments when running curl to download a file. You can specify the name of the stored file using the lowercase -o option:

curl -o new_file.txt https://example.com/file.txt

Curl saves the file with its original filename when run with uppercase -O:

curl -O https://example.com/file.txt

Get the Headers

To see the headers of the website URL https://www.rexposed.com, you can use -I or --head option:

curl -I https://www.rexposed.com

Authentication

To authenticate with a server, you can use the --user option followed by the username and password:

curl --user username:password https://example.com

Cookies and Sessions

Curl can handle cookies and maintain sessions. You can save cookies to a file using the -c flag and load them for subsequent requests using the -b flag:

curl -c cookies.txt https://example.com
curl -b cookies.txt https://example.com

Downloading/Uploading Files

Need to download or upload files? Curl has you covered. You can easily download files from a remote server or even upload files using various protocols such as FTP.

To download a file, use the following command:

curl -O https://example.com/file.txt

To upload a file, you can utilize the -T flag:

curl -T /path/to/local/file.txt ftp://example.com/

Following Redirects

Curl automatically follows redirects by default. However, if you want to limit the number of redirects, you can use the --max-redirs option:

curl --max-redirs 3 https://example.com

Setting Custom Headers

You can also set custom headers to send with your requests. To set custom headers, use the -H flag multiple times:

curl -H "Authorization: Bearer token" -H "Content-Type: application/json" https://example.com/api

Handling Different Data Formats

Curl supports various data formats, including JSON and XML. You can send data in these formats using the -d flag and specifying the appropriate content type using the -H flag:

curl -d '{"name": "John", "age": 25}' -H "Content-Type: application/json" https://example.com

Changing User Agent

To change the User Agent while accessing the URL via Curl, you need to use -A flag:

curl -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' https://www.example.com

Passing HTTP Referer

You can pass custom HTTP Referer while accessing the URL via Curl, to do that use --referer followed by the referer URL:

curl --referer https://www.referer.com https://www.example.com

Uploading Files using HTTP POST Request

You can also upload files using HTTP POST Request via Curl. To do that you need to use -F flag followed by the file key name and file path:

curl -F 'file_key_name=@/home/developer/file.txt https://www.example.com/upload/

Using Curl for Web Scraping

One of the powerful applications of Curl is web scraping. With Curl, you can extract data from websites in a programmatic manner. Although there are dedicated web scraping libraries available, Curl can be a handy tool for quick and simple scraping tasks.

For example, let's say you want to extract the title from a webpage. You can use Curl in combination with other command-line tools like grep and sed:

curl https://example.com | grep -o '<title>[^<]*' | sed 's/<title>//'

This command fetches the webpage, searches for the <title> tag, and extracts the content within it using grep and sed.

Troubleshooting and Tips

While using Curl, you may encounter certain errors or issues. Here are a few tips to help you troubleshoot common problems:

  • If you're behind a proxy server, you can set the proxy using the --proxy option.
  • If you're experiencing SSL certificate errors, you can bypass certificate verification using the -k flag (not recommended for production use).
  • If you encounter timeouts, you can increase the timeout value using the --max-time option.

Additionally, here are a few tips to optimize your Curl usage:

  • Use the --silent or -s flag to suppress unnecessary output and make your scripts cleaner.
  • Employ parallel requests with the --parallel option to improve performance when dealing with multiple requests simultaneously.

Conclusion

Congratulations! You've successfully learned how to install and use Curl on Linux. We covered the installation steps for various Linux distributions, explored basic and advanced Curl usage, and even touched on web scraping with Curl. Curl is an incredibly powerful tool that can simplify data transfers and streamline your workflow.

Remember to refer back to this guide whenever you need a quick refresher or face any troubleshooting challenges. Experiment with Curl's vast array of options and unleash its full potential.

Now it's your turn to put Curl to work! Start incorporating Curl into your Linux projects, automate data transfers, and explore the limitless possibilities. Happy Curling!

Anchor Links:

About the Author

Rexposed Staff

Was this helpful?