How to Install and Use Curl on Ubuntu 18.04

How to Install and Use Curl on Ubuntu 18.04

Curl is a command-line tool that allows you to transfer data from or to a remote server. It supports various protocols, such as HTTP, HTTPS, FTP, SFTP, SCP, and more. You can use curl to download files, upload files, test APIs, and perform many other tasks.

In this article, we will show you how to install and use curl on Ubuntu 18.04.

Installing Curl on Ubuntu

Curl is included in the default Ubuntu 18.04 repositories, so you can easily install it with the apt package manager. To do so, open your terminal and run the following commands:

sudo apt update
sudo apt install curl

This will install curl and its dependencies on your system.

To verify that curl has been installed correctly, type:

curl --version

The output will show the curl version and the supported protocols:

curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL 

Using Curl

Curl has many options and features that allow you to perform various operations with it. In this section, we will show you some basic examples of using curl.

Downloading Files with Curl

To download a file with curl, you can use the -o or -O options.

The lowercase -o option allows you to specify the name of the file you are downloading:

curl -o linux.tar.xz https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.5.tar.xz

This will download the file from the given URL and save it as linux.tar.xz in your current directory.

The uppercase -O option will save the file with its original name:

curl -O https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.5.tar.xz

This will download the file and save it as linux-5.0.5.tar.xz in your current directory.

You can also download multiple files at once by specifying multiple URLs:

curl -O https://example.com/file1.zip -O https://example.com/file2.zip

This will download both file1.zip and file2.zip from the given URLs.

Uploading Files with Curl

To upload a file with curl, you can use the -T option and specify the protocol and destination of the upload.

For example, to upload a file named test.txt to an FTP server with username user and password pass, you can use the following command:

curl -T test.txt -u user:pass ftp://example.com/

This will upload the file test.txt to the root directory of the FTP server.

You can also upload multiple files at once by specifying multiple files after the -T option:

curl -T "{file1,file2,file3}" -u user:pass ftp://example.com/

This will upload file1, file2, and file3 to the FTP server.

Testing APIs with Curl

Curl can also be used to test APIs by sending HTTP requests and displaying the responses.

For example, to send a GET request to an API endpoint that returns a list of users in JSON format, you can use the following command:

curl https://jsonplaceholder.typicode.com/users

This will display the response in your terminal:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    ...
  },
  ...
]

You can also send other types of HTTP requests, such as POST, PUT, PATCH, DELETE, etc., by using the -X option and specifying the request method.

For example, to send a POST request to an API endpoint that creates a new user with some data in JSON format, you can use the following command:

curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe","username":"johndoe","email":"johndoe@example.com"}' https://jsonplaceholder.typicode.com/users

This will display the response in your terminal:

{
  "name": "John Doe",
  "username": "johndoe",
  "email": "johndoe@example.com",
  "id": 11
}

You can also use the -i option to display the HTTP headers of the response along with the body.

For example, to display the headers of a GET request to an API endpoint that returns a single user by ID, you can use the following command:

curl -i https://jsonplaceholder.typicode.com/users/1

This will display something like this:

HTTP/1.1 200 OK
Date: Tue, 02 Apr 2019 21:15:32 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 238
Connection: keep-alive
Set-Cookie: __cfduid=d4c96698cfcf10e083dfff6a8f9e90f9c1554234131; expires=Wed, 01-Apr-20 21:15:31 GMT; path=/; domain=.typicode.com; HttpOnly; Secure
X-Powered-By: Express
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: max-age=14400
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Etag: W/"ee-Iwz/UyUeK8KbCgE+QJfNqQ"
Via: 1.1 vegur
CF-Cache-Status: HIT
Age: 1194
Accept-Ranges: bytes

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  ...
}

In this article, we have shown you how to install and use curl on Ubuntu 18.04. Curl is a powerful tool that allows you to transfer data from or to a remote server using various protocols.

You can learn more about curl by typing man curl or visiting its official website.

We hope you found this article useful and informative. If you have any questions or feedback, feel free to leave a comment below.

Leoz Dawn Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *