HomeHOWTOSInstall and Use curl on Linux, Mac, and Windows Systems

Install and Use curl on Linux, Mac, and Windows Systems

Today, we will learn how to install and use curl on Linux, Mac, and Windows Systems. But let us first know whet is cURL and its use scenarios.

Curl is a command-line tool used to transfer data to and from a server. It supports many protocols, including HTTP, FTP, SMTP, IMAP, and many more. Curl is widely used in many applications and programming languages to make HTTP requests and receive responses.

Curl is used in many applications and contexts, some of the most common uses include:

  • Automated testing: Curl is often used in the automated testing of web applications to make HTTP requests and verify responses.
  • API development: Developers use Curl to test and debug RESTful APIs, as it allows them to easily send requests and inspect the responses.
  • File transfer: Curl can be used to transfer files over many protocols, including FTP, SFTP, and SCP.
  • Scraping and crawling: Curl can be used for web scraping and crawling, allowing developers to extract data from websites.
  • Downloading files: Curl can be used to download files from the internet, including large files and archives.
  • Debugging network issues: Curl can be used to debug network issues, as it allows developers to inspect HTTP headers and response data.
  • Integration with other tools: Curl can be easily integrated with other tools and programming languages, making it a valuable tool for many developers.

#1. Install curl on Linux, Mac, and Windows Systems

cURL has cross-platform compatibility, it runs on a wide variety of platforms, including Linux, Windows, macOS, and many others.

Follow the below steps to install it on your desired system.

a. Install cURL on Linux

cURL exists in default repositories of many Linux distributions. This makes its installation so easy. You can use the below commands to install cURL on your Linux system.

##on Debian/Ubuntu/Kali Linux
sudo apt update && sudo apt install curl

##On Fedora/Rocky Linux/Alma Linux/CentOS Stream
sudo yum install curl

##On Arch/Manjaro
sudo pacman -Syy

##On OpenSUSE
sudo zypper refresh
sudo zypper install curl

Once installed, check the version with the command:

$ curl --version
curl 7.61.1 (x86_64-redhat-linux-gnu) libcurl/7.61.1 OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.6 libidn2/2.2.0 libpsl/0.20.2 (+libidn2/2.2.0) libssh/0.9.6/openssl/zlib nghttp2/1.33.0
Release-Date: 2018-09-05
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz brotli TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL 

b. Install cURL on MacOS/OS-X

Curl is pre-installed on macOS, so you should already have it installed on your system. To check if it is installed, you can open a Terminal window and type the following command:

curl --version

If it is not installed or you want to update it, you can use HomeBrew as shown.

Install HomeBrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Now install cuRL on MacOS with the command:

brew install curl

c. Install cURL on Windows

To install cURL on Windows, download the binary from the official downloads page. On the page, select an appropriate ZIP file for your system.

Install and Use curl on Linux, Mac, and Windows Systems

Once downloaded, extract the contents of the file as shown.

Install and Use curl on Linux, Mac, and Windows Systems

The next thing is to add the directory to your system’s PATH environment variable so that you can run cURL from anywhere on your system. Here’s how:

First, launch the “Environment Variables” from the start menu.

Once launched, click on Environment variables:

Under the system variables, click on New

Now provide the name and the values by specifying the PATH on your cURL command:

#2. How to Use cURL on Linux, Mac, and Windows Systems

The command for sending a cURL request follows the below syntax:

curl [OPTIONS]  [URL]

There are several options that can be used with cURL. Some of them are:

  • -A Specify your USER_AGENT
  • -b Save a cookie to file
  • -c Send a cookie to the server from a file
  • -C Continue loading the file from the break or the specified offset
  • -d Send data using the POST method
  • -E Use an external SSL certificate
  • -F Send data as a form
  • -G If this option is enabled, then all data specified in the -d option will be transferred using the GET method
  • -H Send headers to the server
  • -I Receive only the HTTP header, and ignore the entire page content
  • -k Allows insecure SSL connections
  • -L Accept and process redirects
  • -o Output the content of the page to a file
  • -O Save the content to a file with the name of the page or file on the server
  • -p Use proxy
  • -X Specifies the HTTP method to use (e.g., GET, POST, PUT, DELETE, etc.)
  • -u Specifies a username and password to use for authentication (e.g., -u username:password)
  • -v Maximum verbose output

Now let us learn how you can use some of the options with cURL.

Download Files with cURL

One of the basic tasks for cURL is downloading files. To use it, you have to pass the -O or the -o option together with your command.

These two options have a slight difference. that is;

  • -O saves the file with the same name as the remote one
  • -o allows you to save the file with a different filename or location

The command for that will have the below syntax:

curl -O [URL]
##OR
curl -o [file name] [URL]

For example:

$ curl -o  sample-file.zip https://example.com/file.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   3376      0 --:--:-- --:--:-- --:--:--  3376
$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  sample-file.zip  Templates  Videos

Send POST requests data using cURL

To send a POST request with data using cURL, you can use the -d option followed by the data you want to send. The command has the below syntax:

curl -d "key1=value1&key2=value2" [URL]

Here is an example command:

curl -X POST -d 'param1=value1&param2=value2' https://example.com/api/endpoint

In the above example, we have sent a POST request to https://example.com/api/endpoint with two parameters: param1 with the value value1 and param2 with the value value2.

This command can also be used with the -H flag if you need to send HTTP headers. For example:

curl -X POST -H 'Content-Type: application/json' -d '{"name": "John", "age": 30}' https://example.com/api/endpoint

Get HTTP headers using Curl

To get HTTP headers, you use the -I or –head option along with the URL of the website. The command has the syntax:

curl -I [URL]
##OR
curl --head  [URL]

For example:

$ curl -I https://www.example.com/
HTTP/2 200 
content-encoding: gzip
accept-ranges: bytes
age: 318344
cache-control: max-age=604800
content-type: text/html; charset=UTF-8
date: Mon, 08 May 2023 20:50:48 GMT
etag: "3147526947"
expires: Mon, 15 May 2023 20:50:48 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server: ECS (nyb/1DCD)
x-cache: HIT
content-length: 648

The above command sends a HEAD request to the URL and returns only the headers, not the body of the response. But if you want to see both the headers and the response body, you can use the -i or –include option.

For example:

$ curl -i https://www.example.com/
HTTP/2 200 
age: 406935
cache-control: max-age=604800
content-type: text/html; charset=UTF-8
date: Mon, 08 May 2023 20:52:16 GMT
etag: "3147526947+gzip+ident"
expires: Mon, 15 May 2023 20:52:16 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server: ECS (nyb/1D07)
vary: Accept-Encoding
x-cache: HIT
content-length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
.....

Get HTTP Authentication using cURL

You can use the -u option to make a request with HTTP authentication using cURL. The command for that bears the syntax:

curl -u [Username:Password] [URL]

For example:

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

You can also verify the authentication using the -H option.

curl -H "Authorization: Basic $(echo -n 'username:password' | base64)"  https://example.com

Upload Files Using cURL

cURL supports file upload using the -F option followed by the file parameter name and file path. The command has the below syntax:

curl -X POST -H "Authorization: Bearer ACCESS_TOKEN" -F "file=@/path/to/file.jpg" https://example.com/upload

In the command:

  • X option specifies the HTTP method as POST.
  • -H option sets the Authorization header with a bearer access token.
  • -F option is used to upload a file.
  • file=@/path/to/file.jpg specifies the parameter name and file path to upload.
  • The last argument is the URL of the API endpoint to which the file will be uploaded.

Use Proxy with cURL

To use a proxy when making a cURL request, you can use the x or –proxy option. The command bears he below syntax:

curl -x [proxy-URL] [curl-URL]

For example:

curl -x http://proxy.example.com:8080 http://example.com

If you need to authenticate to the proxy server, you can include the creds:

curl -x http://user:password@proxy.example.com:8080 http://example.com

Provide the user and password for your proxy server and proceed.

Set a timeout for cURL

It is also possible to set a timeout for a cURL command. To achieve that, you use the –connect-timeout parameter to set the time in seconds that allows is required for cURL to connect to the server. You can also use the max-time or -m parameter for the maximum time authorized for the whole session.

The command bears the syntax below:

curl -m [SECONDS] [URL]
##OR
curl --connection-timeout [SECONDS] [URL]

Set Download Speed on cURL

You can also restrict cURL to use some download speeds on your network. To achieve that, use the –limit-rate option.

curl --limit-rate 200k -O [URL]

The above will set a 200k limit download speed on your server.

Ignoring SSL Certificate Checks with cURL

At times, you can self-signed or invalid certificates for the server you are trying to contact. To do that, you need to pass the -k or –insecure option.

The command will have the below syntax:

curl -k [URL]

For example:

curl -k https://expired.badssl.com

This will skip the SSL certificate checks and still maintain SSL-encrypted communications.

Combining cURL Options

It is also possible to use several cURL options together. For example:

curl -ivk https://expired.badssl.com

In the above command:

  • -i – shows the response header along with the response body
  • -v – enables verbose output to display detailed information about the request and response
  • -k – allows insecure connections to SSL sites, ignoring SSL certificate errors

Closing Thoughts

That is it! There are several uses of cURL not covered in this guide. Feel free to explore them on your own. I hope this was informative.

See more:

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here